SpringBoot使用POI导出Excel文件,读取Exce内容的工具类封装和自动合并单元格(内容相同)。使用vue2 和axios 下载

本文介绍了如何在SpringBoot中使用POI工具类封装导出Excel文件,包括读取Excel内容和自动合并相同单元格功能。通过HttpServletResponse直接写入输出流,避免了临时文件的创建。在实际应用中遇到axios下载文件导致的文件损坏问题,通过调整响应头和测试发现浏览器直接请求可以正常下载。此外,还分享了2019年11月14日新增的自动合并单元格功能及其调用方式。
摘要由CSDN通过智能技术生成

报表导出功能在JavaWeb 开发中非常常见,网上搜索相应的代码也非常多,大多都相似:利用POI生成excel 文件到服务器,再利用InputStream和Response 返回给前端做处理。无聊中发现WorkBook的write方法API如下:

随有更改stream为Response.getOutputStream()来减少创建文件的开销。实现工具类如下:

package com.newtouch.dssp.utils;


import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URLEncoder;
import java.util.*;

/**
 * @description: 基于POI的Excel工具类
 * @author: xiao.wang@newtouch.com
 * @date: 2019/8/15
 **/
public class ExcelUtil {

    private static final String XML ="xls";
    private static final String XLSX ="xlsx";

    /**
     * @description: //todo 下载Excel (样式为通用样式,sheet页是一个)
     * @author: xiao.wang@newtouch.com
     * @param resp response返回
     * @param fileName 文件名 不需要后缀 默认是xlsx
     * @param datas 数据
     * @param titles 表头
     * */
    public static void exportExcel(HttpServletResponse resp, String fileName,
                                   Collection<?> datas, List<String> titles)throws Exception{
        resp.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        resp.addHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(fileName, "utf-8")+ ".xlsx");
        exportExcel(resp.getOutputStream(),datas,titles);
    }

    /**
     * @description: //todo Excel 写入OutputStream
     * @author: xiao.wang@newtouch.com
     * @param out 输出流
     * @param datas 数据
     * @param titles 表头
     * */
    public static void exportExcel(OutputStream out,Collection<?> datas, List<String> titles){
        XSSFWorkbook wb = new XSSFWorkbook();
        try {
            XSSFSheet sheet = wb.createSheet("new sheet");
            writeDataToExcel(wb,sheet,datas,titles);
            wb.write(out);
        }catch (IOException ioe){
            ioe.printStackTrace();
        }
    }

    /**
     * @description: // todo 读取上传的Excel文件
     * @author: xiao.wang@newtouch.com
     * @param file 上传的文件
     * @param startRow  开始行 0开始
     * @param startCell 开始列 0开始
     * */
    public static List<String[]> readExcel(MultipartFile file, int startRow, int startCell)throws Exception{
        checkFile(file);
        String fileName = file.getOriginalFilename();
        Workbook workbook = getWorkBook(file,fileName);
        List<S
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值