Bug 55658 - SXSSFCell.convertCellValueToString() incorrectly implemented for CELL_TYPE_NUMERIC
Summary: SXSSFCell.convertCellValueToString() incorrectly implemented for CELL_TYPE_NU...
Alias: None
Product: POI
Classification: Unclassified
Component: SXSSF (show other bugs)
Version: 3.9-FINAL
Hardware: All All
: P2 blocker (vote)
Target Milestone: ---
Assignee: POI Developers List
Depends on:
Reported: 2013-10-16 10:51 UTC by Piers Geyman
Modified: 2013-10-16 19:48 UTC (History)
0 users


Note You need to log in before you can comment on or make changes to this bug.
Description Piers Geyman 2013-10-16 10:51:48 UTC
When you call cell.setValue( myValue ) for a numeric cell when using SXSSF the call fails with the following error:

java.lang.IllegalStateException: Cannot get a error value from a numeric cell
	at org.apache.poi.xssf.streaming.SXSSFCell.typeMismatch(SXSSFCell.java:841)
	at org.apache.poi.xssf.streaming.SXSSFCell.getErrorCellValue(SXSSFCell.java:487)
	at org.apache.poi.xssf.streaming.SXSSFCell.convertCellValueToString(SXSSFCell.java:891)
	at org.apache.poi.xssf.streaming.SXSSFCell.setType(SXSSFCell.java:760)
	at org.apache.poi.xssf.streaming.SXSSFCell.ensureTypeOrFormulaType(SXSSFCell.java:744)
	at org.apache.poi.xssf.streaming.SXSSFCell.setCellValue(SXSSFCell.java:235)

Looking at the code in org.apache.poi.xssf.streaming.SXSSFCell.convertCellValueToString it has an incorrect case for CELL_TYPE_NUMERIC. There is no implementation and it just uses the same code as CELL_TYPE_ERROR, which is obviously incorrect:

    private String convertCellValueToString() {
        int cellType = getCellType();

        switch (cellType) {
            case CELL_TYPE_BLANK:
                return "";
            case CELL_TYPE_BOOLEAN:
                return getBooleanCellValue() ? "TRUE" : "FALSE";
            case CELL_TYPE_STRING:
                return getStringCellValue();
            case CELL_TYPE_NUMERIC:
            case CELL_TYPE_ERROR:
                byte errVal = getErrorCellValue();
                return FormulaError.forInt(errVal).getString();
            case CELL_TYPE_FORMULA:
                return "";
                throw new IllegalStateException("Unexpected cell type (" + cellType + ")");

What should this code be? Is there an existing patch I can apply? This bug makes using SXSSF un-useable. I have a project that I have big memory issues with when using XSSF and need this implementation.

If this is just a case of converting the NumericCell value to a string, then why not just do:

    return Double.toString( getNumericCellValue() );

Or to be more OO, why not override toString() on the class NumericValue?
Comment 1 Dominik Stadler 2013-10-16 19:48:42 UTC
I took a look, it only happens if you set a cell first to a numeric value and then set a string value to the same cell.

So a workaround for now might be to only set the value of the cell once during creation of the xlsx.

I fixed it for the next release, see r1532873 in SVN.