Skip to content

同样的数据,生成的excel有时可以打开,有时打不开 #4094

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wangnaijiang opened this issue Mar 14, 2025 · 2 comments
Open
Labels
help wanted Extra attention is needed

Comments

@wangnaijiang
Copy link

wangnaijiang commented Mar 14, 2025

同样的数据,batchDataList都是26358行, 分3次写入,多次调用这个方法,
有时返回byte[]长度=5894824,这时生成的excel可以打开;
有时返回byte[]长度=5897583,这时生成的excel打不开

ExcelWriterBuilder excelWriterBuilder = EasyExcel.write(outputStream, clazz).registerWriteHandler(EasyExcelUtil.defaultCellFormat());
        try (ExcelWriter excelWriter = excelWriterBuilder.build()) {
            ExcelWriterSheetBuilder sheet = excelWriterBuilder.sheet(sheetName);
            WriteSheet writeSheet = sheet.build();
            do {
                batchDataList = queryFunction.apply(BatchPageParam.of(queryParam, batchIndex, batchSize));
                excelWriter.write(batchDataList, writeSheet);
                batchIndex++;
                //安全防御代码。
                AssertUtil.isTrue( batchIndex < batchLoopLimit, "批次数量超过上限");
            // 如果数据不足 PAGE_SIZE,说明数据已全部读取完毕
            } while (batchDataList.size() == batchSize);
            excelWriter.finish();
            return outputStream.toByteArray();
        }

获取字节流后,再上传到阿里云oss,在上传前打印了byte[]数组的长度。
easyexcel版本 3.2.1

背景:

  1. 项目2月底发布了一次,每天大约导出35个文件,导出大约450个excel后,13天后开始出现该问题,
  2. 今天再次发布后,问题又消失了
@wangnaijiang wangnaijiang added the help wanted Extra attention is needed label Mar 14, 2025
@wangnaijiang
Copy link
Author

wangnaijiang commented Apr 1, 2025

原因找到了:创建了两次ExcelWriter

  1. ExcelWriter excelWriter = excelWriterBuilder.build();
  2. ExcelWriterSheetBuilder sheet = excelWriterBuilder.sheet(sheetName); 这个方法会隐藏创建一个ExcelWriter
  3. 两个ExcelWriter,一个显式调用,一个gc调用,处理文件头信息残留导致。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants
@wangnaijiang and others