excel按模板文件导出多个文件并压缩为ZIP格式返回前端

思路:先准备好模板文件和与之对应的实体类,数据库数据等,还是之前思路,根据查出的数据,填充模板文件,生成一个临时文件,最后将这些个临时文件打包为zip返回前端,并将多个临时文件删除掉就ok了

紧接着就是copy

java 复制代码
    @Log(title = "表格管理-批量导出", businessType = BusinessType.EXPORT)
    @Operation(description = "下载表格")
    @PostMapping("/imports")
    public void excelimport(HttpServletResponse response, @RequestBody List<FormVo> formVo) throws IOException {
        response.setContentType("application/zip");
        response.setCharacterEncoding("UTF-8");
        String fileName = "/home/ag/project/files/templates/" + System.currentTimeMillis() + ".zip";
        response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(fileName, "UTF-8"));
        FileOutputStream fos = new FileOutputStream(fileName);
        ZipOutputStream zos = new ZipOutputStream(fos);
        ExcelWriter writer = null;
        ArrayList<String> o = new ArrayList<>();
        for (FormVo f : formVo) {
            // 模板文件路径   switch case部分是我的业务代码(多个不同的模板文件)  各位就不用看了,删掉即可
            String templateFilePath;
            String type = f.getType();
            switch (type) {
                case "1":
                    templateFilePath = form;
                    break;
                case "2":
                    templateFilePath = formCao;
                    break;
                case "3":
                    templateFilePath = formLin;
                    break;
                case "4":
                    templateFilePath = formYuan;
                    break;
                case "5":
                    templateFilePath = formShui;
                    break;
                case "6":
                    templateFilePath = formJian;
                    break;
                case "7":
                    templateFilePath = formQi;
                    break;
                default:
                    throw new RuntimeException("表格类型有误");
            }
            // 创建 ExcelWriter 实例
            String fileName1 = "/home/ag/project/files/templates/" + System.currentTimeMillis() + "听忆计划表.xlsx";
            writer = EasyExcel
                    // 写入到临时文件
                    .write(fileName1)
                    // 指定模板
                    .withTemplate(templateFilePath)
                    .build();

            WriteSheet sheet = EasyExcel.writerSheet().build();

            FillConfig fillConfig = FillConfig.builder()
                    // 开启填充换行
                    .forceNewRow(true)
                    .build();
            List list = getjituan1(f, f.getType());
            //计算公式
            Workbook workbook = writer.writeContext().writeWorkbookHolder().getWorkbook();
            workbook.setForceFormulaRecalculation(true);
            // 执行填充操作
            writer.fill(list, fillConfig, sheet);
            o.add(fileName1);
//            // 压缩文件
            writer.finish();
        }
        for (String f:o){
            // 压缩文件
            File file1 = new File(f);
            addFileToZip(file1, zos);
//            删除临时文件
            deleteFile(f);
        }

        zos.close();
        //将压缩文件输入流传给response输出流
        InputStream fileInputStream = new FileInputStream(fileName);
        OutputStream outputStream = response.getOutputStream();
        byte[] bytes = new byte[1024 * 8];
        int len;
        while ((len = fileInputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, len);
        }
        outputStream.close();
        fileInputStream.close();
    }

    static void deleteFile(String path) {
        File file = new File(path);
        if (file.exists()) {
            file.delete();
        }
    }

    public static void addFileToZip(File file, ZipOutputStream zos) throws IOException {
        InputStream fileInputStream = new FileInputStream(file);
        //zip中要放文件称为zipEntry
        ZipEntry zipEntry = new ZipEntry(file.getName());
        zos.putNextEntry(zipEntry);
        byte[] bytes = new byte[1024];
        int len;
        while ((len = fileInputStream.read(bytes)) > 0) {
            //读的内容会自动放到zip条目中,因此zipentry再输出流读完需要关闭
            zos.write(bytes, 0, len);
        }
        fileInputStream.close();
        zos.closeEntry();
    }
相关推荐
a程序小傲1 分钟前
国家电网Java面试被问:API网关的JWT令牌验证和OAuth2.0授权码流程
java·开发语言·spring boot·后端·面试·职场和发展·word
tqs_123451 分钟前
单例模式代码
java·开发语言·单例模式
Marshmallowc1 分钟前
React stopPropagation 阻止冒泡失效?深度解析 React 17 事件委派机制变更与微前端冲突解决方案
前端·react.js·事件循环·微前端·前端架构
xiaohutushen4 分钟前
紧急预警:微软 Edge Webview2 v144 升级导致 SAP GUI 严重白屏故障 (Note 3704912)
前端·microsoft·edge·abap·sap 用户·sap license·usmm
2501_944526424 分钟前
Flutter for OpenHarmony 万能游戏库App实战 - 多语言国际化实现
android·java·开发语言·javascript·flutter·游戏
CHU7290357 分钟前
淘宝扭蛋机小程序前端功能详解:以交互设计赋能趣味体验
java·前端·小程序·php
SunnyDays10117 分钟前
Java 实战:高效合并多个 Word 文档
java·合并word文档
ccino .11 分钟前
【Portswigger : DOM XSS in jQuery selector sink using a hashchange event】
前端·jquery·xss
靠沿11 分钟前
Java数据结构初阶——七大排序算法及“非比较”排序
java·数据结构·排序算法
滴水未满12 分钟前
uniapp的工程
前端·uni-app