diff options
author | David North <dnorth@apache.org> | 2015-08-06 16:37:09 +0000 |
---|---|---|
committer | David North <dnorth@apache.org> | 2015-08-06 16:37:09 +0000 |
commit | 10f3a8fff34694d79f8267518bdc97a47af6f25b (patch) | |
tree | 6e13aed4b6277e9b70dedeaeec0998991ce4e0fd /src/examples | |
parent | 9260284a0a63f7b6e88f6ad0d9f06cdd714966a4 (diff) | |
download | poi-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
Diffstat (limited to 'src/examples')
-rw-r--r-- | src/examples/src/org/apache/poi/xssf/streaming/examples/HybridStreaming.java | 57 |
1 files changed, 57 insertions, 0 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) { + } + }; + } + +} |