JXL和POI在生成Excel文件方面的性能比较

5

以下是使用JXL生成 .xls 文件的代码:

public void generateXls(String fileName, int sheets, int cols, int rows) {

    if (cols > 256) {
        throw new IllegalArgumentException("Error: number of cols per sheet must be < 256");
    }

    if (rows > 65536) {
        throw new IllegalArgumentException("Error: number of rows per sheet  must be < 65536");
    }

    String fullName = fileName + ".xls";
    WritableWorkbook workbook = null;
    try {
        workbook = Workbook.createWorkbook(new File(fullName));
    } catch (IOException e) {
        e.printStackTrace();
    }
    Random random = new Random();
    for (int s = 0; s < sheets; s++ ) {
        WritableSheet sheet = workbook.createSheet("Sheet" + s, 0);
        for (int i = 0; i < cols ; i++) {
            for (int j = 0;  j < rows; j++) {
                Number number = new Number(i, j, random.nextDouble()*1000);
                try {
                    sheet.addCell(number);
                } catch (RowsExceededException e) {
                    throw new RuntimeException("Error: too many rows in a sheet");
                } catch (WriteException e) {
                    throw new RuntimeException("Error occured while adding cell to sheet", e);
                }
            }
        }
    }

    try {
        workbook.write(); 
        workbook.close();
    } catch (WriteException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

以下是使用POI生成.xls和.xlsx文件的代码:

下面是使用POI生成.xls和.xlsx文件的代码:

public void generateXlsx(String fileName, int sheets, int cols, int rows) {

    if (cols > 16383) {
        throw new IllegalArgumentException("Error: number of cols per sheet must be < 16383");
    }
    XSSFWorkbook workbook = new XSSFWorkbook();

    Random random = new Random();
    for (int s = 0; s < sheets; s++ ) {
        XSSFSheet sheet = workbook.createSheet();
        for (int i = 0; i < rows ; i++) {
            XSSFRow row =  sheet.createRow(i);
            for (int j = 0;  j < cols; j++) {
                XSSFCell cell = row.createCell(j);
                cell.setCellValue(random.nextDouble()*1000);
            }
        }
    }

    FileOutputStream fileOut = null;
    try {
        fileOut = new FileOutputStream(fileName);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        workbook.write(fileOut);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        fileOut.flush();
        fileOut.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


}

public void generateXls(String fileName, int sheets, int cols, int rows) {

    if (cols > 256) {
        throw new IllegalArgumentException("Error: number of cols per sheet must be < 256");
    }

    HSSFWorkbook workbook = new HSSFWorkbook();

    Random random = new Random();
    for (int s = 0; s < sheets; s++ ) {
        HSSFSheet sheet = workbook.createSheet();
        for (int i = 0; i < rows ; i++) {
            HSSFRow row =  sheet.createRow(i);
            for (int j = 0;  j < cols; j++) {
                HSSFCell cell = row.createCell(j);
                cell.setCellValue(random.nextDouble()*1000);
            }
        }
    }

    FileOutputStream fileOut = null;
    try {
        fileOut = new FileOutputStream(fileName);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        workbook.write(fileOut);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        fileOut.flush();
        fileOut.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


}

我写了一个性能测试:
public static void main(String[] args) {

    int sheets =1;
    int cols = 255;
    int rows = 20000;
    long a1 = System.currentTimeMillis();
    ExcelFileGenerator generator  = new ExcelFileGenerator();
    generator.generateXls("xlsJXL.xls", sheets, cols, rows);
    long xls = System.currentTimeMillis()-a1;
    System.out.println("xlsJXL: " + xls);
    ExcelFileGeneratorPOI generatorPOI = new ExcelFileGeneratorPOI();
    long a2 = System.currentTimeMillis();
    generatorPOI.generateXls("xlsPOI.xls", sheets, cols, rows);
    long xlsPoi = System.currentTimeMillis()-a2;
    System.out.println("xlsPOI: " + xlsPoi);
    long a3 = System.currentTimeMillis();
    generatorPOI.generateXlsx("xlsxPOI.xlsx", sheets, cols, rows);
    long xlsx = System.currentTimeMillis()-a3;
    System.out.println("xlsxPOI: " + xlsx);
}

结果如下: sheets =1 cols = 255 rows = 10 xlsJXL: 133 xlsPOI: 162 xlsxPOI: 645
sheets =1 cols = 10 rows = 255 xlsJXL: 130 xlsPOI: 140 xlsxPOI: 650
sheets =10 cols = 255 rows = 255 xlsJXL: 611 xlsPOI: 784 xlsxPOI: 16228
sheets =2 cols = 100 rows = 10000
xlsJXL: 2755 xlsPOI: 3270 xlsxPOI: 在主线程中出现异常:java.lang.OutOfMemoryError: Java heap space 创建.xlsx文��时,使用POI比创建.xls文件慢的原因是什么?
1个回答

5

xls 是一种基于二进制的格式,xlsx 是一种基于 XML 的格式,读写需要更多的工作。

xlsx 可能还需要一个内存文档模型来解析/构建 XML,这可能更加复杂。

最后,由于支持时间更长,xls 可以更好地进行优化。


1
那么没有比我的实现更快地构建xlsx文件的解决方案吗? - AAaa
1
我不知道。可能有其他库可以更快地完成它。 - Peter Lawrey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接