java 导出工具类 ExcelPoiUtil

java 代码

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

/**
 * @ClassName ExcelPoiUtil
 * @Description
 * @Author yangyao
 * @Date 2022/9/11
 **/
public class ExcelPoiUtil {

    /**
     * 生成 stream流 到前端浏览器
     * @param filePath 存储路径 ,如:data\DocumentCenter\WorkDocument\digitalTooling\1832a49d4de.xlsx
     **/
    public static void downStream(String filePath,HttpServletResponse response){
        InputStream in = null;
        ServletOutputStream outputStream = null;
        response.reset();
        response.setContentType("application/ostet-stream");
        try {
            in = new FileInputStream(filePath);
            outputStream = response.getOutputStream();
            //创建存放文件内容的数组
            byte[] buff =new byte[1024];
            //所读取的内容使用n来接收
            int n;
            //当没有读取完时,继续读取,循环
            while((n=in.read(buff))!=-1){
                //将字节数组的数据全部写入到输出流中
                outputStream.write(buff,0,n);
            }
            //强制将缓存区的数据进行输出
            outputStream.println();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            CloseableUtils.close(outputStream, in);
        }
    }

    /**
     * 生成 stream流 到前端浏览器
     * @param filePath 存储路径 ,如:data\DocumentCenter\WorkDocument\digitalTooling\1832a49d4de.xlsx
     **/
    public static void downStream2(String filePath,HttpServletResponse response){
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            File file = new File(filePath);
            ExceptionVerifyUtil.verify(!file.exists(), ApiCode.BUSINESS_EXCEPTION,"附件路径不存在");

            inputStream = new FileInputStream(file);
            outputStream = new BufferedOutputStream(response.getOutputStream());
            byte[] bytes = new byte[1024];
            int len;
            while ((len = inputStream.read(bytes)) != -1){
                response.getOutputStream().write(bytes,0,len);
            }
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            CloseableUtils.close(outputStream, inputStream);
        }
    }


    /**
     * 生成 excel 到前端浏览器
     * @param filePath 存储路径 ,如:data\DocumentCenter\WorkDocument\digitalTooling\1832a49d4de.xlsx
     * @param fileName 导出的文件名字 1832a49d4de.xlsx
     **/
    public static void downExcel(String filePath,String fileName,HttpServletResponse response){
        FileInputStream in;
        try {
            in = new FileInputStream(filePath);
        } catch (FileNotFoundException e) {
            throw new BusinessException("file does not exist.");
        }
        downExcel2(in,fileName, response);
    }

    /**
     * 生成 excel 到前端浏览器
     * @param in FileInputStream流
     * @param fileName 导出的文件名字 1832a49d4de.xlsx
     **/
    public static void downExcel2(InputStream in,String fileName, HttpServletResponse response) {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ServletOutputStream ouputStream = null;
        try {

            int len = 0;
            byte[] b = new byte[1024];
            while ((len = in.read(b, 0, b.length)) != -1) {
                os.write(b, 0, len);
            }
            byte[] bytes = os.toByteArray();

            response.reset(); // 清空输出流
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-Disposition", "attachment;filename*=utf-8'zh_cn'" + URLEncoder.encode(fileName, "UTF-8"));
            response.addHeader("Cache-Control", "no-cache");
            response.setContentLength(bytes.length);
            ouputStream = response.getOutputStream();
            ouputStream.write(bytes, 0, bytes.length);
            ouputStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            CloseableUtils.close(ouputStream, os);
        }
    }


    /**
     * 生成excel到前端浏览器
     * @param wb 组装好的XSSFWorkbook对象
     * @param fileName 导出的文件名字
     */
    public static void downExcel3(String fileName, XSSFWorkbook wb, HttpServletResponse response) {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ServletOutputStream ouputStream = null;
        try {
            wb.write(os);
            byte[] bytes = os.toByteArray();
            response.reset(); // 清空输出流
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-Disposition", "attachment;filename*=utf-8'zh_cn'" + URLEncoder.encode(fileName+ ".xlsx", "UTF-8"));
            response.addHeader("Cache-Control", "no-cache");
            response.setContentLength(bytes.length);
            ouputStream = response.getOutputStream();
            ouputStream.write(bytes, 0, bytes.length);
            ouputStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            CloseableUtils.close(ouputStream, os);
        }
    }


}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值