Java 无论缺少单元格策略如何,ApachePOI都会在缺少单元格时停止解析excel文件
我正在编写一个脚本,对excel工作表中的每一行进行分析,并打印出该行中每个单元格的内容。代码如下:Java 无论缺少单元格策略如何,ApachePOI都会在缺少单元格时停止解析excel文件,java,apache-poi,Java,Apache Poi,我正在编写一个脚本,对excel工作表中的每一行进行分析,并打印出该行中每个单元格的内容。代码如下: for(int cntr=1; cntr<=firstSheet.getPhysicalNumberOfRows(); cntr++){ Row currentRow = firstSheet.getRow(cntr); System.out.println("-------------Working on Row Number: "+current
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;
}
}
}
但我仍然得到一个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秒)请执行两项更改,然后查看,我认为它应该可以工作: 第一次从for循环中删除“=”
for(int cntr=1; cntr<firstSheet.getPhysicalNumberOfRows(); cntr++){
请执行两个更改并查看,我认为它应该可以工作: 第一次从for循环中删除“=”
for(int cntr=1; cntr<firstSheet.getPhysicalNumberOfRows(); cntr++){
在代码中修改此选项
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}
希望这能解决您的问题在代码中修改此选项
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}
希望这能解决您的问题您的问题在于if语句
Cell currentCell = currentRow.getCell(cntr2, Row.RETURN_NULL_AND_BLANK);
if(currentCell==null){
...
}
switch(currentCell.getCellType()){
.
.
.
}
无论currentCell
是否为null,都将其放入switch语句中。这将导致swtich语句在currentCell时抛出NullPointerException
。如果currentCell
为null
,则调用getCellType()
。要消除此问题,请将switch语句放入else
子句中,如下所示:
Cell currentCell = currentRow.getCell(cntr2, Row.RETURN_NULL_AND_BLANK);
if(currentCell==null){
...
} else {
switch(currentCell.getCellType()){
.
.
.
}
}
你的问题在于if语句
Cell currentCell = currentRow.getCell(cntr2, Row.RETURN_NULL_AND_BLANK);
if(currentCell==null){
...
}
switch(currentCell.getCellType()){
.
.
.
}
无论currentCell
是否为null,都将其放入switch语句中。这将导致swtich语句在currentCell时抛出NullPointerException
。如果currentCell
为null
,则调用getCellType()
。要消除此问题,请将switch语句放入else
子句中,如下所示:
Cell currentCell = currentRow.getCell(cntr2, Row.RETURN_NULL_AND_BLANK);
if(currentCell==null){
...
} else {
switch(currentCell.getCellType()){
.
.
.
}
}
这并不会真正影响问题,但如果有人不明白为什么我的第一个计数器(int cntr)从1开始,我会跳过第一行,因为它只是excel电子表格中的一个标题。Thanksbetter捕获Nullpointer异常,这样您的程序就不会异常终止,即使我捕获到它,脚本仍然会停止。此外,理想情况下,我希望将每行的信息添加到数组中,因此我希望空单元格可以写为“empty”或null。您能告诉我们在ExcelFileProcesserLine 64的第64行上写的内容是:switch(currentCell.getCellType()){这并不会真正影响问题,但如果有人不明白为什么我的第一个计数器(int cntr)从1开始,我跳过第一行,因为它只是excel电子表格中的一个标题。感谢捕获Nullpointer异常,这样您的程序就不会异常终止。即使我捕获它,脚本也会停止。此外,理想情况下,我希望将每行的信息添加到数组中,因此如果为空,我希望它为空单元格可以写为“空”或null。您能告诉我们在ExcelFileProcesserLine 64的第64行写了什么吗?行64是:switch(currentCell.getCellType()){Ha,我不知道continue语句存在于Java中。我以为它只存在于Groovy中。但无论如何,它一直读到结尾都非常好。谢谢。Ha,我不知道continue语句存在于Java中。我以为它只存在于Groovy中。但不管怎样,它一直读到结尾都非常好。谢谢。