mirror of https://github.com/apache/poi.git
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
This commit is contained in:
parent
9260284a0a
commit
10f3a8fff3
|
@ -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) {
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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");
|
||||||
|
|
Loading…
Reference in New Issue