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);
        }
    }
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();
                }
            }
        }
        
    }
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
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
您可以使用JavaZipOutputStream类来实现将多个Excel文件打包成一个ZIP文件。以下是一个基本示例: ```java import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class ExcelZipper { public static void main(String[] args) { // Source directory containing the Excel files String sourceDir = "/path/to/excel/files/"; // Destination directory for the ZIP file String destDir = "/path/to/zip/output/"; // Name of the ZIP file String zipName = "excel_files.zip"; try { // Create a FileOutputStream for the ZIP file FileOutputStream fos = new FileOutputStream(destDir + zipName); // Create a ZipOutputStream for the FileOutputStream ZipOutputStream zos = new ZipOutputStream(fos); // Get a list of all the Excel files in the source directory File[] excelFiles = new File(sourceDir).listFiles((dir, name) -> name.endsWith(".xlsx") || name.endsWith(".xls")); // Loop through each Excel file and add it to the ZIP file for (File file : excelFiles) { // Create a new ZipEntry for the file ZipEntry zipEntry = new ZipEntry(file.getName()); // Add the ZipEntry to the ZipOutputStream zos.putNextEntry(zipEntry); // Create a FileInputStream for the Excel file FileInputStream fis = new FileInputStream(file); // Read the Excel file and write it to the ZipOutputStream byte[] buffer = new byte[1024]; int length; while ((length = fis.read(buffer)) > 0) { zos.write(buffer, 0, length); } // Close the FileInputStream fis.close(); // Close the ZipEntry zos.closeEntry(); } // Close the ZipOutputStream zos.close(); System.out.println("Excel files have been zipped successfully!"); } catch (Exception ex) { ex.printStackTrace(); System.out.println("Error zipping Excel files: " + ex.getMessage()); } } } ``` 您需要将“/path/to/excel/files/”替换为包含Excel文件的实际源目录,“/path/to/zip/output/”替换为ZIP文件的实际目标目录,并将“excel_files.zip”替换为所需的ZIP文件名称。 此代码将创建一个ZipOutputStream并将Excel文件添加到其ZipOutputStream将自动将Excel文件压缩为ZIP文件。一旦所有文件都添加到ZipOutputStream,就可以关闭它并将所有Excel文件打包到单个ZIP文件。 请注意,此代码仅适用于XLSX和XLS格式的Excel文件。如果您需要支持其他格式,请相应地修改文件过滤器。 希望这可以帮助您实现所需的功能。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值