一、java导出多个excel压缩成zip包
1.添加依赖
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-logging</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
2.controller
/**
* 导出excel的zip模板
*
* @param ids 课程ids
* @param request request
* @param response response
* @throws IOException io异常
*/
@GetMapping("exportExcel/{ids}")
public void exportExcel(@PathVariable @NotBlank(message = "id不能为空") String ids, HttpServletRequest request, HttpServletResponse response) throws IOException {
ExportParams exportParams = new ExportParams();
exportParams.setDictHandler(new GlobalExcelDictHandler());
List<Long> idList = Arrays.stream(ids.split(",")).map(Long::valueOf).collect(Collectors.toList());
List<EquipmentProduct> equipmentProducts = equipmentProductDao.selectBatchIds(idList);
List<EquipmentImportExcel> equipmentImportExcels = new ArrayList<>(1);
EquipmentImportExcel equipmentImportExcel = new EquipmentImportExcel();
equipmentImportExcels.add(equipmentImportExcel);
List<InputStream> inputStreams = new ArrayList<>();
List<String> paths = new ArrayList<>();
for (EquipmentProduct key : equipmentProducts) {
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, EquipmentImportExcel.class, equipmentImportExcels);
FileOutputStream fos = new FileOutputStream(path + "/ProductKey_" + key.getProductKey() + ".xlsx");
workbook.write(fos);
fos.close();
inputStreams.add(new FileInputStream(path + "/ProductKey_" + key.getProductKey() + ".xlsx"));
paths.add("/ProductKey_" + key.getProductKey() + ".xlsx");
}
String userAgent = request.getHeader("User-Agent");
response.setContentType("application/octet-stream;charset=utf-8");
String fileName = "设备导入模板";
try {
if (userAgent.contains("MSIE") || userAgent.contains("Trident") || userAgent.contains("Edge")) {
fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
} else {
//非IE浏览器的处理:
fileName = new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
}
} catch (Exception e) {
e.printStackTrace();
}
response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".zip");
ZipUtil.zip(response.getOutputStream(), paths.toArray(new String[paths.size()]), inputStreams.toArray(new InputStream[inputStreams.size()]));
for (EquipmentProduct key : equipmentProducts) {
File file = new File(path + "/ProductKey_" + key.getProductKey() + ".xlsx");
if (file.exists()) {
file.delete();
}
}
response.getOutputStream().flush();
response.getOutputStream().close();
}
二、java导出多个json文件压缩成zip包
/**
* 导出json的zip模板
*
* @param ids 课程ids
* @param request request
* @param response response
* @throws IOException io异常
*/
@GetMapping("exportJson/{ids}")
public void exportJson(@PathVariable @NotBlank(message = "id不能为空") String ids, HttpServletRequest request, HttpServletResponse response) throws IOException {
List<Long> idList = Arrays.stream(ids.split(",")).map(Long::valueOf).collect(Collectors.toList());
List<EquipmentImportJson> equipmentImportJsons = new ArrayList<>(1);
EquipmentImportJson equipmentImportExcel = new EquipmentImportJson();
equipmentImportJsons.add(equipmentImportExcel);
List<EquipmentProduct> equipmentProducts = equipmentProductDao.selectBatchIds(idList);
List<InputStream> inputStreams = new ArrayList<>();
List<String> paths = new ArrayList<>();
for (EquipmentProduct key : equipmentProducts) {
try {
String fileName = "ProductKey_"+key.getProductKey()+".json";
//JsonUtil.exportJson(response, equipmentImportExcels, "/ProductKey_" + key.getProductKey() + ".json");
String jsonString = JSON.toJSONString(equipmentImportJsons,
SerializerFeature.PrettyFormat,
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteDateUseDateFormat);
String fullPath = "/" + fileName;
File file = new File(fullPath);
Writer write = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8);
write.write(jsonString);
write.flush();
write.close();
FileInputStream fis = new FileInputStream(file);
OutputStream os = new FileOutputStream(path + "/ProductKey_" + key.getProductKey() + ".json");
byte[] buf = new byte[1024];
int len = 0;
while ((len = fis.read(buf)) != -1) {
os.write(buf, 0, len);
}
fis.close();
os.close();
inputStreams.add(new FileInputStream(path + "/ProductKey_" + key.getProductKey() + ".json"));
paths.add("ProductKey_" + key.getProductKey() + ".json");
} catch (Exception e) {
e.printStackTrace();
}
}
String userAgent = request.getHeader("User-Agent");
response.setContentType("application/octet-stream;charset=utf-8");
String fileName = "设备导入模板";
try {
if (userAgent.contains("MSIE") || userAgent.contains("Trident") || userAgent.contains("Edge")) {
fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
} else {
//非IE浏览器的处理:
fileName = new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
}
} catch (Exception e) {
e.printStackTrace();
}
response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".zip");
ZipUtil.zip(response.getOutputStream(), paths.toArray(new String[paths.size()]), inputStreams.toArray(new InputStream[inputStreams.size()]));
for (EquipmentProduct key : equipmentProducts) {
File file = new File(path + "/ProductKey_" + key.getProductKey() + ".json");
if (file.exists()) {
file.delete();
}
}
response.getOutputStream().flush();
response.getOutputStream().close();
}