模板文件
sheet页复制
ClassPathResource templateResource = new ClassPathResource("template\\" + templateName);
InputStream inputStream = templateResource.getInputStream();
XSSFWorkbook workbook = new XSSFWorkbook(templateResource.getInputStream());
workbook.setSheetName(0, tableVoucherVoList.get(0).getKCode() + "-" + tableVoucherVoList.get(0).getKName());
if (!exportParam.isMultiExcelExportFlag()){
for (int i = 1; i < sheetNum; i++) {
workbook.cloneSheet(0, tableVoucherVoList.get(i).getKCode() + "-" + tableVoucherVoList.get(i).getKName());
}
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
workbook.write(bos);
byte[] bArray = bos.toByteArray();
inputStream = new ByteArrayInputStream(bArray);
ExcelWriter excelWriter = EasyExcel.write(out).withTemplate(inputStream).build();
FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
写入数据
for (int i = 0; i < tableVoucherVoList.size(); i++) {
Gcw3AccountTableVoucherVo voucherVo = tableVoucherVoList.get(i);
WriteSheet writeSheet = EasyExcel.writerSheet(voucherVo.getKCode() + "-" + voucherVo.getKName()).build();;
map.put("kCode", voucherVo.getKCode());
map.put("kName", voucherVo.getKName());
excelWriter.fill(map, writeSheet);
excelWriter.fill(new FillWrapper("voucher", voucherVo.getDetailVoList()), fillConfig, writeSheet);
}
excelWriter.finish();
实现效果