[alibaba/easyexcel]关于在数据量大的情况下,开启多个线程在同一个文件写入多个sheet,最终只有一个sheet

2024-06-20 735 views
0
@GetMapping("/consume/list/download")
public Result exportConsumeList(Boolean income, @RequestParam String company, String orderId, Long startTime, Long endTime) throws Exception {
    int batchMaxCount = 5000;
    int total = (Integer) walletService.findConsumeCount(income, company,orderId, startTime, endTime).getData();
    int pageNums = total / batchMaxCount;
    boolean flag = total % batchMaxCount == 0;
    int pages = pageNums > 1 && flag ?  pageNums + 1 : pageNums;

    final ArrayList<String> file = Lists.newArrayList();
    String tmpdir = System.getProperty("java.io.tmpdir");
    File writeFile = new File(tmpdir + File.separator + UUID.randomUUID().toString() + ExcelTypeEnum.XLSX.getValue());

    IntStream.range(1,pages).forEach(i->{
        PageVo<ChannelTurnover> data = channelUserService.findChannelConsumePage(income, company, orderId, startTime, endTime, i, batchMaxCount);
        List<ConsumeExport> exportList = getConsumeExportList(data);
        logger.info("data:"+ JSON.toJSONString(exportList));
        try {
            String tmp = getDownloadUri(writeFile, exportList,ConsumeExport.class,"消费列表",i).get();
            file.add(tmp);
        }catch (Exception e) {
            e.printStackTrace();
        }
    });

    return renderSuccess("成功",file.get(0));

}

@Async("scheduledExecutorService")
public Future<String> getDownloadUri(File writeFile, List dataList, Class entityClass, String sheetName,int sheetNo) throws IOException{
    ExcelWriter writer = new ExcelWriter(new FileOutputStream(writeFile),ExcelTypeEnum.XLSX);
    Sheet sheet = new Sheet(sheetNo,0, entityClass);
    sheet.setSheetName(sheetName + sheetNo);
    writer.write(dataList,sheet).finish();

    log.info("saveFile:" + writeFile.getAbsolutePath() + " file size: " + writeFile.length());
    String filePath = "/download/" + writeFile.getName();

    executorService.schedule(()->{
        writeFile.delete();
    },10, TimeUnit.MINUTES);
    return new AsyncResult<>(filePath);
}

回答

8

同一个方法里,writer使用多个数据集及sheet写入,然后finish,这样没问题

6

分批拿到数据然后写入就不行

1

目前不支持并发