一、模板
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/5cdb8d9c3d2a5e34b391663621065282.png)
二、代码
@PostMapping("/monitoring-record-export")
@RequestLog("体征监测记录导出")
@ApiOperation(value = "体征监测记录导出")
public void monitoringRecordExport(@RequestBody MonitoringRecordExportReq req, HttpServletResponse response) throws IOException {
List<MonitoringRecordExportRep> repList = infectiousDiseaseReportService.monitoringRecordExport(req);
//读取模板
ClassPathResource classPathResource = new ClassPathResource("template/health-monitoring-template.xlsx");
InputStream inputStream = classPathResource.getInputStream();
log.info("获取模板={}", inputStream);
//创建一个工作薄对象
ExcelWriter workBook = null;
List<String> sheetNames = new ArrayList<>();
try (ServletOutputStream os = response.getOutputStream()) {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/octet-stream;charset=UTF-8");
final String encodeFileName = URLEncoder.encode("体征监测记录" + ".xlsx", "UTF-8");
response.setHeader("Content-Disposition", String.format("attachment;filename=%s", encodeFileName));
Map<String, Integer> nameMap = new HashMap<String, Integer>();
for (MonitoringRecordExportRep monitoringRecordExportRep : repList) {
String sheetName;
if (nameMap.containsKey(monitoringRecordExportRep.getUserName())) {
sheetName = monitoringRecordExportRep.getUserName() + "+" + monitoringRecordExportRep.getNationalityName() + "(" + nameMap.get(monitoringRecordExportRep.getUserName()) + ")";
nameMap.put(monitoringRecordExportRep.getUserName(), Objects.isNull(nameMap.get(monitoringRecordExportRep.getUserName())) ? 1 : nameMap.get(monitoringRecordExportRep.getUserName()) + 1);
} else {
sheetName = monitoringRecordExportRep.getUserName() + "+" + monitoringRecordExportRep.getNationalityName();
nameMap.put(monitoringRecordExportRep.getUserName(), Objects.isNull(nameMap.get(monitoringRecordExportRep.getUserName())) ? 1 : nameMap.get(monitoringRecordExportRep.getUserName()) + 1);
}
sheetNames.add(sheetName);
}
//原模板只有一个sheet,通过poi复制出需要的sheet个数的模板
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(inputStream);
//循环表名
if (CollectionUtil.isEmpty(sheetNames)){
return;
}
for (String sheetName : sheetNames) {
//第一参数是 以谁为模板,第二个参数是复制出来的模板名
xssfWorkbook.cloneSheet(0, sheetName);
}
//写到流里
ByteArrayOutputStream bos = new ByteArrayOutputStream();
xssfWorkbook.write(bos);
byte[] bArray = bos.toByteArray();
InputStream is = new ByteArrayInputStream(bArray);
//创建一个工作薄对象
workBook = EasyExcel.write(os).withTemplate(is).excelType(ExcelTypeEnum.XLSX).build();
//开启多组之间换行
FillConfig fillConfig = FillConfig.builder().forceNewRow(true).build();
//创建工作表对象
if (!CollectionUtil.isEmpty(sheetNames)) {
for (int i = 0; i < sheetNames.size(); i++) {
//插入数据
//插入用户基本信息数据
log.info("sheetName={}", sheetNames.get(i));
WriteSheet writeSheet = EasyExcel.writerSheet(i+1).build();
workBook.fill(repList.get(i), writeSheet);
// 如果有多个list 模板上必须有{前缀.} 这里的前缀就是 a,然后多个list必须用 FillWrapper包裹
//多组填充 体征打卡信息
workBook.fill(new FillWrapper("a", repList.get(i).getPhysicalSignPunch()), fillConfig, writeSheet);
//多组填充 病情记录
workBook.fill(new FillWrapper("b",repList.get(i).getPathography()), fillConfig,writeSheet);
}
}
// 千万别忘记关闭流
is.close();
if (workBook != null) {
workBook.finish();
}
} catch (Exception e) {
throw new BizException("导出问卷数据失败", e);
}
}
结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f87eab0407e22371e1e28a8e069ee3c8.png)