diff --git a/src/main/java/com/monitorjbl/xlsx/impl/StreamingSheetReader.java b/src/main/java/com/monitorjbl/xlsx/impl/StreamingSheetReader.java index bb8154e6..5cea30df 100644 --- a/src/main/java/com/monitorjbl/xlsx/impl/StreamingSheetReader.java +++ b/src/main/java/com/monitorjbl/xlsx/impl/StreamingSheetReader.java @@ -189,7 +189,9 @@ && isSpreadsheetTag(event.asStartElement().getName())) { } } } else if("f".equals(tagLocalName)) { - currentCell.setType("str"); + if (currentCell != null) { + currentCell.setType("str"); + } } // Clear contents cache @@ -207,9 +209,12 @@ && isSpreadsheetTag(event.asEndElement().getName())) { currentRowNum++; } else if("c".equals(tagLocalName)) { currentRow.getCellMap().put(currentCell.getColumnIndex(), currentCell); + currentCell = null; currentColNum++; } else if("f".equals(tagLocalName)) { - currentCell.setFormula(lastContents); + if (currentCell != null) { + currentCell.setFormula(lastContents); + } } } diff --git a/src/test/java/com/monitorjbl/xlsx/StreamingReaderTest.java b/src/test/java/com/monitorjbl/xlsx/StreamingReaderTest.java index 853ada39..42084b96 100644 --- a/src/test/java/com/monitorjbl/xlsx/StreamingReaderTest.java +++ b/src/test/java/com/monitorjbl/xlsx/StreamingReaderTest.java @@ -4,6 +4,7 @@ import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.PackageAccess; import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Workbook; @@ -671,4 +672,25 @@ public void testShouldHandleBlankSSTReference() throws Exception { } } } + + // The last cell on this sheet should be a NUMERIC but there is a lingering "f" + // tag that was getting attached to the last cell causing it to be a FORUMLA. + @Test + public void testForumulaOutsideCellIgnored() throws Exception { + try( + InputStream is = new FileInputStream(new File("src/test/resources/formula_outside_cell.xlsx")); + Workbook wb = StreamingReader.builder().open(is); + ) { + Iterator rows = wb.getSheetAt(0).iterator(); + Cell cell = null; + while(rows.hasNext()) { + Iterator cells = rows.next().iterator(); + while(cells.hasNext()) { + cell = cells.next(); + } + } + assertNotNull(cell); + assertThat(cell.getCellTypeEnum(), is(CellType.NUMERIC)); + } + } } diff --git a/src/test/resources/formula_outside_cell.xlsx b/src/test/resources/formula_outside_cell.xlsx new file mode 100644 index 00000000..100b8383 Binary files /dev/null and b/src/test/resources/formula_outside_cell.xlsx differ