复现代码
@Test
public void test() {
// 注意商户号列的空格
String csvData = "序号,商户号,商户名\n" +
"1, 商户号1 ,商户名1\n" +
"2, 商户号2 ,商户名2\n" +
"3, 商户号3 ,商户名3";
EasyExcel.read(new ByteArrayInputStream(csvData.getBytes()))
.registerReadListener(new AnalysisEventListener<RowContent>() {
@Override
public void invoke(RowContent o, AnalysisContext analysisContext) {
System.out.println(o + ",原始长度=" + o.getKey2().length() + ",trim长度后=" + o.getKey2().trim().length());
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
}
})
.autoTrim(true)
.headRowNumber(1)
.head(RowContent.class)
.excelType(ExcelTypeEnum.CSV)
.sheet()
.doRead();
}
model类
@Data
public class RowContent {
@ExcelProperty(value = "序号")
private String key1;
@ExcelProperty(value = "商户号")
private String key2;
@ExcelProperty(value = "商户名")
private String key3;
}
运行结果
RowContent(key1=1, key2= 商户号1 , key3=商户名1),原始长度=6,trim长度后=4
RowContent(key1=2, key2= 商户号2 , key3=商户名2),原始长度=6,trim长度后=4
RowContent(key1=3, key2= 商户号3 , key3=商户名3),原始长度=6,trim长度后=4