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

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

相关推荐
程序员小假33 分钟前
我们来说一下消息的可靠性投递
java·后端
duangww41 分钟前
SAPUI5 1.71.78老版本的消费restful服务
后端·restful
用户8599681677691 小时前
UE5虚幻引擎汽车HMI设计高级研修课
后端
用户8599681677691 小时前
鸿蒙HarmonyOS多线程编程实战:AI语音
后端
开心猴爷1 小时前
iOS 应用发布流程中常被忽视的关键环节
后端
用户21991679703911 小时前
使用Agent Framework进行多Agent工作流编排
后端
serendipity_hky1 小时前
【go语言 | 第5篇】channel——多个goroutine之间通信
开发语言·后端·golang
zhaorong2 小时前
RabbitMQ发布订阅模式同一消费者多个实例如何防止重复消费?
后端
开心猴爷2 小时前
提升 iOS 应用安全审核通过率的一种思路,把容易被拒的点先处理
后端
我家领养了个白胖胖2 小时前
Prompt、格式化输出、持久化ChatMemory
java·后端·ai编程