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();
        }
    }
}

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

相关推荐
打工仔折腾 AI2 小时前
从Attention到BERT:双向预训练语言模型到底解决了什么问题
人工智能·后端·python·深度学习·语言模型·bert
lizhongxuan3 小时前
Agent Sandbox: Firecracker 任务生命周期
后端
YYYing.3 小时前
【设计模式系列 (七) 】桥接模式
c++·后端·设计模式·桥接模式
lizhongxuan4 小时前
Agent Sandbox: Firecracker 镜像、启动与 I/O
后端
打工仔折腾 AI5 小时前
飞牛OS上用Docker Compose部署ExerciseDiary运动记录并配置远程访问
运维·人工智能·后端·python·docker·容器·ai agent 实战
小蒜学长5 小时前
基于Python的动物救助站管理系统的设计与实现(代码+数据库+LW)
数据库·后端·python·django·动物救助
小小张说故事5 小时前
Python 中 __new__ 和 __init__ 到底谁才是构造函数?一文讲透区别与 5 个坑
后端·python
用户847172102845 小时前
.shot 文件格式规范
前端·后端
wang_shu_mo_ran6 小时前
Spring IoC和DI概念篇
java·后端·spring
Cheney Pan6 小时前
第4篇 Spring Boot 应用监控:Micrometer 与 JVM 指标
jvm·spring boot·后端·prometheus