WbTest.java
2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.taover.easyexcel.test;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import com.taover.easyexcel.EasyExcel;
public class WbTest {
public static void main(String[] args) {
File dataFile = new File("C:\\Users\\Administrator\\Desktop\\Excel隐藏行\\配送确认1.16(1).xlsx");
// List<List<Object>> data = transListMapTo2List(EasyExcel.read(dataFile).readHiddenRow(false).headRowNumber(0).doReadSelectedSync());
// for(List<Object> row: data) {
// for(Object item: row) {
// System.out.print(item+":");
// }
// System.out.println();
// }
Map<Integer, List<Map<Integer, Object>>> data2 = EasyExcel.read(dataFile).readHiddenRow(false).headRowNumber(0).doReadAllSyncForMap();
System.out.println(data2.get(0).size());
}
private static List<List<Object>> transListMapTo2List(List<Object> listMapData) {
List<List<Object>> data = new ArrayList<List<Object>>();
if(listMapData == null) {
return data;
}
for(Object item: listMapData) {
data.add(transMapToList((Map<Integer, Object>) item));
}
return data;
}
private static List<Object> transMapToList(Map<Integer, Object> cellData) {
List<Object> data = new ArrayList<Object>((int)(cellData.size()*1.5));
if(cellData == null || cellData.isEmpty()) {
return data;
}
//获取最大索引
int maxIndex = 0;
for(Entry<Integer, Object> item: cellData.entrySet()) {
if(item.getKey() > maxIndex && item.getValue()!=null && !"".equals(item.getValue())) {
maxIndex = item.getKey();
}
}
//设置元素并填入空串
fillBlankToList(data, maxIndex+1);
//置入元素
int dataSize = data.size();
for(Entry<Integer, Object> item: cellData.entrySet()) {
if(item.getKey() >= dataSize) {
continue;
}
if(item.getValue() == null) {
data.set(item.getKey(), "");
}else {
data.set(item.getKey(), item.getValue());
}
}
return data;
}
private static void fillBlankToList(List<Object> data, int blankNumber) {
for(int i=0; i<blankNumber; ++i) {
data.add("");
}
}
}