导出多个Excel表生成多级目录压缩包——ZipOutputStream+easypoi


easypoi——Excel表系列

easypoi的基本用法
easypoi自定义样式
本篇文章是easypoi+ZipOutputStream。

ZipOutputStream简介

ZipOutputStream是一种压缩流,是Java用来生成zip压缩包的应该类。下面熟悉一下主要内容:
1、new ZipOutputStream(java.io.OutputStream));创建压缩流
2、new ZipEntry(String);zip包下的目录词条,创建zip实体
3、zipOutputStream.putNextEntry(entry);将实体推入压缩流里

工具类(核心)

使用了easypoi框架,简化创建工作上一篇博客有介绍和使用方法
流程:
1、创建工作簿
2、将工作簿放入压缩流
3、导出压缩包

    /**
     * 多个Excel表压入压缩包导出
     */
     /**
     * 创建Excel表的工作簿Workbook
     * @param list 数据集合
     * @param title 标题
     * @param sheetName 页名
     * @param entity 实体类
     * @return
     */
    public static Workbook Excel(List<?> list, String title, String sheetName, Class<?> entity) {
        ExportParams exportParams = new ExportParams(title, sheetName);
        //添加样式
        exportParams.setStyle(ExcelExportStylerUtil.class);
        //冻结表头
        exportParams.setCreateHeadRows(true);
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, entity, list);
        return workbook;
    }
    
        /**
     * 工作簿集合导出压缩包
     * @param response
     * @param workbooks  工作簿集合
     * @param fileName   压缩包名
     * @param exelNames  excel表名
     * @param parentName 多级目录名
     */
    public static void exportZip(HttpServletResponse response,List<Workbook> workbooks,String fileName,List<String> exelNames,List<String> parentName){
        try {
            // 指定下载的文件名--设置响应头
            response.setContentType("application/zip; charset=UTF-8");
            response.setDateHeader("Expires", 0);
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".zip");
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setCharacterEncoding("UTF-8");

            // 多个Excel写入压缩文件
            ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream());
            for (int i = 0; i < workbooks.size(); i++) {
                ZipEntry entry =null;
                if (CollectionUtil.isEmpty(parentName)) {
                	//若想在zip包生成,一级目录\二级目录\excel表名.xlsx文件
                	//new ZipEntry("一级目录\二级目录\excel表名.xlsx");
                    entry = new ZipEntry(exelNames.get(i)+".xlsx");
                }else {
                    entry = new ZipEntry(parentName.get(i)+exelNames.get(i)+".xlsx");
                }
                zipOutputStream.putNextEntry(entry);
                ByteOutputStream byteOutputStream = new ByteOutputStream();
                workbooks.get(i).write(byteOutputStream);
                byteOutputStream.writeTo(zipOutputStream);
                byteOutputStream.close();
            }
            zipOutputStream.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

service层伪代码

1、获取数据集合vos、标题title、页名sheetName
2、获取工作簿集合
Workbook workbook(ExcelUtil.Excel(vos,title,sheetName,Vo.class))
3、生成压缩包
ExcelUtil.exportZip(response,workbooks,zipName,excelNames, parentName);
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,对于Java导出多个Excel生成多级目录压缩包的需求,您可以按照以下步骤实现: 1. 首先,您需要使用Java操作Excel格,这里推荐使用Apache POI库。您可以使用POI库读取Excel格中的数据,也可以使用POI库创建新的Excel格。 2. 接下来,您需要将生成Excel格按照要求组织成多级目录结构。您可以使用Java的File类来创建文件夹和文件,将生成Excel格存放在对应的文件夹中。 3. 最后,您需要使用JavaZipOutputStream类将生成的文件夹和文件压缩成一个多级目录压缩包ZipOutputStream类可以将多个文件或文件夹压缩成一个压缩包,同时可以设置压缩包的名称和路径。 以下是一个示例代码,用于将两个Excel导出生成多级目录压缩包: ```java import java.io.*; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.*; public class ExcelExport { public static void main(String[] args) { // 创建两个Excel格 Workbook workbook1 = new HSSFWorkbook(); Sheet sheet1 = workbook1.createSheet("Sheet1"); Row row1 = sheet1.createRow(0); Cell cell1 = row1.createCell(0); cell1.setCellValue("Hello"); Workbook workbook2 = new HSSFWorkbook(); Sheet sheet2 = workbook2.createSheet("Sheet2"); Row row2 = sheet2.createRow(0); Cell cell2 = row2.createCell(0); cell2.setCellValue("World"); // 创建目录结构 File dir = new File("export"); dir.mkdir(); File subDir1 = new File(dir, "subdir1"); subDir1.mkdir(); File subDir2 = new File(subDir1, "subdir2"); subDir2.mkdir(); // 保存Excel格到文件夹中 try { FileOutputStream fileOut1 = new FileOutputStream(new File(subDir2, "file1.xls")); workbook1.write(fileOut1); fileOut1.close(); FileOutputStream fileOut2 = new FileOutputStream(new File(subDir2, "file2.xls")); workbook2.write(fileOut2); fileOut2.close(); } catch (IOException e) { e.printStackTrace(); } // 压缩文件夹为多级目录压缩包 String zipFileName = "export.zip"; try { FileOutputStream fileOut = new FileOutputStream(zipFileName); ZipOutputStream zipOut = new ZipOutputStream(fileOut); addToZip(subDir1, "", zipOut); zipOut.close(); fileOut.close(); } catch (IOException e) { e.printStackTrace(); } } private static void addToZip(File file, String parentDir, ZipOutputStream zipOut) throws IOException { String filePath = parentDir + file.getName(); if (file.isDirectory()) { if (!filePath.isEmpty()) { ZipEntry zipEntry = new ZipEntry(filePath + "/"); zipOut.putNextEntry(zipEntry); zipOut.closeEntry(); } for (File subFile : file.listFiles()) { addToZip(subFile, filePath + "/", zipOut); } } else { FileInputStream fileIn = new FileInputStream(file); ZipEntry zipEntry = new ZipEntry(filePath); zipOut.putNextEntry(zipEntry); byte[] buf = new byte[1024]; int len; while ((len = fileIn.read(buf)) > 0) { zipOut.write(buf, 0, len); } fileIn.close(); zipOut.closeEntry(); } } } ``` 在这个示例代码中,我们创建了两个Excel格,然后将它们保存在一个名为"export"的文件夹中,同时按照要求组织了多级目录结构。最后,我们使用ZipOutputStream类将"export"文件夹压缩成一个名为"export.zip"的多级目录压缩包
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值