EasyExcel导出多个文件并直接打包成zip下载
String zipName = "xxx.zip";
try {
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(zipName, "UTF-8"));
response.setContentType("application/vnd.ms-excel");
// 准备zip输出流
ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
// 循环导出多个xlsx文件
for (...) {
// 准备zip对象
ZipEntry entry = new ZipEntry("*文件名*" + ".xlsx");
zos.putNextEntry(entry);
// 准备填充模板的对象
ExcelWriter build = EasyExcel.write(byteArrayOutputStream)
.withTemplate(new ClassPathResource("*模板路径*").getInputStream()).build();
// 修改换行规则为每填充一行后插入一个空行
FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
WriteSheet sheet0 = EasyExcel.writerSheet(0).build();
build.fill("*数据集合1*", sheet0);
build.fill("*数据集合2*", fillConfig, sheet0);
...
// 单个文件完成
build.finish();
// 添加到zip输出流中
zos.write(byteArrayOutputStream.toByteArray());
byteArrayOutputStream.reset();
}
zos.flush();
zos.close();
} catch (Exception e) {
e.printStackTrace();
}
标签:zip,EasyExcel,导出,zos,build,byteArrayOutputStream,new From: https://www.cnblogs.com/yoojyn/p/yoojyn_easyexcel02.html