git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1694536 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
David North 2015-08-06 16:37:09 +00:00
parent 9260284a0a
commit 10f3a8fff3
2 changed files with 62 additions and 1 deletions

View File

@ -0,0 +1,57 @@
package org.apache.poi.xssf.streaming.examples;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable;
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler;
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler.SheetContentsHandler;
import org.apache.poi.xssf.usermodel.XSSFComment;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
import org.xml.sax.SAXException;
/**
* This demonstrates how a hybrid approach to workbook read can be taken, using
*/
public class HybridStreaming {
private static final String SHEET_TO_STREAM = "large sheet";
public static void main(String[] args) throws IOException, SAXException {
InputStream sourceBytes = new FileInputStream("/path/too/workbook.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(sourceBytes) {
/** Avoid DOM parse of large sheet */
public void parseSheet(java.util.Map<String,XSSFSheet> shIdMap, CTSheet ctSheet) {
if (SHEET_TO_STREAM.equals(ctSheet.getName())) {
return;
}
};
};
// Having avoided a DOM-based parse of the sheet, we can stream it instead.
ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(workbook.getPackage());
new XSSFSheetXMLHandler(workbook.getStylesSource(), strings, createSheetContentsHandler(), false);
workbook.close();
}
private static SheetContentsHandler createSheetContentsHandler() {
return new SheetContentsHandler() {
public void startRow(int rowNum) {
}
public void headerFooter(String text, boolean isHeader, String tagName) {
}
public void endRow(int rowNum) {
}
public void cell(String cellReference, String formattedValue, XSSFComment comment) {
}
};
}
}

View File

@ -401,7 +401,11 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
} }
} }
protected void parseSheet(Map<String, XSSFSheet> shIdMap, CTSheet ctSheet) { /**
* Not normally to be called externally, but possibly to be overridden to avoid
* the DOM based parse of large sheets (see examples).
*/
public void parseSheet(Map<String, XSSFSheet> shIdMap, CTSheet ctSheet) {
XSSFSheet sh = shIdMap.get(ctSheet.getId()); XSSFSheet sh = shIdMap.get(ctSheet.getId());
if(sh == null) { if(sh == null) {
logger.log(POILogger.WARN, "Sheet with name " + ctSheet.getName() + " and r:id " + ctSheet.getId()+ " was defined, but didn't exist in package, skipping"); logger.log(POILogger.WARN, "Sheet with name " + ctSheet.getName() + " and r:id " + ctSheet.getId()+ " was defined, but didn't exist in package, skipping");