poi导出文件到本地并打成zip包

/**
 * @author 李银宽
 * <p>描述: poi导出文件并打包
 * <p>日期: 2023/10/19 上午 10:05
 */
public static void exportExcel(HttpServletResponse response, String fileName, List<String> columnList, List<List<String>> dataList)throws Exception{
    //声明输出流
    OutputStream os = new FileOutputStream(fileName+".xlsx");
    //设置响应头,浏览器直接返回需要用到这个

// setResponseHeader(response,fileName);
try {
//获取输出流,浏览器直接返回需要用到这个
// os= response.getOutputStream();
// 1.创建一个工作簿。03
Workbook wb = new XSSFWorkbook();
// 2.创建一个工作表
Sheet sheet1 = wb.createSheet(“文件时效性统计”);

        int excelRow = 0;
        //创建标题行
        Row titleRow = sheet1.createRow(excelRow++);
        for(int i = 0;i<columnList.size();i++){
            //创建该行下的每一列,并写入标题数据
            Cell cell = titleRow.createCell(i);
            cell.setCellValue(columnList.get(i));
        }
        //设置内容行
        if (dataList != null && dataList.size() > 0) {
            //外层for循环创建行
            for (int i = 0; i < dataList.size(); i++) {
                Row dataRow = sheet1.createRow(excelRow++);
                //内层for循环创建每行对应的列,并赋值
                for (int j = 0; j < dataList.get(0).size(); j++) {
                    Cell cell = dataRow.createCell(j);
                    cell.setCellValue(dataList.get(i).get(j));
                }
            }
        }
        //将.xlsx保存到本地
        wb.write(new FileOutputStream(fileName+".xlsx"));

        //将生成的文件打包成zip
        String[] files = {fileName+".xlsx"};

        try {
            FileOutputStream fos = new FileOutputStream(fileName+".zip");
            ZipOutputStream zipOut = new ZipOutputStream(fos);
            for (String file : files) {
                File fileToZip = new File(file);

                if (fileToZip.isFile()) {
                    addToZip(fileToZip, fileToZip.getName(), zipOut);
                } else if (fileToZip.isDirectory()) {
                    addFolderToZip(fileToZip, fileToZip.getName(), zipOut);
                }
            }
            zipOut.close();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            // 关闭输出流
            if (os != null) {
                os.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

/*
    设置浏览器下载响应头
 */
private static void setResponseHeader(HttpServletResponse response, String fileName) {
    try {
        try {
            fileName = new String(fileName.getBytes(),"ISO8859-1");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setContentType("application/octet-stream;charset=UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
        response.addHeader("Pargam", "no-cache");
        response.addHeader("Cache-Control", "no-cache");
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}


//将文件打包成zip
private static void addToZip(File fileToZip, String fileName, ZipOutputStream zipOut) throws IOException {
    FileInputStream fis = new FileInputStream(fileToZip);
    ZipEntry zipEntry = new ZipEntry(fileName);
    zipOut.putNextEntry(zipEntry);

    byte[] bytes = new byte[1024];
    int length;
    while ((length = fis.read(bytes)) >= 0) {
        zipOut.write(bytes, 0, length);
    }

    fis.close();
}

//将文件夹打包成zip
private static void addFolderToZip(File folderToZip, String folderName, ZipOutputStream zipOut) throws IOException {
    File[] files = folderToZip.listFiles();
    if (files != null) {
        for (File file : files) {
            if (file.isFile()) {
                addToZip(file, folderName + File.separator + file.getName(), zipOut);
            } else if (file.isDirectory()) {
                addFolderToZip(file, folderName + File.separator + file.getName(), zipOut);
            }
        }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值