import org.apache.xmlbeans.XmlException;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.STSheetState;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.WorkbookDocument;
/**
//step 2. Read array of CTSheet elements, wrap it in a ArayList and construct an iterator
//Note, using XMLBeans might be expensive, consider refactoring to use SAX or a plain regexp search
CTWorkbook wbBean = WorkbookDocument.Factory.parse(wb.getInputStream(), DEFAULT_XML_OPTIONS).getWorkbook();
- sheetIterator = wbBean.getSheets().getSheetList().iterator();
+ List<CTSheet> validSheets = new ArrayList<CTSheet>();
+ for (CTSheet ctSheet : wbBean.getSheets().getSheetList()) {
+ //if there's no relationship id, silently skip the sheet
+ if ("".equals(ctSheet.getId())) {
+ //skip it
+ } else {
+ validSheets.add(ctSheet);
+ }
+ }
+ sheetIterator = validSheets.iterator();
} catch (InvalidFormatException e){
throw new POIXMLException(e);
} catch (XmlException e){
pkg.close();
}
+
+ /**
+ * NPE when sheet has no relationship id in the workbook
+ * 60825
+ */
+ public void testSheetWithNoRelationshipId() throws Exception {
+ OPCPackage pkg = XSSFTestDataSamples.openSamplePackage("60825.xlsx");
+ ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(pkg);
+ assertNotNull(strings);
+ XSSFReader reader = new XSSFReader(pkg);
+ StylesTable styles = reader.getStylesTable();
+ assertNotNull(styles);
+
+ XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) reader.getSheetsData();
+ assertNotNull(iter.next());
+ assertFalse(iter.hasNext());
+
+ pkg.close();
+ }
}