Browse Source

Merge pull request #3875 from alibaba/bugfix

新增csv样例
pull/3915/head
Jiaju Zhuang 5 months ago committed by GitHub
parent
commit
4658a0e87d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 25
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java
  2. 11
      easyexcel-test/src/test/resources/demo/demo.csv

25
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java

@ -16,6 +16,7 @@ import com.alibaba.excel.enums.CellExtraTypeEnum;
import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.read.metadata.holder.csv.CsvReadWorkbookHolder;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.fastjson2.JSON;
@ -309,4 +310,28 @@ public class ReadTest {
// 这里 只要,然后读取第一个sheet 同步读取会自动finish
EasyExcel.read(fileName, new NoModelDataListener()).sheet().doRead();
}
/**
* 自定义修改csv配置
*/
@Test
public void csvFormat() {
String fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.csv";
try (ExcelReader excelReader = EasyExcel.read(fileName, DemoData.class, new DemoDataListener()).build()) {
// 判断是 csv 文件
if (excelReader.analysisContext().readWorkbookHolder() instanceof CsvReadWorkbookHolder) {
CsvReadWorkbookHolder csvReadWorkbookHolder = (CsvReadWorkbookHolder)excelReader.analysisContext()
.readWorkbookHolder();
// 设置成逗号分隔 当然默认也是逗号分隔
// 这里要注意 withDelimiter 会重新生成一个 所以要放回去
csvReadWorkbookHolder.setCsvFormat(csvReadWorkbookHolder.getCsvFormat().withDelimiter(','));
}
// 拿到所有 sheet
List<ReadSheet> readSheetList = excelReader.excelExecutor().sheetList();
// 如果只想读取第一个 咋样传入参数即可
//ReadSheet readSheet = EasyExcel.readSheet(0).build();
excelReader.read(readSheetList);
}
}
}

11
easyexcel-test/src/test/resources/demo/demo.csv

@ -0,0 +1,11 @@
字符串标题,日期标题,数字标题
字符串0,2020-01-01 01:01:00,1
字符串1,2020-01-02 01:01:00,2
字符串2,2020-01-03 01:01:00,3
字符串3,2020-01-04 01:01:00,4
字符串4,2020-01-05 01:01:00,5
字符串5,2020-01-06 01:01:00,6
字符串6,2020-01-07 01:01:00,7
字符串7,2020-01-08 01:01:00,8
字符串8,2020-01-09 01:01:00,9
字符串9,2020-01-10 01:01:00,10
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
Loading…
Cancel
Save