【实战】excel分页写入导出大文件

java 复制代码
 @RequestMapping("export")
    @ResponseBody
    public void export(HttpServletResponse response) {
        long start = System.currentTimeMillis();
        QueryVo query = new QueryVo();

        // response响应头
        setResponseHeader(response, "excel");
        ExcelWriter writer = ExcelUtil.genExcelWriter(response);
        // 表格
        WriteTable table = new WriteTable();
        WriteSheet sheet = new WriteSheet();
        sheet.setSheetNo(1);
        sheet.setSheetName("excel");
        writer.write(Lists.newArrayList(),sheet,table);

        int pageNo=1;
        query.setPageSize(exportPageSize);
        int index = 0;

        while (true) {
            // 构造分页信息
            int offset = exportPageSize * (pageNo++ - 1);
            query.setOffset(offset);
            // 查询一页数据
            List<Pharmacist> list = mybatisDao.getList(query);
            if (CollectionUtils.isEmpty(list)) {
                break;
            }
            // 分批写入
            writer.write(list,sheet);
        }
        long end = System.currentTimeMillis();
        log.info("导出excel耗时:{}", end - start);
        writer.finish();

    }

工具类

java 复制代码
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.example.demo.utils.BizException;
import com.example.demo.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;

/**
 * @Description: excel工具
 */
@Slf4j
public class ExcelUtil {

    /**
     * 读取excel并处理数据
     * @param inputStream
     * @param dataInfo  function中处理的数据文件格式为 List<List<String>>
     */
    public static void readExcel(InputStream inputStream,ExcelDataInfo dataInfo){
        log.info("------读取excel文件开始-------");
        ExcelDataListener headerListener = new ExcelDataListener(dataInfo);
        EasyExcel.read(inputStream, null,headerListener).sheet().headRowNumber(1).doRead();
        log.info("------读取excel文件结束-----\n------表头------\n" + JsonUtil.toJson(headerListener.getHeader()));

    }

    /**
     * 读取excel并处理数据
     * @param inputStream
     * @param dataInfo  function中处理的数据文件格式为 List<List<String>>
     */
    public static void readExcel(InputStream inputStream,ExcelDataInfo dataInfo, int headerRow){
        log.info("------读取excel文件开始-------");
        ExcelDataListener headerListener = new ExcelDataListener(dataInfo);
        EasyExcel.read(inputStream, null,headerListener).sheet().headRowNumber(headerRow).doRead();
        log.info("------读取excel文件结束-----\n------表头------\n" + JsonUtil.toJson(headerListener.getHeader()));
    }

    /**
     * 生成writer
     * @param response
     * @return
     */
    public static ExcelWriter genExcelWriter(HttpServletResponse response){
        WriteCellStyle wcs = new WriteCellStyle();
        // 垂直居中、 左对齐
        wcs.setVerticalAlignment(VerticalAlignment.CENTER);
        wcs.setHorizontalAlignment(HorizontalAlignment.LEFT);
        WriteFont wf = new WriteFont();
        wf.setFontHeightInPoints((short)12);
        wcs.setWriteFont(wf);
        // 表头
        WriteCellStyle hwcs = new WriteCellStyle();
        hwcs.setVerticalAlignment(VerticalAlignment.CENTER);
        hwcs.setHorizontalAlignment(HorizontalAlignment.CENTER);
        WriteFont hwf = new WriteFont();
        hwf.setFontHeightInPoints((short)12);
        hwf.setBold(Boolean.TRUE);
        hwcs.setWriteFont(hwf);

        try {
            ExcelWriter writer = EasyExcel.write(response.getOutputStream()).registerWriteHandler(new HorizontalCellStyleStrategy(hwcs,wcs)).build();
            return writer;
        } catch (Exception e) {
            log.error("导出excel失败",e);
            throw new BizException("导出excel失败", e);
        }
    }

    /**
     * 设置导出response响应头
     * @param response
     * @param fileName
     */
    public static void setResponseHeader(HttpServletResponse response,String fileName){

        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition","attachment;filename="+fileName);
        response.setHeader("Param","no-cache");
        response.setHeader("Cache-Control","no-cache");
    }
}
相关推荐
海边的Kurisu2 小时前
苍穹外卖日记 | Day1 苍穹外卖概述、开发环境搭建、接口文档
java
C雨后彩虹5 小时前
任务最优调度
java·数据结构·算法·华为·面试
heartbeat..5 小时前
Spring AOP 全面详解(通俗易懂 + 核心知识点 + 完整案例)
java·数据库·spring·aop
Jing_jing_X5 小时前
AI分析不同阶层思维 二:Spring 的事务在什么情况下会失效?
java·spring·架构·提升·薪资
元Y亨H7 小时前
Nacos - 服务发现
java·微服务
微露清风7 小时前
系统性学习C++-第十八讲-封装红黑树实现myset与mymap
java·c++·学习
dasi02277 小时前
Java趣闻
java
阿波罗尼亚8 小时前
Tcp SSE Utils
android·java·tcp/ip
susu10830189118 小时前
springboot3.5.8整合minio8.5.9
java·springboot
不知道累,只知道类8 小时前
深入理解 Java 虚拟线程 (Project Loom)
java·开发语言