2016-03-03 115 views
1

我正在研究需要读取大型xlsx文件的需求,其中包含超过一百万条记录。 apache POI在读取大文件时效率并不高。因此,我在使用下面的API,其中添加了使用apache POI/monitorjbl/excel-streaming-reader读取大型xlsx文件中的空白单元问题

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) 

如果行的细胞是空则在开关的情况下即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()) { 
} 
    } 

如果我用细胞的细胞= row.getCell(I,Row.CREATE_NULL_AS_BLANK)读取空单元格为空白我得到以下问题。请帮我解决这个

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

试试这个,如果(Cell.CELL_TYPE_BLANK)Cell.setValue(”“); –

+1

为什么不在你的switch语句之前添加一个'if(cell!= null)'? – Gagravarr

+0

我将xlsx转换为csv。如果任何单元格为空,我必须将其视为空白并将空字符串(“”)视为该字段的csv数据。 – Kiran

回答

0

的方法地块不受流支持的Excel,但它给阅读大量的Excel文件的优势。您可以从行读取空白单元格(应用数据流的Excel阅读器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; 
} 
相关问题