I am trying to use Apache POI to read in old (pre-2007 and XLS) Excel files. My program goes to the end of the rows and iterates back up until it finds something that's not either null or empty. Then it iterates back up a few times and grabs those cells. This program works just fine reading in XLSX and XLS files made in Office 2010.
I get the following error message:
Exception in thread "main" java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at the line:
num = Double.parseDouble(str);
from the code:
str = cell.toString();
if (str != "" || str != null) {
System.out.println("Cell is a string");
num = Double.parseDouble(str);
} else {
System.out.println("Cell is numeric.");
num = cell.getNumericCellValue();
}
where the cell
is the last cell in the document that's not empty or null. When I try to print the first cell that's not empty or null, it prints nothing, so I think I'm not accessing it correctly.
Perhaps the reason why you're reading in blank cells is due to the usage of the right subcomponent of Apache POI to read Excel files. Use HSSF (Horrible SpreadSheet Format) for XLS formats and XSSF (XML SpreadSheet Format) for XLSX formats.
As for the code itself, you may want to refine your boolean expression. The way you have it now, since you're using the or operator (
||
),str != null
, andelse
part of your if statement will execute ifstr == null
.The first part of the if statement will throw a
NumberFormatException
in callingDouble.parseDouble
ifstr
cannot be parsed as a number.Maybe the following code snippet will help you:
To find out more about
Cell
, read its Javadoc.It would be nice if we all know what code line number caused the exception.
I suspect your first line of code is the cause. Object cell could be null, and a null address can not be converted to a String type. You can check by code.
Note: It's good that the code works with Office 2010 but I think this type of problem can happen in any Excel version.
It would be better to evaluate the cell type and then do what you need. I use this code to handle the cell data (check that I even handle blank cells):