多个Excel打包成zip导出

先实现IO工具类

package per.xgt.IoUtil;

import java.io.InputStream;
import java.io.OutputStream;

/**
 * @Author: Valen
 * @Email:ValentinoXiong@163.com
 * @CreateTime: 2020-12-14 10:29:15
 * @Descirption:
 */
public class IoUtil {

    public static long copy(InputStream in, OutputStream out) throws Exception{
        return copy((InputStream)in,(OutputStream)out,1024);
    }

    public static long copy(InputStream in,OutputStream out,int bufferSize) throws Exception {
        return copy((InputStream) in,(OutputStream) out,bufferSize,0);
    }

    public static long copy(InputStream in,OutputStream out,int bufferSize,int flag){
        if (bufferSize <= 0){
            bufferSize = 1024;
        }
        byte[] buffer = new byte[bufferSize];
        long size = 0L;
        try {
            boolean var7 = true;
            int readSize;
            while ((readSize = in.read(buffer)) != -1){
                out.write(buffer,0,readSize);
                size += (long)readSize;
                out.flush();
            }
        } catch (Exception e){
            System.out.println(e.getMessage());
        }
        return size;
    }

}

再实现工具类ResponseUtil

package per.xgt.ReponseUtil;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import per.xgt.IoUtil.IoUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @Author: Valen
 * @Email:ValentinoXiong@163.com
 * @CreateTime: 2020-12-11 17:57:02
 * @Descirption:
 */
public class ResponseUtil {

    //response文件下载设置
    public static void ResponseSetForFile(HttpServletResponse response,String fileName){
        //文件名编码设置,防止中文乱码
        try {
            fileName = URLEncoder.encode(fileName,"UTF-8");
        } catch (Exception e){
            //抛出异常
            System.out.println(e.getMessage());
        }
        //设置文件类型,编码和文件名
        response.setHeader("content-disposition", "attachment;filename="+fileName);
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/octet-stream");
    }

	//将多个Excel打包写出
    private void exportZip(List<Workbook> workbooks,String fileName,HttpServletResponse response){
        try (ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream())){
            //excel文件循环添加到zipOut中
            ZipEntry entry;
            ByteArrayOutputStream byteOut;
            ByteArrayInputStream byteIn;
            int index =1;
            for (Workbook workbook : workbooks){
                //设置文件名字
                entry = new ZipEntry(fileName+"_"+(index++)+".xlsx");
                zipOut.putNextEntry(entry);
                //workbook写入到byteOut(内润流中),以你为workBook.write(zipOut)会将ZipOut关闭,下次调用时报ZipOut已关闭
                byteOut = new ByteArrayOutputStream();
                workbook.write(byteOut);
                byteIn = new ByteArrayInputStream(byteOut.toByteArray());
                //将文件写入到zipOut中
                IoUtil.copy(byteIn, byteOut);
            }
            //刷新zipOut缓存
            zipOut.flush();
        } catch (Exception e){
            System.out.println(e.getMessage());
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值