概述
使用EasyExcel写入Excel后,将多个Excel打包为ZIP压缩包下载
代码
@GetMapping("/downloadToZip")
public void downloadToZip(HttpServletResponse response){
// 设置响应头
response.setContentType("application/zip");
response.setCharacterEncoding(StandardCharsets.UTF_8.toString());
String fileName = URLEncoder.encode("测试压缩包", StandardCharsets.UTF_8.toString());
response.setHeader("Content-Disposition", "attachment; filename="+fileName+".zip");
// 多个Excel
List<String> excelNameList = Arrays.asList("测试1", "测试2");
// 按照表格,循环处理导出数据,可优化为多线程处理
try (ZipOutputStream zos = new ZipOutputStream(response.getOutputStream())) {
for (int i = 0; i < excelNameList.size(); i++) {
String excelName = excelNameList.get(i);
ZipEntry entry = new ZipEntry(excelName + ".xlsx");
zos.putNextEntry(entry);
// 模板与数据
String excelModelFilePath = "/xxx/xxx/xxx/模板.xlsx";
Map<String, List> sheetMap = new HashMap<>();
sheetMap.put("Sheet1", new ArrayList<>());
sheetMap.put("Sheet2", new ArrayList<>());
// 写入Excel数据并获取写入后的Excel的byte[]
byte[] exportExcelByte = service.writeExcelData(excelModelFilePath, sheetMap);
zos.write(exportExcelByte);
}
}
}
writeExcelData()方法地址
Springboot使用EasyExcel 的填充模板导出,导出为多Sheet工作簿
标签:ZIP,Excel,EasyExcel,写入,sheetMap,new,response From: https://www.cnblogs.com/lucky-jun/p/18460143