7
7
import java .util .List ;
8
8
import java .util .Map ;
9
9
10
- import cn .idev .excel .read .listener .PageReadListener ;
11
- import cn .idev .excel .test .util .TestFileUtil ;
12
10
import cn .idev .excel .EasyExcel ;
11
+ import cn .idev .excel .read .listener .PageReadListener ;
13
12
import cn .idev .excel .support .ExcelTypeEnum ;
13
+ import cn .idev .excel .test .util .TestFileUtil ;
14
14
15
15
import lombok .extern .slf4j .Slf4j ;
16
16
import org .junit .jupiter .api .Assertions ;
20
20
import org .junit .jupiter .api .TestMethodOrder ;
21
21
22
22
/**
23
+ * Test simple read/write for Excel formats:
24
+ * <li>Excel 2007 format (.xlsx)</li>
25
+ * <li>Excel 2003 format (.xls)</li>
26
+ * <li>CSV format (.csv)</li>
27
+ * Test methods are grouped by prefixes:
28
+ * <li>t0x: Basic read/write tests</li>
29
+ * <li>t1x: Synchronous reading tests</li>
30
+ * <li>t2x: Specific feature tests (sheet name reading, pagination, etc.)</li>
31
+ *
23
32
* @author Jiaju Zhuang
24
33
*/
25
34
@ TestMethodOrder (MethodOrderer .MethodName .class )
@@ -52,8 +61,14 @@ public void t03ReadAndWriteCsv() {
52
61
readAndWrite (fileCsv );
53
62
}
54
63
64
+ /**
65
+ * Test simple read/write with file
66
+ *
67
+ * @param file file
68
+ */
55
69
private void readAndWrite (File file ) {
56
70
EasyExcel .write (file , SimpleData .class ).sheet ().doWrite (data ());
71
+ //use a SimpleDataListener object to handle and check result
57
72
EasyExcel .read (file , SimpleData .class , new SimpleDataListener ()).sheet ().doRead ();
58
73
}
59
74
@@ -72,44 +87,96 @@ public void t06ReadAndWriteCsv() throws Exception {
72
87
readAndWriteInputStream (fileCsv , ExcelTypeEnum .CSV );
73
88
}
74
89
90
+ /**
91
+ * Test simple read/write with InputStream/OutputStream
92
+ *
93
+ * @param file file used to generate InputStream/OutputStream
94
+ * @param excelTypeEnum excel type enum
95
+ * @throws Exception exception
96
+ */
75
97
private void readAndWriteInputStream (File file , ExcelTypeEnum excelTypeEnum ) throws Exception {
76
98
EasyExcel .write (new FileOutputStream (file ), SimpleData .class ).excelType (excelTypeEnum ).sheet ().doWrite (data ());
99
+ //use a SimpleDataListener object to handle and check result
77
100
EasyExcel .read (new FileInputStream (file ), SimpleData .class , new SimpleDataListener ()).sheet ().doRead ();
78
101
}
79
102
103
+ /**
104
+ * Test synchronous reading of Excel 2007 format
105
+ */
80
106
@ Test
81
107
public void t11SynchronousRead07 () {
82
108
synchronousRead (file07 );
83
109
}
84
110
111
+ /**
112
+ * Test synchronous reading of Excel 2003 format
113
+ */
85
114
@ Test
86
115
public void t12SynchronousRead03 () {
87
116
synchronousRead (file03 );
88
117
}
89
118
119
+ /**
120
+ * Test synchronous reading of CSV format
121
+ */
90
122
@ Test
91
123
public void t13SynchronousReadCsv () {
92
124
synchronousRead (fileCsv );
93
125
}
94
126
127
+ /**
128
+ * test read sheet in an Excel file with specified sheetName
129
+ */
95
130
@ Test
96
131
public void t21SheetNameRead07 () {
97
132
List <Map <Integer , Object >> list = EasyExcel .read (
98
133
TestFileUtil .readFile ("simple" + File .separator + "simple07.xlsx" ))
134
+ //set the sheet name to read
99
135
.sheet ("simple" )
100
136
.doReadSync ();
101
137
Assertions .assertEquals (1 , list .size ());
102
138
}
103
139
140
+ /**
141
+ * test read sheet in an Excel file with specified sheetNo
142
+ */
143
+ @ Test
144
+ public void t22SheetNoRead07 () {
145
+ List <Map <Integer , Object >> list = EasyExcel .read (
146
+ TestFileUtil .readFile ("simple" + File .separator + "simple07.xlsx" ))
147
+ // sheetNo begin with 0
148
+ .sheet (1 )
149
+ .doReadSync ();
150
+ Assertions .assertEquals (1 , list .size ());
151
+ }
152
+
153
+ /**
154
+ * Test page reading with PageReadListener
155
+ * <p>
156
+ * PageReadListener processes Excel data in batches, triggering callbacks when reaching
157
+ * specified batch size. {@link PageReadListener#invoke}
158
+ * Useful for large files to prevent memory overflow
159
+ * </p>
160
+ */
104
161
@ Test
105
- public void t22PageReadListener07 () {
162
+ public void t23PageReadListener07 () {
163
+ //Read the first 5 rows of an Excel file
106
164
EasyExcel .read (file07 , SimpleData .class ,
107
165
new PageReadListener <SimpleData >(dataList -> {
108
166
Assertions .assertEquals (5 , dataList .size ());
109
167
}, 5 ))
110
168
.sheet ().doRead ();
111
169
}
112
170
171
+ /**
172
+ * Synchronous reading of Excel files
173
+ * <p>
174
+ * Unlike asynchronous reading with listeners, synchronous reading loads all data into memory
175
+ * and returns a complete data list. It may cause memory issues when processing large files.
176
+ * </p>
177
+ *
178
+ * @param file file
179
+ */
113
180
private void synchronousRead (File file ) {
114
181
// Synchronous read file
115
182
List <Object > list = EasyExcel .read (file ).head (SimpleData .class ).sheet ().doReadSync ();
@@ -118,6 +185,11 @@ private void synchronousRead(File file) {
118
185
Assertions .assertEquals (((SimpleData )list .get (0 )).getName (), "姓名0" );
119
186
}
120
187
188
+ /**
189
+ * mock data
190
+ *
191
+ * @return {@link List }<{@link SimpleData }>
192
+ */
121
193
private List <SimpleData > data () {
122
194
List <SimpleData > list = new ArrayList <SimpleData >();
123
195
for (int i = 0 ; i < 10 ; i ++) {
0 commit comments