Don't create an error-log when slightly broken spreadsheets contain an empty value for attribute "s"

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1897331 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2022-01-22 08:57:43 +00:00
parent bba249d522
commit 7f1d843752
2 changed files with 46 additions and 14 deletions

View 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,14 +413,15 @@ public class XSSFSheetXMLHandler extends DefaultHandler {
case SST_STRING:
String sstIndex = value.toString();
if (sstIndex.length() > 0) {
try {
int idx = Integer.parseInt(sstIndex);
RichTextString rtss = sharedStringsTable.getItemAt(idx);
thisStr = rtss.toString();
}
catch (NumberFormatException ex) {
} catch (NumberFormatException ex) {
LOG.atError().withThrowable(ex).log("Failed to parse SST index '{}'", sstIndex);
}
}
break;
case NUMBER:

View 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));
}
}
}
}