I. Usage scenarios
Generate multiple Excel files, pack and compress them, and export the compressed package.
2. Solution exploration
In the process of programming for Baidu (Google), I found that most of the solutions were to first build a temporary folder locally, put each Excel into this folder, package this folder after all the addition, and then delete this folder.
Seeing this solution, I feel this way is not very suitable! I thought there would be a more elegant way, after some searching, I found the solution shown in the code below, no need to create an intermediate file, just pack and compress, thank you very much to the blogger.
The original blog link javaWeb exports multiple Excel zip files created by POI.
Code 3.
@ResponseBody @RequestMapping(value = "/xxx") public BaseResp unDirectExport(Date date, HttpServletResponse response) throws Exception { String fileName = xxx; ResponseUtil.setMultipartHeader(response, fileName); List<T> list = xxxService.query(xx); // Grouping Collectors by a condition Map<String, List<T>> Map = list.stream().collect(Collectors. GroupingBy (T::getXXX)); // Grouping Collectors by a condition Map<String, List<T>> Map = list.stream().collect(Collectors. exportExcel(response, map); return BaseResp.succResp(); } private void exportExcel(HttpServletResponse response, Map<String, List<T>> map) throws IOException {//Excel sheet Header String[] headers = {""}; ServletOutputStream outputStream = response.getOutputStream(); ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream); HSSFWorkbook = createExcelAndSetHeaders(headers, k); // Continue to populate the sheet with object data setSheetCellValue(workbook.getSheet(k), v); ZipEntry = new ZipEntry(k + ".xls"); zipOutputStream.putNextEntry(zipEntry); } catch (IOException e) {logger.error(" Failed to add Excel to XXX package "); Throw new Exception(" Failed to add Excel to XXX package "); } try {// write a compressed workbook.write(zipOutputStream); } catch (IOException e) {logger.error(" Failed to write to zipOutputStream "); Throw new Exception(" Failed to write stream data to zipOutputStream "); }}); zipOutputStream.flush(); } catch (Exception e) {// Try catch finally to close logger.error(' cause XXX failed '+ lltterrorCode ()); Throw new Exception(" Failed to export XXX settlement data, cause: "+ db TerrorCode ()); } finally {// Close the data stream, note the closing order zipoutputstream.close (); outputStream.close(); } } private HSSFWorkbook createExcelAndSetHeaders(String[] headers, String sheetName) { HSSFWorkbook hssfWorkbook = new HSSFWorkbook(); HSSFSheet hssfSheet = hssfWorkbook.createSheet(sheetName); HSSFRow row0 = hssfSheet.createRow(0); for (int i = 0; i < headers.length; i++) { HSSFCell cellHeader = row0.createCell(i); cellHeader.setCellValue(headers[i]); } return hssfWorkbook; } private void setSheetCellValue(HSSFSheet hssfSheet, List<T> dtos) { for (T dto : HSSFRow row = hssfsheet.createrow (hssfsheet.getLastrowNum () + 1); dtos) {// Add a row after the last row of the current sheet and start filling data. int count = -1; row.createCell(++count).setCellValue(dto.getXXX); row.createCell(++count).setCellValue(dto.getXXX); row.createCell(++count).setCellValue(dto.getXXX); row.createCell(++count).setCellValue(dto.getXXX); row.createCell(++count).setCellValuedto.getXXX); }}Copy the code
import javax.servlet.http.HttpServletResponse; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; /** * @author zhaojq * @email [email protected] * @date 2019 2019/10/14 17:11 */ public class ResponseUtil { private ResponseUtil() { } public static void setMultipartHeader(HttpServletResponse response, String fileName) { try { response.setCharacterEncoding("utf-8"); response.setContentType("multipart/form-data"); response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + URLEncoder.encode(fileName, "UTF-8")); } the catch (UnsupportedEncodingException e) {throw new BusinessException (" file name code exception "); }}}Copy the code