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

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

相关推荐
风象南2 小时前
我把大脑开源给了AI
人工智能·后端
橙序员小站6 小时前
Agent Skill 是什么?一文讲透 Agent Skill 的设计与实现
前端·后端
怒放吧德德7 小时前
Netty 4.2 入门指南:从概念到第一个程序
java·后端·netty
雨中飘荡的记忆8 小时前
大流量下库存扣减的数据库瓶颈:Redis分片缓存解决方案
java·redis·后端
开心就好20259 小时前
UniApp开发应用多平台上架全流程:H5小程序iOS和Android
后端·ios
悟空码字10 小时前
告别“屎山代码”:AI 代码整洁器让老项目重获新生
后端·aigc·ai编程
小码哥_常10 小时前
大厂不宠@Transactional,背后藏着啥秘密?
后端
奋斗小强10 小时前
内存危机突围战:从原理辨析到线上实战,彻底搞懂 OOM 与内存泄漏
后端
小码哥_常10 小时前
Spring Boot接口防抖秘籍:告别“手抖”,守护数据一致性
后端
心之语歌11 小时前
基于注解+拦截器的API动态路由实现方案
java·后端