[alibaba/easyexcel]同样用XSSFWorkbook创建单元格,为何easyExcel比直接使用poi创建快很多

2024-04-25 572 views
1

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:row.createCell(columnIndex),为何耗时相关了1个数量级,直接使用poi创建耗时是easyExcel的10倍,求解

回答

5

记得之前回复过你呀 SXSSFWorkbook 不是XSSFWorkbook

1

记得之前回复过你呀 SXSSFWorkbook 不是XSSFWorkbook

我也回复了你的回复,之后就没下文了,我调整了参数就是为了做比较,代码我上面也贴了,同样都是使用的XSSFWorkbook,但耗时就是不一样的,我debug代码看了的,拜托请细看