diff --git a/src/main/java/com/github/pjfanning/xlsx/impl/BaseEvaluationWorkbook.java b/src/main/java/com/github/pjfanning/xlsx/impl/BaseEvaluationWorkbook.java index e4937348..f25b9cbd 100644 --- a/src/main/java/com/github/pjfanning/xlsx/impl/BaseEvaluationWorkbook.java +++ b/src/main/java/com/github/pjfanning/xlsx/impl/BaseEvaluationWorkbook.java @@ -15,6 +15,8 @@ import org.apache.poi.xssf.usermodel.XSSFName; import org.apache.poi.xssf.usermodel.XSSFTable; +import static com.github.pjfanning.xlsx.impl.NumberUtil.parseInt; + /** * Copied from POI BaseXSSFEvaluationWorkbook but a lot of stuff is removed because it is not easy * or impossible to support in excel-streaming-reader @@ -78,7 +80,7 @@ private int resolveBookIndex(String bookName) { // Is it already in numeric form? try { - return Integer.parseInt(bookName); + return parseInt(bookName); } catch (NumberFormatException e) {} // Not properly referenced diff --git a/src/main/java/com/github/pjfanning/xlsx/impl/NumberUtil.java b/src/main/java/com/github/pjfanning/xlsx/impl/NumberUtil.java new file mode 100644 index 00000000..5c0286ab --- /dev/null +++ b/src/main/java/com/github/pjfanning/xlsx/impl/NumberUtil.java @@ -0,0 +1,12 @@ +package com.github.pjfanning.xlsx.impl; + +final class NumberUtil { + + static int parseInt(final String s) { + return Integer.parseInt(s.trim()); + } + + static double parseDouble(final String s) { + return Double.parseDouble(s.trim()); + } +} diff --git a/src/main/java/com/github/pjfanning/xlsx/impl/StreamingCell.java b/src/main/java/com/github/pjfanning/xlsx/impl/StreamingCell.java index 4e019585..799cdaec 100644 --- a/src/main/java/com/github/pjfanning/xlsx/impl/StreamingCell.java +++ b/src/main/java/com/github/pjfanning/xlsx/impl/StreamingCell.java @@ -13,6 +13,8 @@ import java.util.Calendar; import java.util.Date; +import static com.github.pjfanning.xlsx.impl.NumberUtil.parseDouble; + public class StreamingCell implements Cell { private static final Supplier NULL_SUPPLIER = () -> null; @@ -197,7 +199,7 @@ public double getNumericCellValue() { } } } - return rawContents == null ? 0.0 : Double.parseDouble(rawContents); + return rawContents == null ? 0.0 : parseDouble(rawContents); } /** diff --git a/src/main/java/com/github/pjfanning/xlsx/impl/StreamingRowIterator.java b/src/main/java/com/github/pjfanning/xlsx/impl/StreamingRowIterator.java index 99756281..808e149a 100644 --- a/src/main/java/com/github/pjfanning/xlsx/impl/StreamingRowIterator.java +++ b/src/main/java/com/github/pjfanning/xlsx/impl/StreamingRowIterator.java @@ -38,6 +38,9 @@ import java.time.LocalDateTime; import java.util.*; +import static com.github.pjfanning.xlsx.impl.NumberUtil.parseDouble; +import static com.github.pjfanning.xlsx.impl.NumberUtil.parseInt; + class StreamingRowIterator implements CloseableIterator { private static final Logger LOG = LoggerFactory.getLogger(StreamingRowIterator.class); private static final QName QNAME_HIDDEN = QName.valueOf("hidden"); @@ -144,7 +147,7 @@ && isSpreadsheetTag(event.asStartElement().getName())) { Attribute rowNumAttr = startElement.getAttributeByName(QNAME_R); int rowIndex = currentRowNum; if (rowNumAttr != null) { - rowIndex = Integer.parseInt(rowNumAttr.getValue()) - 1; + rowIndex = parseInt(rowNumAttr.getValue()) - 1; currentRowNum = rowIndex; } Attribute isHiddenAttr = startElement.getAttributeByName(QNAME_HIDDEN); @@ -166,7 +169,7 @@ && isSpreadsheetTag(event.asStartElement().getName())) { if (styleAttr != null) { String indexStr = styleAttr.getValue(); try { - int index = Integer.parseInt(indexStr); + final int index = parseInt(indexStr); currentRow.setRowStyle(stylesTable.getStyleAt(index)); } catch (NumberFormatException nfe) { LOG.warn("Ignoring invalid row style index {}", indexStr); @@ -189,8 +192,8 @@ && isSpreadsheetTag(event.asStartElement().getName())) { if (isHidden || width >= 0) { Attribute minAttr = startElement.getAttributeByName(QNAME_MIN); Attribute maxAttr = startElement.getAttributeByName(QNAME_MAX); - int min = Integer.parseInt(minAttr.getValue()) - 1; - int max = Integer.parseInt(maxAttr.getValue()) - 1; + int min = parseInt(minAttr.getValue()) - 1; + int max = parseInt(maxAttr.getValue()) - 1; for (int columnIndex = min; columnIndex <= max; columnIndex++) { if (isHidden) hiddenColumns.add(columnIndex); if (width >= 0) columnWidths.put(columnIndex, width); @@ -226,7 +229,7 @@ && isSpreadsheetTag(event.asStartElement().getName())) { if (style != null) { String indexStr = style.getValue(); try { - int index = Integer.parseInt(indexStr); + final int index = Integer.parseInt(indexStr); currentCell.setCellStyle(stylesTable.getStyleAt(index)); } catch (NumberFormatException nfe) { LOG.warn("Ignoring invalid style index {}", indexStr); @@ -249,7 +252,7 @@ && isSpreadsheetTag(event.asStartElement().getName())) { for (int i = ref.length() - 1; i >= 0; i--) { if (!Character.isDigit(ref.charAt(i))) { try { - streamingSheetReader.setLastRowNum(Integer.parseInt(ref.substring(i + 1)) - 1); + streamingSheetReader.setLastRowNum(parseInt(ref.substring(i + 1)) - 1); } catch (NumberFormatException ignore) { } break; @@ -467,7 +470,7 @@ private void parseSheetFormatPr(final StartElement startElement) { final Attribute baseColWidthAtt = startElement.getAttributeByName(QName.valueOf("baseColWidth")); if (baseColWidthAtt != null) { try { - streamingSheetReader.setBaseColWidth(Integer.parseInt(baseColWidthAtt.getValue())); + streamingSheetReader.setBaseColWidth(parseInt(baseColWidthAtt.getValue())); } catch (Exception e) { LOG.warn("unable to parse baseColWidth {}", baseColWidthAtt.getValue()); } @@ -504,7 +507,7 @@ private Supplier getFormatterForType(String type) { switch(type) { case "s": //string stored in shared table if (!lastContents.isEmpty()) { - int idx = Integer.parseInt(lastContents); + final int idx = parseInt(lastContents); if (!getBuilder().fullFormatRichText() && sst instanceof SharedStringsTableBase) { return new LazySupplier<>(() -> ((SharedStringsTableBase)sst).getString(idx)); } @@ -525,7 +528,7 @@ private Supplier getFormatterForType(String type) { final String currentNumericFormat = currentCell.getNumericFormat(); return new LazySupplier<>(() -> dataFormatter.formatRawCellContents( - Double.parseDouble(lastContents), + parseDouble(lastContents), currentNumericFormatIndex, currentNumericFormat)); @@ -580,7 +583,7 @@ private String unformattedContents(Supplier formattedContentSupplier) { return formattedContent.toString(); } if (!lastContents.isEmpty()) { - int idx = Integer.parseInt(lastContents); + final int idx = parseInt(lastContents); if (sst == null) throw new NullPointerException("sst is null"); if (sst instanceof SharedStringsTableBase) { return ((SharedStringsTableBase)sst).getString(idx); diff --git a/src/main/java/com/github/pjfanning/xlsx/impl/StreamingSheetReader.java b/src/main/java/com/github/pjfanning/xlsx/impl/StreamingSheetReader.java index aae7f909..2dc79523 100644 --- a/src/main/java/com/github/pjfanning/xlsx/impl/StreamingSheetReader.java +++ b/src/main/java/com/github/pjfanning/xlsx/impl/StreamingSheetReader.java @@ -37,6 +37,8 @@ import java.io.IOException; import java.util.*; +import static com.github.pjfanning.xlsx.impl.NumberUtil.parseInt; + public class StreamingSheetReader implements Iterable { private static final Logger LOG = LoggerFactory.getLogger(StreamingSheetReader.class); @@ -200,7 +202,7 @@ void setFormatString(StartElement startElement, StreamingCell cell) { if (stylesTable != null) { if(cellStyleString != null) { - style = stylesTable.getStyleAt(Integer.parseInt(cellStyleString)); + style = stylesTable.getStyleAt(parseInt(cellStyleString)); } else if(stylesTable.getNumCellStyles() > 0) { style = stylesTable.getStyleAt(0); } diff --git a/src/test/java/com/github/pjfanning/xlsx/StreamingWorkbookTest.java b/src/test/java/com/github/pjfanning/xlsx/StreamingWorkbookTest.java index 4be94d71..95b8abe0 100644 --- a/src/test/java/com/github/pjfanning/xlsx/StreamingWorkbookTest.java +++ b/src/test/java/com/github/pjfanning/xlsx/StreamingWorkbookTest.java @@ -866,6 +866,18 @@ public void testSheetReadWrongOrder() throws Exception { } } + @Test + public void testExtraWhitespace() throws Exception { + try (Workbook workbook = openWorkbook("extra_whitespace.xlsx")) { + Sheet sheet = workbook.getSheetAt(0); + for(Row row : sheet) { + for(Cell cell : row) { + + } + } + } + } + private void validateFormatsSheet(Sheet sheet) throws IOException { Iterator rowIterator = sheet.rowIterator(); diff --git a/src/test/resources/extra_whitespace.xlsx b/src/test/resources/extra_whitespace.xlsx new file mode 100644 index 00000000..41356f38 Binary files /dev/null and b/src/test/resources/extra_whitespace.xlsx differ