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

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

相关推荐
天上掉下来个程小白15 分钟前
MybatisPlus-06.核心功能-自定义SQL
java·spring boot·后端·sql·微服务·mybatisplus
知了一笑22 分钟前
独立开发第二周:构建、执行、规划
java·前端·后端
寻月隐君27 分钟前
想用 Rust 开发游戏?这份超详细的入门教程请收好!
后端·rust·github
晴空月明40 分钟前
分布式系统高可用性设计 - 缓存策略与数据同步机制
后端
Real_man2 小时前
新物种与新法则:AI重塑开发与产品未来
前端·后端·面试
小马爱打代码2 小时前
Spring Boot:将应用部署到Kubernetes的完整指南
spring boot·后端·kubernetes
卜锦元2 小时前
Go中使用wire进行统一依赖注入管理
开发语言·后端·golang
SoniaChen334 小时前
Rust基础-part3-函数
开发语言·后端·rust
全干engineer4 小时前
Flask 入门教程:用 Python 快速搭建你的第一个 Web 应用
后端·python·flask·web
William一直在路上4 小时前
SpringBoot 拦截器和过滤器的区别
hive·spring boot·后端