-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
110 lines (98 loc) · 3.84 KB
/
Main.java
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Main {
@Test
public void writeOneSheet() {
EasyExcel
.write("one.xlsx")
.sheet("test")
.doWrite(Stream.iterate(1, i -> i + 1)
.limit(20)
.map(i -> List.of("A" + i, "B" + i, "C" + i, "D" + i))
.collect(Collectors.toList()));
}
@Test
public void writeMoreSheet() {
ExcelWriter excel = EasyExcel
.write("more.xlsx")
.build();
for (int i = 0; i < 5; i++) {
String sheetName = "Sheet" + i;
excel.write(
Stream.iterate(1, j -> j + 1)
.limit(20)
.map(j -> List.of(sheetName + "A" + j, sheetName + "B" + j, sheetName + "C" + j, sheetName + "D" + j))
.collect(Collectors.toList()),
EasyExcel.writerSheet(i, "Sheet" + i).build()
);
}
excel.finish();
}
@Test
public void readOneSheet() {
EasyExcel
.read("one.xlsx")
.registerReadListener(new AnalysisEventListener<Map<Integer, String>>() {
@Override
public void invoke(Map<Integer, String> data, AnalysisContext context) {
System.out.println(data);
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
System.out.println("doAfterAllAnalysed");
}
})
.doReadAll();
}
@Test
public void readMoreSheet() {
EasyExcel
.read("more.xlsx")
.registerReadListener(new AnalysisEventListener<Map<Integer, String>>() {
@Override
public void invoke(Map<Integer, String> data, AnalysisContext context) {
System.out.println(data);
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
System.out.println("SheetName:" + context.readSheetHolder().getSheetName());
}
})
.doReadAll();
}
@Test
public void transfer() {
ExcelWriter excel = EasyExcel.write("transfer.xlsx").build();
EasyExcel
.read("more.xlsx")
.registerReadListener(new AnalysisEventListener<Map<Integer, String>>() {
private List<List<String>> sheet = new ArrayList<>();
@Override
public void invoke(Map<Integer, String> data, AnalysisContext context) {
sheet.add(data
.entrySet()
.stream()
.sorted(Comparator.comparingInt(Map.Entry::getKey))
.map(Map.Entry::getValue)
.collect(Collectors.toList())
);
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
excel.write(sheet, EasyExcel.writerSheet(context.readSheetHolder().getSheetName()).build());
sheet = new ArrayList<>();
}
})
.doReadAll();
excel.finish();
}
}