InputStream workbookDataInputStream = null;
XMLStreamReader workbookStreamReader = null;
try {
final OPCPackage opcPackage = OPCPackage.open(inputStream);
final XSSFReader xssfReader = new XSSFReader(opcPackage);
workbookDataInputStream = xssfReader.getWorkbookData();
final XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
workbookStreamReader = xmlInputFactory.createXMLStreamReader(workbookDataInputStream);
sharedStringsTable = xssfReader.getSharedStringsTable();
/*
sample sheet element:
<sheets>
<sheet name="Movies" sheetId="1" state="visible" r:id="rId2"/>
<sheet name="Person" sheetId="2" state="visible" r:id="rId3"/>
</sheets>
*/
while (workbookStreamReader.hasNext()) {
if (workbookStreamReader.next() == XMLStreamConstants.START_ELEMENT && "sheet".equals(workbookStreamReader.getLocalName())) {
final String shn = workbookStreamReader.getAttributeValue(null, "name");
final String shId = workbookStreamReader.getAttributeValue(null, "sheetId");
if ((sheetName != null && sheetName.equals(shn)) ||
(sheetName == null && String.valueOf(this.sheetIndex + 1).equals(shId))) {
//this is the target sheet
final String relationshipId = workbookStreamReader.getAttributeValue(schemaRelationships, "id");
sheetInputStream = xssfReader.getSheet(relationshipId);
sheetStreamReader = xmlInputFactory.createXMLStreamReader(sheetInputStream);
break;
}
}
}