SpringBoot dynamically generates multiple Excel files to download in compressed package .zip format

foreword

There are many scenarios for file download (not that the demand is rich~), so what is dynamically generated file, that is, the user selects the content of the file, the server generates a file in a certain format according to the selected data, and then downloads it to the client. Download a single code directly, and download multiple packages!

Project scene

There is a need to export data to excel in the project, and it is in batches, so it needs to be packaged.

Solution steps

The basic implementation is as follows:

  1. makefile
  2. save to temp directory || cache in memory
  3. Download the file as a package

I don't want to talk nonsense, here is the code:

Put the file output stream directly into the compressed stream ZipOutputStream

 List<String> fields = new ArrayList<>();
        fields.add("字段1");
        fields.add("字段2");
        fields.add("字段3");
        HSSFWorkbook workbook = excelUtil.getNewExcel("压缩文件测试", fields);
        try {
            response.setContentType("application/zip; charset=UTF-8");
            //返回客户端浏览器的版本号、类型
            String agent = request.getHeader("USER-AGENT");
            String downloadName = "压缩文件测试.zip";
            //针对IE或者以IE为内核的浏览器:
            if (agent.contains("MSIE") || agent.contains("Trident")) {
                downloadName = java.net.URLEncoder.encode(downloadName, "UTF-8");
            } else {
                downloadName = new String(downloadName.getBytes("UTF-8"), "ISO-8859-1");
            }
            response.setHeader("Content-disposition", "attachment;filename=" + downloadName);
   
            ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream());
   //多个从这里就可遍历了
   // --start
            ZipEntry entry = new ZipEntry("第一个文件名.xls");
            zipOutputStream.putNextEntry(entry);

            ByteOutputStream byteOutputStream = new ByteOutputStream();
            workbook.write(byteOutputStream);
            byteOutputStream.writeTo(zipOutputStream);
//            zipOutputStream.write(workbook.getBytes());
            byteOutputStream.close();
            zipOutputStream.closeEntry();
            // --end
            zipOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

Generate the excel file and paste it as follows:

public static HSSFWorkbook getNewExcel(String formName, List<String> fields) {
        //新建excel对象
        HSSFWorkbook workbook = new HSSFWorkbook();
        //新建工作表
        HSSFSheet sheet = workbook.createSheet(formName);
        // 设置表格默认列宽度为20个字节
        sheet.setDefaultColumnWidth((short) 20);
        // 生成一个样式
        HSSFCellStyle style = workbook.createCellStyle();
        // 生成一个字体
        HSSFFont font = workbook.createFont();
        font.setFontHeightInPoints((short) 12);
        //字体应用到当前样式
        style.setFont(font);
        //创建表格行
        HSSFRow row = sheet.createRow(0);
        //设置表单名称
        row.createCell(0).setCellValue(formName);
        if (fields.size() - 1 > 0) {
            //合并单元格
            sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, fields.size() - 1));
        }
        HSSFRow row1 = sheet.createRow(sheet.getLastRowNum() + 1);
        //设置列标题行
        for (int i = 0; i < fields.size(); i++) {
            row1.createCell(i).setCellValue(fields.get(i));
        }
        return workbook;
    }

Some practical summary, welcome to correct!

Guess you like

Origin blog.csdn.net/m0_63437643/article/details/123733570