zoukankan      html  css  js  c++  java
  • 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() + "	");
                    break;
                case Cell.CELL_TYPE_STRING:
                    System.out.print("S" + cell.getStringCellValue() + "	");
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    System.out.print("B" + (cell.getBooleanCellValue() + "	"));
                    break;
            }
        }
        System.out.print("
    ");
    }

    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);

    官方的方案内存占用明显较高.

  • 相关阅读:
    js面向对象和PHP面相对象
    git
    css3动画、2D与3D效果
    渲染数据方式
    ajax
    面向对象
    Date 日期
    Math 数值对象
    What is CGLib and JDK动态代理
    IDEA中lock对象不提示newCondition();
  • 原文地址:https://www.cnblogs.com/milton/p/5302507.html
Copyright © 2011-2022 走看看