excel数据
a-title | b-title | c-title | d-title | ||
---|---|---|---|---|---|
c-sub-1 | c-sub-2 | d-sub-1 | d-sub-2 | ||
1 | 2 | 3 | 4 | 5 | 6 |
6 | 4 | 5 | 2 | 2 | 2 |
定义导入时的映射对象:
@Data
public class Content {
@ExcelProperty(index = 0)
private String field1;
@ExcelProperty(index = 1)
private String field2;
@ExcelProperty(index = 2)
private String field3;
@ExcelProperty(index = 3)
private String field4;
@ExcelProperty(index = 4)
private String field5;
@ExcelProperty(index = 5)
private String field6;
}
读取内容:
public class MainTest{
public static void main(String[] args){
ExcelReader build = EasyExcel.read(new File("/home/demo.xlsx")).build();
SyncReadListener syncReadListener = new SyncReadListener();
ReadSheet readSheet1 =
EasyExcel.readSheet(0).head(Content.class).registerReadListener(syncReadListener).build();
build.read(Arrays.asList(readSheet1));
syncReadListener.getList().forEach(x -> System.out.println(x.toString()));
}
}
输出内容为:
Content(field1=null, field2=null, field3=c-sub-1, field4=c-sub-2, field5=d-sub-1, field6=d-sub-2)
Content(field1=1, field2=2, field3=3, field4=4, field5=5, field6=6)
Content(field1=6, field2=4, field3=5, field4=2, field5=2, field6=2)
此时数据已经读入,且映射完成,但是第一行数据是部分表头数据,是无效数据