Skip to content
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

测试类中写入表格数据丢失 #3780

Open
lovejk888 opened this issue Apr 22, 2024 · 1 comment
Open

测试类中写入表格数据丢失 #3780

lovejk888 opened this issue Apr 22, 2024 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@lovejk888
Copy link

触发场景描述

测试方法中
image
这个,之后如果要保存数据,应该加上excelWriter.finish();
不加就会出现生成的表格里面没有sheet也没有数据

触发Bug的代码

       // 方法2: 如果写到不同的sheet 同一个对象
        fileName = TestFileUtil.getPath() + "repeatedWrite" + System.currentTimeMillis() + ".xlsx";
        // 这里 指定文件
        try (ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build()) {
            // 去调用写入,这里我调用了五次,实际使用时根据数据库分页的总的页数来。这里最终会写到5个sheet里面
            for (int i = 0; i < 5; i++) {
                // 每次都要创建writeSheet 这里注意必须指定sheetNo 而且sheetName必须不一样
                WriteSheet writeSheet = EasyExcel.writerSheet(i, "模板" + i).build();
                // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
                List<DemoData> data = data();
                excelWriter.write(data, writeSheet);
            }
        }

提示的异常或者没有达到的效果

没有达到效果就会出现类似下图
image
sheet名字并不是想要的,并且sheet里面没有数据信息

@lovejk888 lovejk888 added the bug Something isn't working label Apr 22, 2024
@ls9527
Copy link
Contributor

ls9527 commented Apr 25, 2024

try (ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build()){}

这种try-with-resource 的写法,会在try完成后,自动执行close方法,close会调用finish.

如果你在try里面用这个文件,那么可能出现这种情况,请在try后面再看这个文件试试

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants