[alibaba/easyexcel]求解:同样用XSSFWorkbook,为何easyExcel创建1个单元格速度比直接使用poi创建1个单元格快很多

2024-04-25 2 views
7

1.直接使用poi api创建: String templateFileName = TestFileUtil.getPath() + "demo" + File.separator + "fill" + File.separator + "list.xlsx";

    byte[] templateFileByte = FileUtils.readFileToByteArray(new File(templateFileName));
    Workbook workbook = new XSSFWorkbook(new ByteArrayInputStream(templateFileByte));
    Sheet currentSheet = workbook.getSheetAt(0);

    for(int i=0;i<100000;i++){
        Row row = currentSheet.createRow(i+2);
        for(int j=0;j<10;j++){
            long start = System.nanoTime();
            Cell cell = row.createCell(j);
            cell.setCellValue(i+":"+j);
            System.out.println((i + ":" + j)+"::"+ (System.nanoTime() - start));
        }
    }

2.使用easyExcel创建 String templateFileName = TestFileUtil.getPath() + "demo" + File.separator + "fill" + File.separator + "1.xlsx";

    // 方案1 一下子全部放到内存里面 并填充
    String fileName = TestFileUtil.getPath() + "listFill" + System.currentTimeMillis() + ".xlsx";
    // 这里 会填充到第一个sheet, 然后文件流会自动关闭
    EasyExcel.write(new FileOutputStream(new File(fileName))).withTemplate(new FileInputStream(new File(templateFileName))).sheet().doFill(data1());

easyExcel创建单元格: private Cell createCellIfNecessary(Row row, Integer lastColumnIndex, CellWriteHandlerContext cellWriteHandlerContext) {

    Cell cell = row.getCell(lastColumnIndex);

    if (cell != null) {
        return cell;
    }
    WriteHandlerUtils.beforeCellCreate(cellWriteHandlerContext);
    long start = System.nanoTime();
    cell = row.createCell(lastColumnIndex);
    System.out.println(Thread.currentThread().getName() + ":" +System.currentTimeMillis() + ":" +(System.nanoTime() - start));

    cellWriteHandlerContext.setCell(cell);

    WriteHandlerUtils.afterCellCreate(cellWriteHandlerContext);
    return cell;
}

问题:创建cell底层都是调用了poi的api,为何耗时相关了1个数量级,直接使用poi创建耗时是easyExcel的10倍,求解

回答

9

XSSFWorkbook 和SXSSFWorkbook 的区别 都是poi

0

XSSFWorkbook 和SXSSFWorkbook 的区别 都是poi

不是的 ,我调了参数,都是使用的XSSFWorkbook ,底层创建单元格调用都一样,不知为何耗时相差这么大

2

XSSFWorkbook 和SXSSFWorkbook 的区别 都是poi