Apache HSSF Problem

A

Audison.Athena

I ran into a peculiar problem when I use the apache poi library to
read an excel file. I think many people who has used this library
should have ran into this problem already and appreciate if anyone can
share his experience in solving this problem.
When I read some data from a pre-constructed excel file(the file was
created via office excel 2003, Chinese edition), I use the
getStringCellValue method to extract each cell from each row. Whenever
I extract some cell in which the content was made up by all digits,
this method may throw the runtime exception NumberFormatException.
However some other cells' contents also was made up of digits, this
method may pass.
I searched some topics related to this problem over the web. some
solutions said that every cell has a type, the HSSF cell is classified
as six type: CELL_TYPE_BLANK CELL_TYPE_BOOLEAN CELL_TYPE_ERROR
CELL_TYPE_FORMULA CELL_TYPE_NUMERIC CELL_TYPE_STRING
and through the insight of the source code of the HSSFCell class, it
seems for a numeric type cell, if you use the getStringCellValue, it
will deliberately throw NumberFormatException. Some suggested before
extract each cell value, first extract its type use getCellType method,
using if-then-else statement to code for each statement.
However, I think this doesn't touch the core of the problem. The
substantial problem is that why some cell constructed by all digits
will be deemed as a numeric type by hssf? if this is the case, if I
want to read the data as "0099", then read as a numeric value, I will
get "99", which is not the accepted case.
In office excel, each cell can set its cell style, for example, normal,
numeric, currency, date, text, etc. However when I change the failed
cell's type to text, and ran the program again, the problem still
exist, it seems the hssf cell's type has no relation to the excel
file's cell type determined when it was created in excel. what even
puzzled me is that when I changed the failed cell to some aother
numeric value, and save, and then change back, and then save again.
Then this time the Program pass this cell, and failed at the next cell.
So the substantial problem I'm urgent to know is that what determined
the hssf cell type, judging from the contents that made the cell value?
or judging from some factor as that was set when the cell was created
by office excel that each cell was bound to a specific type, or some
other factor. Because I know little about the excel file format, so the
problem seems to be buried too deep for me, anyone can help?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top