aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid North <dnorth@apache.org>2015-08-06 16:37:09 +0000
committerDavid North <dnorth@apache.org>2015-08-06 16:37:09 +0000
commit10f3a8fff34694d79f8267518bdc97a47af6f25b (patch)
tree6e13aed4b6277e9b70dedeaeec0998991ce4e0fd
parent9260284a0a63f7b6e88f6ad0d9f06cdd714966a4 (diff)
downloadpoi-10f3a8fff34694d79f8267518bdc97a47af6f25b.tar.gz
poi-10f3a8fff34694d79f8267518bdc97a47af6f25b.zip
Example to go with refactor for https://bz.apache.org/bugzilla/show_bug.cgi?id=58219
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1694536 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--src/examples/src/org/apache/poi/xssf/streaming/examples/HybridStreaming.java57
-rw-r--r--src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java6
2 files changed, 62 insertions, 1 deletions
diff --git a/src/examples/src/org/apache/poi/xssf/streaming/examples/HybridStreaming.java b/src/examples/src/org/apache/poi/xssf/streaming/examples/HybridStreaming.java
new file mode 100644
index 0000000000..99af0356a1
--- /dev/null
+++ b/src/examples/src/org/apache/poi/xssf/streaming/examples/HybridStreaming.java
@@ -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) {
+ }
+ };
+ }
+
+}
diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
index cb9ba9a5fe..4b4a30e0b6 100644
--- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
+++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
@@ -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());
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");