1.导入
``
List<WriteOffBaseInfo> writeOffBaseInfoList = new ArrayList<>();
List<WriteOffBaseInfo> writeOffBaseInfoList1 = new ArrayList<>();
ReadSheet readSheet = EasyExcelFactory.readSheet(0,"0").head(WriteOffBaseInfo.class).registerReadListener(new AnalysisEventListener<WriteOffBaseInfo>() {
@Override
public void invoke(WriteOffBaseInfo writeOffBaseInfo, AnalysisContext analysisContext) {
writeOffBaseInfoList.add(writeOffBaseInfo);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
log.info("读取到第一个sheet,数据长度为{}", writeOffBaseInfoList.size());
}
}).build();
ReadSheet readSheet1 = EasyExcelFactory.readSheet(1,"1").head(WriteOffBaseInfo.class).registerReadListener(new AnalysisEventListener<WriteOffBaseInfo>() {
@Override
public void invoke(WriteOffBaseInfo writeOffBaseInfo, AnalysisContext analysisContext) {
writeOffBaseInfoList1.add(writeOffBaseInfo);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
log.info("读取到第二个sheet,数据长度为{}", writeOffBaseInfoList1.size());
}
}).build();
ExcelReader excelReader = EasyExcelFactory.read(file.getInputStream()).build();
excelReader.read(readSheet,readSheet1);
excelReader.finish();
``
2.导出
点击查看代码
ExcelWriter excelWriterBuilder = EasyExcelFactory.write(response.getOutputStream()).build();
WriteSheet htSheetWrite = EasyExcelFactory.writerSheet(0).sheetName("数据明细").head(classType).includeColumnFiledNames(includeColumnFiledNames).build();
//classType 代表类型 Class
WriteSheet htSheetWriteTemplate = EasyExcelFactory.writerSheet(1).sheetName("数据模板").head(classType).build();
excelWriterBuilder.write(writeOffBaseInfoList, htSheetWrite);
//参数一为要导出的数据,参数二为配置信息
excelWriterBuilder.write(null, htSheetWriteTemplate);
excelWriterBuilder.finish();