无论是否缺少单元格策略,Apache POI都会停止将excel文件解析为丢失的单元格

时间:2013-07-12 15:17:00

标签: java apache-poi

我正在编写一个脚本,该脚本解析Excel工作表中的每一行,并打印出该行中每个单元格的内容。这是代码:

    for(int cntr=1; cntr<=firstSheet.getPhysicalNumberOfRows(); cntr++){
        Row currentRow = firstSheet.getRow(cntr);
        System.out.println("-------------Working on Row Number: "+currentRow.getRowNum());
        for(int cntr2 = 0; cntr2<currentRow.getLastCellNum(); cntr2++){
            Cell currentCell = currentRow.getCell(cntr2, Row.RETURN_NULL_AND_BLANK);
            if(currentCell==null){
                //cell doesn't have anything in there
                System.out.println("Cell in Row number "+(currentRow.getRowNum()+1)+" is null.");
            }
            switch(currentCell.getCellType()){
                case Cell.CELL_TYPE_STRING:
                    System.out.println("Current Cell: "+currentCell.getStringCellValue());
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    System.out.println("Current Cell: "+currentCell.getNumericCellValue());
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    System.out.println("Current Cell: "+currentCell.getBooleanCellValue());
                    break;
                case Cell.CELL_TYPE_BLANK:
                    System.out.println("Cell in row number "+(currentRow.getRowNum()+1)+" is returned as blank");
                    break;
            }
        }

    }

问题在于,在我的Excel电子表格的第11行(如果从0开始计算,则是第10行),有一个单元格,确切地说是3个单元格,其中没有任何内容。每次我的脚本到达这一点时,它会抛出一个空指针异常并停止运行,尽管我的IDE说构建成功了。我做了一些研究如何处理这个问题,并找到了MissingCellPolicy,我在行中使用了

    Cell currentCell = currentRow.getCell(cntr2, Row.RETURN_NULL_AND_BLANK);

但我仍然收到NullPointerException错误。我的算法出了什么问题?

另外,这是输出:

    Exception in thread "main" java.lang.NullPointerException

    ...

    -------------Working on Row Number: 10
    Current Cell: OC[C@H]1OC(O)[C@H](O)[C@@H](O)[C@@H]1O
    Current Cell: NCGC00159408-02
    Current Cell: DL-Glucose
    Current Cell: 180.16
    Current Cell: -3.51
    Current Cell: 6.0
    Current Cell: 5.0
    -------------Working on Row Number: 11
    Current Cell: OC[C@@H](O)C(O)[C@@H](O)CO
    Current Cell: NCGC00165982-01
    Cell in Row number 11 is null.
   at excellibrarycreation.ExcelFileProcesser.processFile(ExcelFileProcesser.java:64)
   at excellibrarycreation.ExcelLibraryCreation.main(ExcelLibraryCreation.java:25)

Java结果:1         建立成功(总时间:11秒)

3 个答案:

答案 0 :(得分:2)

请执行两项更改并查看,我认为它应该有效:

首先从for循环中移除'='

for(int cntr=1; cntr<firstSheet.getPhysicalNumberOfRows(); cntr++){

第二次使用,如果它没有改变你的结果/要求

Row.CREATE_NULL_AS_BLANK       

答案 1 :(得分:2)

你的问题在于if语句。

Cell currentCell = currentRow.getCell(cntr2, Row.RETURN_NULL_AND_BLANK);
if(currentCell==null){
    ...
}
switch(currentCell.getCellType()){
               .
               .
               .
}

无论currentCell是否为null,都将其放入switch语句中。如果NullPointerExceptioncurrentCell.getCellType(),则在currentCell被调用时,这将导致swtich语句抛出null。要摆脱这种情况,请将switch语句放入else子句中,如下所示:

Cell currentCell = currentRow.getCell(cntr2, Row.RETURN_NULL_AND_BLANK);
if(currentCell==null){
    ...
} else {
    switch(currentCell.getCellType()){
                   .
                   .
                   .
    }
}

答案 2 :(得分:1)

在您的代码中修改此内容

    if(currentCell==null){
                //cell doesn't have anything in there
      System.out.println("Cell in Row number "+(currentRow.getRowNum()+1)+" is null.");
      continue;// continue if row is null..meaning do not go to switch case statement
                    }

            }
            switch(currentCell.getCellType()){
         ////you do not want to enter here if currentCell is null
                //cell doesn't have anything in there}

希望这能解决您的问题