easyExcel多出大量数据方法

写了个导出大量数据的方法

java 复制代码
public void exportAll(String fileName, HttpServletResponse response,String startTime, String endTime) throws IOException {
    if (StringUtils.isEmpty(fileName)) {
        fileName = "数据-" + DateUtils.getNowTime();
    }
    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    response.setCharacterEncoding("utf-8");
    fileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8);
    response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");

    // 构建查询条件
    QueryWrapper<OldPortGateMaintainDo> queryWrapper = new QueryWrapper<>();
    if (startTime != null && endTime != null) {
        queryWrapper.between("create_time", startTime, endTime);
    }

    // 获取总条数
    Integer totalCount = gateMaintainMapper.selectCount(null);
    Integer maxRowsPerSheet = 5000; // 每个Sheet最多5000条
    Integer pageSize = 2000; // 每次查询2000条

    // 创建ExcelWriter(整个导出过程只创建一次)
    try (ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), GateMaintainVo.class)
            .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) // 自动列宽
            .build()) {

        int currentRowCount = 0; // 当前Sheet已写入行数
        int currentSheetIndex = 0; // 当前Sheet索引
        WriteSheet currentSheet = null; // 当前Sheet对象

        // 分页查询数据
        int totalPages = (totalCount + pageSize - 1) / pageSize;
        for (int page = 1; page <= totalPages; page++) {
            // 获取当前页数据
            List<OldPortGateMaintainDo> dataPage = gateMaintainMapper.selectPage(
                    new Page<>(page, pageSize), queryWrapper).getRecords();
            // 转换为VO
            List<GateMaintainVo> voList = dataPage.stream().map(e -> {
                GateMaintainVo vo = new GateMaintainVo();
                vo.setMaintainTime(e.getCreateTime());
                vo.setMaintainContent(e.getContent());
                vo.setUsr(e.getUsr());
                vo.setPhone(e.getPhone());
                return vo;
            }).collect(Collectors.toList());

            // 处理Sheet切换逻辑
            if (currentSheet == null || currentRowCount + voList.size() > maxRowsPerSheet) {
                // 创建新Sheet
                currentSheetIndex++;
                currentSheet = EasyExcel.writerSheet("Sheet" + currentSheetIndex)
                        .needHead(currentSheetIndex == 1) // 只在第一个Sheet写表头
                        .build();
                currentRowCount = 0; // 重置计数器
            }
            // 写入当前页数据
            excelWriter.write(voList, currentSheet);
            currentRowCount += voList.size();
        }
    }
}

如果有大佬有更简洁高效的方法的话欢迎指教哦!!!

相关推荐
mldong6 小时前
从 mldong 到 jeeflow:一个工作流引擎的独立进化
后端
陈随易6 小时前
MoonBit抓包模块pcap,查看电脑的每一次联网通信
前端·后端·程序员
Aaron - Wistron7 小时前
Web API C# (Furion版)带 单元测试
开发语言·后端·c#
卷无止境7 小时前
写代码这件事,到底该讲究点什么?
后端·python
卷无止境7 小时前
循环复杂度到底在算什么,Python 代码怎么才能写得让人一看就懂
后端·python
IT_陈寒9 小时前
Vite热更新失效?你可能漏了这个配置
前端·人工智能·后端
SomeB1oody10 小时前
【RustyML入门】1.0. 快速上手
开发语言·后端·机器学习·rust·教程
史呆芬11 小时前
分布式事务实战:微服务跨服务数据一致性解决方案
java·后端·spring cloud