使用apache POI / monitorjbl / excel-streaming-reader读取大型xlsx文件中的空白单元格的问题

时间:2016-03-03 08:04:32

标签: java apache-poi

我正在处理需求,我需要读取大型xlsx文件包含超过一百万条记录。读取大文件时,apache POI的内存效率不高.Hence我正在使用下面的API添加

https://github.com/monitorjbl/excel-streaming-reader,它是流API的包装,同时保留了标准POI API的语法。一切正常,除了读取行中的空白单元格。如果单元格为空,则上面的API抛出空指针

       for(int i=0; i<=expectedColumns-1; i++) {
              Cell cell = row.getCell(i);
    switch (cell.getCellType()) {
    }
            }
java.lang.NullPointerException
at test.XLSXToCSVConverterStreamer.xlsx(XLSXToCSVConverterStreamer.java:67)
at test.XLSXToCSVConverterStreamer.main(XLSXToCSVConverterStreamer.java:164)

如果行中的单元格为空,则在Switch case处抛出空指针,即cell.getCelltype。我修改了代码以将空单元格读取为空白单元格但不支持

    for(int i=0; i<=expectedColumns-1; i++) {
      //Cell cell = row.getCell(i);
     Cell cell = row.getCell(i, Row.CREATE_NULL_AS_BLANK);
switch (cell.getCellType()) {
}
    }

如果我使用Cell cell = row.getCell(i,Row.CREATE_NULL_AS_BLANK)将空单元格读为空白,我将面临问题。请帮我解决这个问题

com.monitorjbl.xlsx.exceptions.NotSupportedException
at com.monitorjbl.xlsx.impl.StreamingRow.getCell(StreamingRow.java:108)

1 个答案:

答案 0 :(得分:0)

流式传输excel不支持很多方法但是它提供了读取大型Excel文件的优势。您可以按如下方式读取行中的空白单元格(使用Streaming Excel Reader v1.1.0)

boolean flag = false;
int lastcolno = row.getLastCellNum();

for (colno = 0; colno < lastcolno; colno++) {
    colFlag = isColumnEmpty(row, colno);

    if (flag == true)
        break;
}

if (colFlag == true) {
     System.out.println("In index row, column no: "
            + (colno + 1) + " is empty");
}

public static boolean isColumnEmpty(Row row, int colno) {
     Cell c = row.getCell(colno);
     if (c == null || c.getCellType() == Cell.CELL_TYPE_BLANK)
            return true;
return false;
}