• Ruoyi 从数据库中导出多个excel打包为zip


    1.需求描述

    将MySQL多个表转excel, 打包下载为zip.

    2. 修改ExcelUtil.java

    新增如下方法:将Workbook写入字节流ByteOutputStream

    public void byteOutputStreamExcel(ByteOutputStream byteOutputStream, List<T> list, String sheetName, String title) {
            this.init(list, sheetName, title);
            try {
                writeSheet();
                wb.write(byteOutputStream);
            } catch (Exception e) {
                log.error("导出Excel异常{}", e.getMessage());
            } finally {
                IOUtils.closeQuietly(wb);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    3. Service层方法

    没什么好说的,就是这么写, 当作模板使用,自己优化.

        public void exportZip(HttpServletResponse response, int[] ids) {
            String fileName = "Mapping_Tables.zip";
            response.setContentType("application/zip");
            response.setHeader("content-disposition", "attachment;filename=" + fileName);
            ZipOutputStream zos = null;
            try {
                zos = new ZipOutputStream(response.getOutputStream());
                for (int tableId : ids) {
                    IMappingTableBaseService service = mappingTableFactory.getMappingTableService(tableId);
                    MappingTable mappingTable = this.selectMappingTableById(tableId);
                    List list = service.list(null);
                    ExcelUtil util = service.getExcelUtil();
                    ZipEntry entry = new ZipEntry(mappingTable.getDescription() + ".xls");
                    zos.putNextEntry(entry);
                    ByteOutputStream bos = new ByteOutputStream();
                    util.byteOutputStreamExcel(bos, list,"Date List", "");
                    bos.writeTo(zos);
                    bos.close();
                    zos.closeEntry();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            } finally {
                if(zos != null){
                    try{
                        zos.close();
                    } catch(Exception e){
                        e.printStackTrace();
                    }
                }
            }
            
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    4. Controller方法

    根据请求判断是要下载多个文件还是单个文件. 如果是单个文件则无需打包.

        @PostMapping("/export")
        public void export(HttpServletResponse response, @RequestBody int[] ids) {
            if (ids != null) {
                if (ids.length == 1) {
                    IMappingTableBaseService service = mappingTableFactory.getMappingTableService(ids[0]);
                    MappingTable mappingTable = mappingTableService.selectMappingTableById(ids[0]);
                    List list = service.list(null);
                    ExcelUtil util = service.getExcelUtil();
                    util.exportExcel(response, list, "Date List", mappingTable.getDescription() + ".xls");
                } else {
                    mappingTableService.exportZip(response, ids);
                }
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    结束~

  • 相关阅读:
    第九章《搞懂算法:决策树是怎么回事》笔记
    弄清数据库索引的来龙去脉
    在springboot项目中显示Services面板的方法
    Mongodb的分页优化及索引使用
    在Windows 10上安装单机版的hadoop-3.3.5
    Linux上C++通过LDAP协议使用kerberos认证AES加密连接到AD服务器
    【zabbix监控四】zabbix之监控tomcat服务报警
    智能abc是什么输入法:win10可用的智能abc输入法免费下载
    【JavaScript-事件①】事件三要素,事件处理程序
    Tensorflow2.x版本initializer正态化变量输出函数
  • 原文地址:https://blog.csdn.net/lk1985021/article/details/128106170