当前位置:   article > 正文

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

zipoutputstream 多层


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();
        }
    }
  • 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
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62

service层伪代码

1、获取数据集合vos、标题title、页名sheetName
2、获取工作簿集合
Workbook workbook(ExcelUtil.Excel(vos,title,sheetName,Vo.class))
3、生成压缩包
ExcelUtil.exportZip(response,workbooks,zipName,excelNames, parentName);
  • 1
  • 2
  • 3
  • 4
  • 5
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/216016
推荐阅读
相关标签
  

闽ICP备14008679号