alibaba/easyexcel

使用模板填充,有20个sheet,在某个几万条的sheet处oom,并且服务重启第一次跑正常,多导几次oom

Open

#4118 opened on Jun 19, 2025

View on GitHub
 (0 comments) (0 reactions) (0 assignees)Java (33,728 stars) (7,599 forks)batch import
help wanted

Description

建议先去看文档

快速开始常见问题

异常代码

  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        try (ExcelWriter excelWriter = EasyExcel.write(outputStream).withTemplate(this.adjustTempByEasyExcel(organizationId,scanId,lang)).build()) {
            FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
            //-------------------------第一个sheet页(封面)-----------------------------
            Map<String, Object> coverParam = this.getPrompt(lang, "xhiker.analyseReport");
            Object coverSheetName = coverParam.get("xhiker_analyseReport_cover_cover");
            WriteSheet coverSheet = EasyExcel.writerSheet(0).registerWriteHandler(new CustomTemplateSheetStrategyHandler(0, String.valueOf(coverSheetName))).build();
            ScanInfoQueryVO scanInfo = scanInfoRepository.selectScanInfoDetail(scanId);
            coverParam.put("projectNumber", scanInfo.getProjectCode());
            coverParam.put("projectName", scanInfo.getProjectName());
            coverParam.put("projectVersion", scanInfo.getProjectVersion());
            coverParam.put("sapVersionName", scanInfo.getTargetVersionName());
            coverParam.put("reportDate", new Date());
            excelWriter.fill(coverParam, coverSheet);
            Map<String, Object> systemBackgroundParam = this.getPrompt(lang, "xhiker.systemBackground");
            if (this.isAdministratorRole()) {
                this.uewSheet(organizationId, scanId, lang, excelWriter, fillConfig, systemBackgroundParam);
                this.customizedDevelopmentSheet(organizationId, scanId, lang, excelWriter, fillConfig);
            } else {
                if (Constants.ScanStatus.COMPLETE.equals(scanInfo.getUewStatusCode())) {
                    this.uewSheet(organizationId, scanId, lang, excelWriter, fillConfig, systemBackgroundParam);
                }
                if (Constants.ScanStatus.COMPLETE.equals(scanInfo.getCsaStatusCode())) {
                    this.customizedDevelopmentSheet(organizationId, scanId, lang, excelWriter, fillConfig);
                }
            }
            excelWriter.finish();
            outputStream.close();
            return outputStream;
        } catch (IOException e) {
            log.error("Error creating temporary file", e);
            throw new CommonException("Failed to create temporary file");
        }
    }
 private void uewSheet(Long organizationId, Long scanId, String lang, ExcelWriter excelWriter, FillConfig fillConfig, Map<String, Object> systemBackgroundParam) throws IOException {
       
        //客制程序
        this.scanCpSheet(organizationId, scanId, lang, excelWriter, fillConfig);
        、、、
    }
 public void scanCpSheet(Long tenantId, Long scanId, String lang, ExcelWriter excelWriter, FillConfig fillConfig){
       
        //分页写入
        int pageSize = 1500; // 每批处理量
        int pageNum = 0;
        PageRequest pageRequest = new PageRequest();
        pageRequest.setSize(pageSize);
        pageRequest.setPage(pageNum);
        List<ScanCpDetail> batchList;
        do {
            batchList = scanCpDetailService.viewQuery(tenantId, pageRequest, scanCpDetail);
            excelWriter.fill(new FillWrapper("data1", batchList), fillConfig, scanCpSheet);
            pageNum++;
            pageRequest.setPage(pageNum);
        } while (!batchList.isEmpty());
    }

异常提示

大家尽量把问题一次性描述清楚,然后贴上全部异常,这样方便把问题一次性解决掉。 至少大家要符合一个原则就是,能让其他人复现出这个问题,如果无法复现,肯定无法解决。

问题描述

Contributor guide