Java 讀取xlsx


讀取特別大的xlsx文件時, 需要使用StreamingReader, 可以控制JVM內存峰值在200M以內

InputStream is = new FileInputStream(new File(filePath));
StreamingReader reader = StreamingReader.builder()
        .rowCacheSize(10)    // number of rows to keep in memory (defaults to 10)
        .bufferSize(1024)     // buffer size to use when reading InputStream to file (defaults to 1024)
        .sheetIndex(0)        // index of sheet to use (defaults to 0)
        .read(is);            // InputStream or File for XLSX file (required)

for (Row r : reader) {
    for (Cell cell : r) {
        switch (cell.getCellType()) {
            case Cell.CELL_TYPE_NUMERIC:
                System.out.print("D" + cell.getNumericCellValue() + "\t");
                break;
            case Cell.CELL_TYPE_STRING:
                System.out.print("S" + cell.getStringCellValue() + "\t");
                break;
            case Cell.CELL_TYPE_BOOLEAN:
                System.out.print("B" + (cell.getBooleanCellValue() + "\t"));
                break;
        }
    }
    System.out.print("\n");
}

https://github.com/monitorjbl/excel-streaming-reader

相比較官方的方案

File file = new File("C:\\D\\Data Book.xlsx");
OPCPackage opcPackage = OPCPackage.open(file);
XSSFWorkbook workbook = new XSSFWorkbook(opcPackage);

官方的方案內存占用明顯較高.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM