Search code examples
javaexcelapache-poipoi-hssf

Reading string value from Excel with HSSF but it's double


I'm using HSSF-POI for reading excel data. The problem is I have values in a cell that look like a number but really are strings. If I look at the format cell in Excel, it says the type is "text". Still the HSSF Cell thinks it's numeric. How can I get the value as a string?

If I try to use cell.getRichStringValue, I get exception; if cell.toString, it's not the exact same value as in Excel sheet.

Edit: until this gets resolved, I'll use

new BigDecimal(cell.getNumericCellValue()).toString()

Solution

  • You mean HSSF-POI says

    cell.getCellType() == Cell.CELL_TYPE_NUMERIC

    NOT

    Cell.CELL_TYPE_STRING as it should be?

    I would think it's a bug in POI, but every cell contains a Variant, and Variant has a type. It's kind of hard to make a bug there, so instead I think Excel uses some extra data or heuristic to report the field as text. Usual MS way, alas.

    P.S. You cannot use any getString() on a Variant containing numeric, as the binary representation of the Variant data depends on it's type, and trying to get a string from what is actually a number would result in garbage -- hence the exception.