]> source.dussan.org Git - poi.git/commitdiff
Don't create an error-log when slightly broken spreadsheets contain an empty value...
authorDominik Stadler <centic@apache.org>
Sat, 22 Jan 2022 08:57:43 +0000 (08:57 +0000)
committerDominik Stadler <centic@apache.org>
Sat, 22 Jan 2022 08:57:43 +0000 (08:57 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1897331 13f79535-47bb-0310-9956-ffa450edef68

poi-ooxml/src/main/java/org/apache/poi/xssf/eventusermodel/XSSFSheetXMLHandler.java
poi-ooxml/src/test/java/org/apache/poi/xssf/eventusermodel/TestXSSFSheetXMLHandler.java

index 86c4dcfe783547265f56e1df098558a09f0d024f..d6b66c95781f44951fc5449574aa06c40f123a1c 100644 (file)
@@ -67,18 +67,18 @@ public class XSSFSheetXMLHandler extends DefaultHandler {
    /**
     * Table with the styles used for formatting
     */
-   private Styles stylesTable;
+   private final Styles stylesTable;
 
    /**
     * Table with cell comments
     */
-   private Comments comments;
+   private final Comments comments;
 
    /**
     * Read only access to the shared strings table, for looking
     *  up (most) string cell's contents
     */
-   private SharedStrings sharedStringsTable;
+   private final SharedStrings sharedStringsTable;
 
    /**
     * Where our text is going
@@ -105,12 +105,12 @@ public class XSSFSheetXMLHandler extends DefaultHandler {
    private int rowNum;
    private int nextRowNum;      // some sheets do not have rowNums, Excel can read them so we should try to handle them correctly as well
    private String cellRef;
-   private boolean formulasNotResults;
+   private final boolean formulasNotResults;
 
    // Gathers characters as they are seen.
-   private StringBuilder value = new StringBuilder(64);
-   private StringBuilder formula = new StringBuilder(64);
-   private StringBuilder headerFooter = new StringBuilder(64);
+   private final StringBuilder value = new StringBuilder(64);
+   private final StringBuilder formula = new StringBuilder(64);
+   private final StringBuilder headerFooter = new StringBuilder(64);
 
    private Queue<CellAddress> commentCellRefs;
 
@@ -413,13 +413,14 @@ public class XSSFSheetXMLHandler extends DefaultHandler {
 
            case SST_STRING:
                String sstIndex = value.toString();
-               try {
-                   int idx = Integer.parseInt(sstIndex);
-                   RichTextString rtss = sharedStringsTable.getItemAt(idx);
-                   thisStr = rtss.toString();
-               }
-               catch (NumberFormatException ex) {
-                   LOG.atError().withThrowable(ex).log("Failed to parse SST index '{}'", sstIndex);
+               if (sstIndex.length() > 0) {
+                   try {
+                       int idx = Integer.parseInt(sstIndex);
+                       RichTextString rtss = sharedStringsTable.getItemAt(idx);
+                       thisStr = rtss.toString();
+                   } catch (NumberFormatException ex) {
+                       LOG.atError().withThrowable(ex).log("Failed to parse SST index '{}'", sstIndex);
+                   }
                }
                break;
 
index eaea83d97222e7fbf0359702120f7d18b89dd028..0ddf7bf678f54487f4021a25f590f49e854c0e50 100644 (file)
@@ -68,4 +68,35 @@ public class TestXSSFSheetXMLHandler {
             }
         }
     }
+
+    @Test
+    public void testNumber() throws Exception {
+        try (OPCPackage xlsxPackage = OPCPackage.open(_ssTests.openResourceAsStream("sample.xlsx"))) {
+            final XSSFReader reader = new XSSFReader(xlsxPackage);
+
+            final Iterator<InputStream> iter = reader.getSheetsData();
+
+            try (InputStream stream = iter.next()) {
+                final XMLReader sheetParser = XMLHelper.getSaxParserFactory().newSAXParser().getXMLReader();
+
+                sheetParser.setContentHandler(new XSSFSheetXMLHandler(reader.getStylesTable(),
+                        new ReadOnlySharedStringsTable(xlsxPackage), new SheetContentsHandler() {
+                    @Override
+                    public void startRow(final int rowNum) {
+                    }
+
+                    @Override
+                    public void endRow(final int rowNum) {
+                    }
+
+                    @Override
+                    public void cell(final String cellReference, final String formattedValue,
+                                     final XSSFComment comment) {
+                    }
+                }, false));
+
+                sheetParser.parse(new InputSource(stream));
+            }
+        }
+    }
 }