使用 POI 读取 xlsx 文件 (SXSSFWorkbook)

2022-09-04 07:24:27

我正在尝试使用POI读取大型xlsx文件的第一次测试,但是要对小文件进行简单测试,我无法显示单元格的值。

有人可以告诉我我的错误是什么。欢迎所有建议。谢谢。

测试.java:

import java.io.File;
import java.io.FileInputStream;

import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Test {

    public static void main(String[] args) throws Throwable {
        File file = new File("/tmp/test.xlsx");
        OPCPackage pkg = OPCPackage.open(new FileInputStream(file.getAbsolutePath()));
        XSSFWorkbook xssfwb = new XSSFWorkbook(pkg);

        SXSSFWorkbook wb = new SXSSFWorkbook(xssfwb, 100);
        Sheet sh = wb.getSheet("Hola");

        System.out.println("Name: "+sh.getSheetName()); // Line 19
        System.out.println("Val: "+sh.getRow(1).getCell(1).getStringCellValue()); // Line 20
    }
}

结果:

Name: Hola
Exception in thread "main" java.lang.NullPointerException
    at Test.main(Test.java:20)

测试.xlsx:

enter image description here


答案 1

请咨询:类似问题SXSSFWorkBook是只写,它不支持阅读。

对于.xlsx文件的低内存读取,您应该查看XSSF和SAX EventModel文档:Gagravarr

如果内存不是问题,您可以使用XSSFSheet代替例如

    File file = new File("D:/temp/test.xlsx");
    FileInputStream fis = new FileInputStream(file);
    XSSFWorkbook wb = new XSSFWorkbook(fis);

    XSSFSheet sh = wb.getSheet("Hola");
    System.out.println(sh.getLastRowNum());
    System.out.println("Name: "+sh.getSheetName()); 
    Row row = sh.getRow(1);

    System.out.println(row.getRowNum());

    System.out.println("Val: "+sh.getRow(1).getCell(1).getStringCellValue()); 

答案 2

在解析xlsx文件时,我也遇到了相同的OOM问题...经过两天的奋斗,我终于找到了下面真正完美的代码;

此代码基于 sjxlsx。它读取xlsx并存储在HSSF工作表中。

            // read the xlsx file
       SimpleXLSXWorkbook = new SimpleXLSXWorkbook(new File("C:/test.xlsx"));

        HSSFWorkbook hsfWorkbook = new HSSFWorkbook();

        org.apache.poi.ss.usermodel.Sheet hsfSheet = hsfWorkbook.createSheet();

        Sheet sheetToRead = workbook.getSheet(0, false);

        SheetRowReader reader = sheetToRead.newReader();
        Cell[] row;
        int rowPos = 0;
        while ((row = reader.readRow()) != null) {
            org.apache.poi.ss.usermodel.Row hfsRow = hsfSheet.createRow(rowPos);
            int cellPos = 0;
            for (Cell cell : row) {
                if(cell != null){
                    org.apache.poi.ss.usermodel.Cell hfsCell = hfsRow.createCell(cellPos);
                    hfsCell.setCellType(org.apache.poi.ss.usermodel.Cell.CELL_TYPE_STRING);
                    hfsCell.setCellValue(cell.getValue());
                }
                cellPos++;
            }
            rowPos++;
        }
        return hsfSheet;

推荐