1.首先准备实体类 对应excel中得列名
@Data
public class ReadTableEntity {
@ExcelProperty(value = "表名",index = 0)
private String tableName;
@ExcelProperty(value = "字段名",index = 1)
private String columnName;
@ExcelProperty(value = "数据项",index = 2)
private String columnComm;
@ExcelProperty(value = "类型",index = 3)
private String columnType;
@ExcelProperty(value = "宽度",index = 4)
private String columnWidth;
@ExcelProperty(value = "要求",index = 5)
private String columnRequire;
@ExcelProperty(value = "说明",index = 6)
private String columnDesc;
@ExcelProperty(value = "字段名",index = 10)
private String cpColumnName;
@ExcelProperty(value = "数据项",index = 11)
private String cpColumnComm;
@ExcelProperty(value = "类型",index = 12)
private String cpColumnType;
@ExcelProperty(value = "宽度",index = 13)
private String cpColumnWidth;
@ExcelProperty(value = "要求",index = 14)
private String cpColumnRequire;
@ExcelProperty(value = "说明",index = 15)
private String cpColumnDesc;
}
2.读操作
EasyExcel.read("文件读取路径", ReadTableEntity.class, new PageReadListener<ReadTableEntity>(dataList -> {
for (ReadTableEntity demoData : dataList) {
list.add(demoData);
// log.info("读取到一条数据{}", JSON.toJSONString(demoData));
}
})).sheet(i).headRowNumber(2).doRead();
3.写操作
写法1
EasyExcel.write("文件读取路径", DemoData.class)
.sheet("模板")
.doWrite(() -> {
// 分页查询数据
return data();
});
写法2
EasyExcel.write("文件读取路径", DemoData.class).sheet("模板").doWrite(data());
写法3
try (ExcelWriter excelWriter = EasyExcel.write("文件读取路径", DemoData.class).build()) {
WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
excelWriter.write(data(), writeSheet);
}
4.随便搞点数据
private List<DemoData> data() {
List<DemoData> list = ListUtils.newArrayList();
for (int i = 0; i < 10; i++) {
DemoData data = new DemoData();
data.setString("字符串" + i);
data.setDate(new Date());
data.setDoubleData(0.56);
list.add(data);
}
return list;
}
maven坐标
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.1.1</version>
</dependency>