poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dno...@apache.org
Subject svn commit: r1694536 - in /poi/trunk/src: examples/src/org/apache/poi/xssf/streaming/examples/HybridStreaming.java ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
Date Thu, 06 Aug 2015 16:37:09 GMT
Author: dnorth
Date: Thu Aug  6 16:37:09 2015
New Revision: 1694536

URL: http://svn.apache.org/r1694536
Log:
Example to go with refactor for https://bz.apache.org/bugzilla/show_bug.cgi?id=58219

Added:
    poi/trunk/src/examples/src/org/apache/poi/xssf/streaming/examples/HybridStreaming.java
Modified:
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java

Added: poi/trunk/src/examples/src/org/apache/poi/xssf/streaming/examples/HybridStreaming.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xssf/streaming/examples/HybridStreaming.java?rev=1694536&view=auto
==============================================================================
--- poi/trunk/src/examples/src/org/apache/poi/xssf/streaming/examples/HybridStreaming.java
(added)
+++ poi/trunk/src/examples/src/org/apache/poi/xssf/streaming/examples/HybridStreaming.java
Thu Aug  6 16:37:09 2015
@@ -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)
{
+            }
+        };
+    }
+
+}

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java?rev=1694536&r1=1694535&r2=1694536&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java Thu Aug  6 16:37:09
2015
@@ -401,7 +401,11 @@ public class XSSFWorkbook extends POIXML
         }
     }
 
-    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");



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message