From: Nick Burch Date: Mon, 14 Jul 2014 18:53:39 +0000 (+0000) Subject: Fix for #56702 - If a cell is of type numeric but has an empty tag, return as 0 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=696b9d6406b9f08458b32387aadb21ea5ba210ea;p=poi.git Fix for #56702 - If a cell is of type numeric but has an empty tag, return as 0 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1610482 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java index b74cc3b898..e12e3e1b75 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java @@ -206,8 +206,10 @@ public final class XSSFCell implements Cell { case CELL_TYPE_FORMULA: case CELL_TYPE_NUMERIC: if(_cell.isSetV()) { + String v = _cell.getV(); + if (v.isEmpty()) return 0.0; try { - return Double.parseDouble(_cell.getV()); + return Double.parseDouble(v); } catch(NumberFormatException e) { throw typeMismatch(CELL_TYPE_NUMERIC, CELL_TYPE_STRING, false); } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java index b54da4f28b..93d906f5e3 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java @@ -1628,6 +1628,31 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { saveAndReloadReport(wb, xlsOutput); } + /** + * XSSFCell.typeMismatch on certain blank cells when formatting + * with DataFormatter + */ + @Test + public void bug56702() throws Exception { + XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56702.xlsx"); + + Sheet sheet = wb.getSheetAt(0); + + // Get wrong cell by row 8 & column 7 + Cell cell = sheet.getRow(8).getCell(7); + assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType()); + + // Check the value - will be zero as it is + assertEquals(0.0, cell.getNumericCellValue(), 0.001); + + // Try to format + DataFormatter formatter = new DataFormatter(); + formatter.formatCellValue(cell); + + // Check the formatting + assertEquals("0", formatter.formatCellValue(cell)); + } + private void saveAndReloadReport(Workbook wb, File outFile) throws IOException { // run some method on the font to verify if it is "disconnected" already //for(short i = 0;i < 256;i++) diff --git a/test-data/spreadsheet/56702.xlsx b/test-data/spreadsheet/56702.xlsx new file mode 100644 index 0000000000..c17a0bca8b Binary files /dev/null and b/test-data/spreadsheet/56702.xlsx differ