easyexcel 导出excel,导出数据字段24个,数据有100000行,耗时27秒,能不能优化减少耗时

e0uiprwp  于 4个月前  发布在  其他
关注(0)|答案(1)|浏览(44)

String fileName ="D:\test.xlsx";
ExcelWriter excelWriter = EasyExcel.write(fileName,TestVo.class).excelType(ExcelTypeEnum.XLSX)
.inMemory(false)
.autoCloseStream(false)
.build();
List objects = new ArrayList();
for (int i = 0; i < 110000; i++) {
TestVo testVo = new TestVo();
testVo.setSapCustomerCode(System.currentTimeMillis()+""+i);
testVo.setOrderGoodsDtlId("名字");
objects.add(testVo);
}
List testVoLists = Lists.partition(objects, 110000).stream().map(ArrayList::new).collect(Collectors.toList());

ForkJoinPool forkJoinPool = new ForkJoinPool(100, TtlForkJoinPoolHelper.getDefaultDisableInheritableForkJoinWorkerThreadFactory(), null, false);
    forkJoinPool.submit(TtlRunnable.get(() -> testVoLists.parallelStream().forEach(m -> {
            WriteSheet writeSheet = EasyExcel.writerSheet(0, "名字").head(TestVo.class)
                    .registerWriteHandler(new CustomCellWriteHandler()).registerWriteHandler(EasyExcelUtil.getStyleStrategy())
                    .build();
            //写数据
            WriteTable writeTable = new WriteTable();
            excelWriter.write(m, writeSheet,writeTable);
    }))).join();
    excelWriter.finish();
qq24tv8q

qq24tv8q1#

String fileName = "D:\\test.xlsx"; // 注意需要使用双反斜杠来转义
ExcelWriter excelWriter = EasyExcel.write(fileName, TestVo.class)
        .excelType(ExcelTypeEnum.XLSX)
        .inMemory(false)
        .autoCloseStream(false)
        .build();

List<TestVo> objects = new ArrayList<>();
for (int i = 0; i < 110000; i++) {
    TestVo testVo = new TestVo();
    testVo.setSapCustomerCode(System.currentTimeMillis() + "" + i);
    testVo.setOrderGoodsDtlId("名字");
    objects.add(testVo);
}

// 将数据分批处理
List<List<TestVo>> testVoLists = Lists.partition(objects, 1000);

// 遍历分批数据并写入Excel
testVoLists.parallelStream().forEach(m -> {
    WriteSheet writeSheet = EasyExcel.writerSheet(0, "名字")
            .head(TestVo.class)
            .registerWriteHandler(new CustomCellWriteHandler())
            .registerWriteHandler(EasyExcelUtil.getStyleStrategy())
            .build();
    // 写数据
    excelWriter.write(m, writeSheet);
});

// 完成写入并关闭资源
excelWriter.finish();

相关问题