File tree Expand file tree Collapse file tree 2 files changed +36
-0
lines changed
java/com/alibaba/easyexcel/test/demo/read Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change 16
16
import com .alibaba .excel .read .listener .PageReadListener ;
17
17
import com .alibaba .excel .read .listener .ReadListener ;
18
18
import com .alibaba .excel .read .metadata .ReadSheet ;
19
+ import com .alibaba .excel .read .metadata .holder .csv .CsvReadWorkbookHolder ;
19
20
import com .alibaba .excel .util .ListUtils ;
20
21
import com .alibaba .fastjson2 .JSON ;
21
22
@@ -309,4 +310,28 @@ public void noModelRead() {
309
310
// 这里 只要,然后读取第一个sheet 同步读取会自动finish
310
311
EasyExcel .read (fileName , new NoModelDataListener ()).sheet ().doRead ();
311
312
}
313
+
314
+ /**
315
+ * 自定义修改csv配置
316
+ */
317
+ @ Test
318
+ public void csvFormat () {
319
+ String fileName = TestFileUtil .getPath () + "demo" + File .separator + "demo.csv" ;
320
+ try (ExcelReader excelReader = EasyExcel .read (fileName , DemoData .class , new DemoDataListener ()).build ()) {
321
+ // 判断是 csv 文件
322
+ if (excelReader .analysisContext ().readWorkbookHolder () instanceof CsvReadWorkbookHolder ) {
323
+ CsvReadWorkbookHolder csvReadWorkbookHolder = (CsvReadWorkbookHolder )excelReader .analysisContext ()
324
+ .readWorkbookHolder ();
325
+ // 设置成逗号分隔 当然默认也是逗号分隔
326
+ // 这里要注意 withDelimiter 会重新生成一个 所以要放回去
327
+ csvReadWorkbookHolder .setCsvFormat (csvReadWorkbookHolder .getCsvFormat ().withDelimiter (',' ));
328
+ }
329
+
330
+ // 拿到所有 sheet
331
+ List <ReadSheet > readSheetList = excelReader .excelExecutor ().sheetList ();
332
+ // 如果只想读取第一个 咋样传入参数即可
333
+ //ReadSheet readSheet = EasyExcel.readSheet(0).build();
334
+ excelReader .read (readSheetList );
335
+ }
336
+ }
312
337
}
Original file line number Diff line number Diff line change
1
+ 字符串标题,日期标题,数字标题
2
+ 字符串0,2020-01-01 01:01:00,1
3
+ 字符串1,2020-01-02 01:01:00,2
4
+ 字符串2,2020-01-03 01:01:00,3
5
+ 字符串3,2020-01-04 01:01:00,4
6
+ 字符串4,2020-01-05 01:01:00,5
7
+ 字符串5,2020-01-06 01:01:00,6
8
+ 字符串6,2020-01-07 01:01:00,7
9
+ 字符串7,2020-01-08 01:01:00,8
10
+ 字符串8,2020-01-09 01:01:00,9
11
+ 字符串9,2020-01-10 01:01:00,10
You can’t perform that action at this time.
0 commit comments