多个easyexcel合并压缩下载

@GetMapping(value = "/export/orderTemplateZip")
    @Operation(summary = "下载需求导入模板zip")
    public void exportTemplateZip(HttpServletResponse response) throws IOException {
        String zipFileName = URLEncoder.encode("需求导入模板", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + zipFileName + ".zip");
        response.setContentType("application/x-msdownload");
        response.setCharacterEncoding("utf-8");
        try (ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream());
             ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {

            zipOut.putNextEntry(new ZipEntry("****需求导入表.xlsx"));
                //开始写入excel
            ArrayList<OrderTemplateZipBO.MTCOExcelTemplate> mtcoExcelTemplates = new ArrayList<>(1);
            OrderTemplateZipBO.MTCOExcelTemplate mtcoExcelTemplate = new OrderTemplateZipBO.MTCOExcelTemplate();
            mtcoExcelTemplate.setMtco("G5ZC2D00001");
            mtcoExcelTemplate.setAmount(900);
            mtcoExcelTemplate.setRemarks("");
            mtcoExcelTemplates.add(mtcoExcelTemplate);
            EasyExcel.write(outputStream, OrderTemplateZipBO.MTCOExcelTemplate.class) .autoCloseStream(false)
                    // 基于 column 长度,自动适配。最大 255 宽度
                    .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())//可以省略
                    .sheet("**需求导入表").doWrite(mtcoExcelTemplates);
            outputStream.writeTo(zipOut);
            zipOut.closeEntry();
            outputStream.reset();//不重置无法打开后面的excel

            zipOut.putNextEntry(new ZipEntry("****需求导入表.xlsx"));
            ArrayList<OrderTemplateZipBO.APPExcelTemplate> APPExcelTemplates = new ArrayList<>(1);
            OrderTemplateZipBO.APPExcelTemplate APPExcelTemplate = new OrderTemplateZipBO.APPExcelTemplate();
            APPExcelTemplate.setApp("******");
            APPExcelTemplate.setCompact("******");
            APPExcelTemplate.setRemarks("大区报备");
            APPExcelTemplates.add(APPExcelTemplate);
            EasyExcel.write(outputStream, OrderTemplateZipBO.APPExcelTemplate.class) .autoCloseStream(false)
                    // 基于 column 长度,自动适配。最大 255 宽度
                    .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                    .sheet("APP需求导入表").doWrite(APPExcelTemplates);
            outputStream.writeTo(zipOut);
            zipOut.closeEntry();

        } catch (Exception e) {
            // 重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.getWriter().println("下载文件失败" + e.getMessage());
        }


    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值