diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/reader/StreamedCell.java b/src/ooxml/java/org/apache/poi/xssf/streaming/reader/StreamedCell.java new file mode 100644 index 0000000..bf815c4 --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/reader/StreamedCell.java @@ -0,0 +1,490 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.xssf.streaming.reader; + +import java.util.Calendar; +import java.util.Date; + +import org.apache.poi.ss.formula.FormulaParseException; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.CellType; +import org.apache.poi.ss.usermodel.Comment; +import org.apache.poi.ss.usermodel.Hyperlink; +import org.apache.poi.ss.usermodel.RichTextString; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.util.CellAddress; +import org.apache.poi.ss.util.CellRangeAddress; + +/** + * Represents cell in a row + * Value of cell is represented as a string. + * + */ +public class StreamedCell implements Cell{ + private String value; + private int cellNumber; + private CellType cellType; + + public StreamedCell(){ + + } + + /** + *
+     * Return cell value
+     * 
+ * Return the value of a cell in String format. + * Value will be same as how it is represented in excel. + * @return String + */ + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + public String toString(){ + return value; + } + + /** + *
+     * Returns the cell number
+     * 
+ * + * @return int, that represents cell number + */ + public int getCellNumber() { + return cellNumber; + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     *
+ * @param cellNumber + */ + public void setCellNumber(int cellNumber) { + this.cellNumber = cellNumber; + } + + /** + *
+     * Will be supported in future.
+     *  
+ * @exception UnsupportedOperationException + */ + public int getColumnIndex() { + throw new UnsupportedOperationException("Not implememted yet."); + } + + /** + *
+     * Will be supported in future.
+     *  
+ * @exception UnsupportedOperationException + */ + public int getRowIndex() { + throw new UnsupportedOperationException("Not implememted yet."); + } + + /** + *
+     * Will be supported in future.
+     *  
+ * @exception UnsupportedOperationException + */ + public Sheet getSheet() { + throw new UnsupportedOperationException("Not implememted yet."); + } + + /** + *
+     * Will be supported in future.
+     *  
+ * @exception UnsupportedOperationException + */ + public Row getRow() { + throw new UnsupportedOperationException("Not implememted yet."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     *
+ * @param cellType + * @exception UnsupportedOperationException + */ + public void setCellType(int cellType) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     *
+ * @param cellType + */ + public void setCellType(CellType cellType) { + this.cellType = cellType; + } + + /** + *
+     * Will be supported in future.
+     *  
+ * @exception UnsupportedOperationException + */ + public int getCellType() { + throw new UnsupportedOperationException("Not implememted yet."); + } + + /** + *
+     * Return the cell type.
+     *  
+ * + * @return CellType + */ + public CellType getCellTypeEnum() { + return cellType; + } + + /** + *
+     * Will be supported in future.
+     *  
+ * @exception UnsupportedOperationException + */ + public int getCachedFormulaResultType() { + throw new UnsupportedOperationException("Not implememted yet."); + } + + /** + *
+     * Will be supported in future.
+     *  
+ * @exception UnsupportedOperationException + */ + public CellType getCachedFormulaResultTypeEnum() { + throw new UnsupportedOperationException("Not implememted yet."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     *
+ * @param value + * @exception UnsupportedOperationException + */ + public void setCellValue(double value) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     *
+ * @param value + * @exception UnsupportedOperationException + */ + public void setCellValue(Date value) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     *
+ *@exception UnsupportedOperationException + */ + public void setCellValue(Calendar value) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     *
+ *@exception UnsupportedOperationException + */ + public void setCellValue(RichTextString value) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     *
+ *@exception UnsupportedOperationException + */ + public void setCellValue(String value) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     *
+ *@exception UnsupportedOperationException + */ + public void setCellFormula(String formula) throws FormulaParseException { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory foot print.
+     *  
+ * @exception UnsupportedOperationException + */ + public String getCellFormula() { + throw new UnsupportedOperationException("Not implememted yet."); + } + + /** + *
+     * Not supported due to memory foot print.
+     *  
+ * @exception UnsupportedOperationException + */ + public double getNumericCellValue() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory foot print.
+     *  
+ * @exception UnsupportedOperationException + */ + public Date getDateCellValue() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Will be supported in future.
+     *  
+ * @exception UnsupportedOperationException + */ + public RichTextString getRichStringCellValue() { + throw new UnsupportedOperationException("Not implememted yet."); + } + + /** + *
+     *  Returns the String value of cell content
+     *  
+ * @return String, representing the value of cell + */ + public String getStringCellValue() { + return value; + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     *
+ *@exception UnsupportedOperationException + * + */ + public void setCellValue(boolean value) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     *
+ * @exception UnsupportedOperationException + */ + public void setCellErrorValue(byte value) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory foot print.
+     *  
+ * @exception UnsupportedOperationException + */ + public boolean getBooleanCellValue() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Will be supported in future.
+     *  
+ * @exception UnsupportedOperationException + */ + public byte getErrorCellValue() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     *
+ * @exception UnsupportedOperationException + */ + public void setCellStyle(CellStyle style) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Will be supported in future.
+     *  
+ * @exception UnsupportedOperationException + */ + public CellStyle getCellStyle() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     *
+ * @exception UnsupportedOperationException + */ + public void setAsActiveCell() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Will be supported in future.
+     *  
+ * @exception UnsupportedOperationException + */ + public CellAddress getAddress() { + throw new UnsupportedOperationException("Not implememted yet."); + } + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     *
+ *@exception UnsupportedOperationException + * + */ + public void setCellComment(Comment comment) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint
+     *  
+ * @exception UnsupportedOperationException + */ + public Comment getCellComment() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     *
+ * @exception UnsupportedOperationException + */ + public void removeCellComment() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Will be supported in future.
+     *  
+ * @exception UnsupportedOperationException + */ + public Hyperlink getHyperlink() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     *
+ *@exception UnsupportedOperationException + * + */ + public void setHyperlink(Hyperlink link) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     *
+ * @exception UnsupportedOperationException + */ + public void removeHyperlink() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Will be supported in future.
+     *  
+ * @exception UnsupportedOperationException + */ + public CellRangeAddress getArrayFormulaRange() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Will be supported in future.
+     *  
+ * @exception UnsupportedOperationException + */ + public boolean isPartOfArrayFormulaGroup() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /*@Override + protected void finalize() throws Throwable { + super.finalize(); + value = null; + }*/ + + + +} diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/reader/StreamedRow.java b/src/ooxml/java/org/apache/poi/xssf/streaming/reader/StreamedRow.java new file mode 100644 index 0000000..d3768b5 --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/reader/StreamedRow.java @@ -0,0 +1,384 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.xssf.streaming.reader; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.CellType; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; + + +/** + * Represents an excel row. + * Supports only minimal functionality in order to reduce memory consumption. + * + */ +public class StreamedRow implements Row{ + private List cells; + private int rowNumber; + + public StreamedRow(int rowNumber) { + this.rowNumber = rowNumber; + } + + /** + *
+     * Used to get cells of a Row
+     * 
+ * @return Iterator + */ + public Iterator getCellIterator() { + if (cells == null) { + cells = new ArrayList(); + } + return cells.iterator(); + } + + /** + *
+     *  Returns the row number
+     * 
+ * @return int + */ + public int getRowNum() { + return rowNumber; + } + + public void setRowNum(int rowNumber) { + this.rowNumber = rowNumber; + } + + /** + *
+     * Returns String representation of row.
+     * o/p format[Row Number: --> ||.....|]
+     * 
+ * @return String + */ + public String toString() { + StringBuffer sb = new StringBuffer(250); + sb.append("Row Number:").append(rowNumber); + sb.append(" --> "); + if (cells != null) { + for (StreamedCell cell : cells) { + sb.append(cell.toString()); + sb.append(" | "); + } + } + + return sb.toString(); + } + +/* @Override + protected void finalize() throws Throwable { + super.finalize(); + if (cells != null) { + cells.clear(); + cells = null; + } + }*/ + + /** + *
+     * Retuns the List of Cells contained in the row.
+     * 
+ * @return List + */ + public List getCells() { + if (cells == null) { + cells = new ArrayList(); + } + return cells; + } + +/** + *
+ * Will not be supported. Use getCellIterator() instead
+ * 
+ * @exception UnsupportedOperationException + */ +public Iterator iterator() { + throw new UnsupportedOperationException("Operation not supported. Use getCellIterator() instead."); +} + +/** + *
+ * Not supported right now, as StreamedWorkbook
+ * supports only reading.
+ * 
+ * @exception UnsupportedOperationException + * + */ +public Cell createCell(int column) { + throw new UnsupportedOperationException("Operation not supported."); +} + +/** + *
+ * Not supported right now, as StreamedWorkbook
+ * supports only reading.
+ * 
+ * @exception UnsupportedOperationException + */ +public Cell createCell(int column, int type) { + throw new UnsupportedOperationException("Operation not supported."); +} + +/** + *
+ * Not supported right now, as StreamedWorkbook
+ * supports only reading.
+ * 
+ * @exception UnsupportedOperationException + */ +public Cell createCell(int column, CellType type) { + throw new UnsupportedOperationException("Operation not supported."); +} + +/** + *
+ * Not supported right now, as StreamedWorkbook
+ * supports only reading.
+ * 
+ * @exception UnsupportedOperationException + */ +public void removeCell(Cell cell) { + throw new UnsupportedOperationException("Operation not supported."); +} + +/** + *
+ *  Returns the cell on specified cell number
+ * 
+ * @return Cell + */ +public Cell getCell(int cellnum) { + StreamedCell cell = null; + + if(cells != null){ + cell = cells.get(cellnum); + } + + return cell; +} + +/** + *
+ * Will be supported in future.
+ *  
+ * @exception UnsupportedOperationException + */ +public Cell getCell(int cellnum, MissingCellPolicy policy) { +/* StreamedCell cell = null; + + if(cells != null){ + cell = cells.get(cellnum); + } + + switch(policy){ + case RETURN_NULL_AND_BLANK: + return cell; + case RETURN_BLANK_AS_NULL: + boolean isBlank = (cell != null && cell.getCellTypeEnum() == CellType.BLANK); + return (isBlank) ? null : cell; + case CREATE_NULL_AS_BLANK: + return (cell == null) ? createCell(cellnum, CellType.BLANK) : cell; + default: + throw new IllegalArgumentException("Illegal policy " + policy + " (" + policy.id + ")"); + }*/ + throw new UnsupportedOperationException("Not implememted yet."); +} + +/** + *
+ * Get the number of the first cell contained in this row.
+ * 
+ * + * @return short representing the first logical cell in the row, + * or -1 if the row does not contain any cells. + */ +public short getFirstCellNum() { + short firstCellNumber = -1; + + if(cells != null && cells.size() > 0){ + firstCellNumber = (short)cells.get(0).getCellNumber(); + } + + return firstCellNumber; +} + +/** + *
+ * Gets the index of the last cell contained in this row PLUS ONE.
+ * 
+ * + * @return short representing the last logical cell in the row, + * or -1 if the row does not contain any cells. + */ +public short getLastCellNum() { + short lastCellNumber = -1; + + if(cells != null && cells.size() > 0){ + lastCellNumber = (short)(cells.get((cells.size()-1)).getCellNumber()+1); + } + + return lastCellNumber; +} + +/** + *
+ * Gets the number of defined cells (NOT number of cells in the actual row!).
+ * That is to say if only columns 0,4,5 have values then there would be 3.
+ * 
+ * @return int representing the number of defined cells in the row. + */ +public int getPhysicalNumberOfCells() { + if(cells != null)return cells.size(); + else return 0; +} + +/** + *
+ * Not supported right now, as StreamedWorkbook
+ * supports only reading.
+ * 
+ * @exception UnsupportedOperationException + */ +public void setHeight(short height) { + throw new UnsupportedOperationException("Operation not supported."); +} + +/** + *
+ * Not supported right now, as StreamedWorkbook
+ * supports only reading.
+ * 
+ * @exception UnsupportedOperationException + */ +public void setZeroHeight(boolean zHeight) { + throw new UnsupportedOperationException("Operation not supported."); +} + +/** + *
+ * Will be supported in future.
+ *  
+ * @exception UnsupportedOperationException + */ +public boolean getZeroHeight() { + throw new UnsupportedOperationException("Not implememted yet."); +} + +/** + *
+ * Not supported right now, as StreamedWorkbook
+ * supports only reading.
+ * 
+ * @exception UnsupportedOperationException + */ +public void setHeightInPoints(float height) { + throw new UnsupportedOperationException("Operation not supported."); +} + +/** + *
+ * Will be supported in future.
+ *  
+ * @exception UnsupportedOperationException + */ +public short getHeight() { + throw new UnsupportedOperationException("Not implememted yet."); +} + +/** + *
+ * Will be supported in future.
+ *  
+ * @exception UnsupportedOperationException + */ +public float getHeightInPoints() { + throw new UnsupportedOperationException("Not implememted yet."); +} + +/** + *
+ * Will be supported in future.
+ *  
+ * @exception UnsupportedOperationException + */ +public boolean isFormatted() { + throw new UnsupportedOperationException("Not implememted yet."); +} + +/** + *
+ * Will be supported in future.
+ *  
+ * @exception UnsupportedOperationException + */ +public CellStyle getRowStyle() { + throw new UnsupportedOperationException("Not implememted yet."); +} + +/** + *
+ * Not supported right now, as StreamedWorkbook
+ * supports only reading.
+ * 
+ * @exception UnsupportedOperationException + */ +public void setRowStyle(CellStyle style) { + throw new UnsupportedOperationException("Operation not supported."); +} + +/** + *
+ * Not supported right now. Use getCellIterator instead
+ * 
+ * @exception UnsupportedOperationException + */ +public Iterator cellIterator() { + throw new UnsupportedOperationException("Operation not supported.Use getCellIterator instead."); +} + +/** + *
+ * Will be supported in future.
+ *  
+ * @exception UnsupportedOperationException + */ +public Sheet getSheet() { + throw new UnsupportedOperationException("Operation not supported."); +} + +/** + *
+ * Will be supported in future.
+ *  
+ * @exception UnsupportedOperationException + */ +public int getOutlineLevel() { + throw new UnsupportedOperationException("Not implememted yet."); +} + + +} diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/reader/StreamedSheet.java b/src/ooxml/java/org/apache/poi/xssf/streaming/reader/StreamedSheet.java new file mode 100644 index 0000000..c202714 --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/reader/StreamedSheet.java @@ -0,0 +1,1528 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.xssf.streaming.reader; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import javax.xml.stream.XMLEventReader; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.events.XMLEvent; + +import org.apache.poi.ss.usermodel.AutoFilter; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellRange; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.Comment; +import org.apache.poi.ss.usermodel.DataValidation; +import org.apache.poi.ss.usermodel.DataValidationHelper; +import org.apache.poi.ss.usermodel.Drawing; +import org.apache.poi.ss.usermodel.Footer; +import org.apache.poi.ss.usermodel.Header; +import org.apache.poi.ss.usermodel.Hyperlink; +import org.apache.poi.ss.usermodel.PrintSetup; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.SheetConditionalFormatting; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.ss.util.CellAddress; +import org.apache.poi.ss.util.CellRangeAddress; +import org.apache.poi.ss.util.PaneInformation; +import org.apache.poi.xssf.model.SharedStringsTable; +import org.apache.poi.xssf.model.StylesTable; + +/** + * Represents an excel sheet. + * StreamedSheet currently supports only the minimal functionalities as + * additional features requires caching of sheet data, which consumes memory. + * + */ +public class StreamedSheet implements Sheet{ + private XMLEventReader xmlParser; + private SharedStringsTable sharedStringsTable; + private StylesTable stylesTable; + private int numberOfColumns; + private int sheetNumber; + private String sheetName; + private StreamedSheetEventHandler eventHandler = null; + + /** + *
+     * Fetch all rows from the excel.
+     * 
+ * + * This method consumes only less memory, but it is + * advisable to use it only for small excel files, since + * it will fetch all rows in a single call. + * + * @return Iterator + * @throws XMLStreamException + */ + public Iterator getAllRows() throws XMLStreamException{ + return getAllRows(this, sharedStringsTable, stylesTable); + } + + /** + *
+     * Used to fetch N number of rows.
+     * 
+ * + * Recommended method to reduce memory utilization. + * It allows to read big excel files in batch. + * This gives control to the user to process the records already fetched, + * before fetching next set of records. + * After reading N records, invoke the same method with number of rows to be + * fetched to get the next set of rows. + *
+ * + * *********************Usage**************************** + * Iterator rows = sheet.getNRows(1); + *
+ * while(rows.hasNext()){ //read the first 1 row + *
+ * StreamedRow row = rows.next(); + *
+ * } + *
+ * rows = sheet.getNRows(10); + *
+ * while(rows.hasNext()){ //read the next 10 rows + *
+ * StreamedRow row = rows.next(); + *
+ * } + * + * @param numberOfRows + * @return Iterator + * @throws XMLStreamException + */ + public Iterator getNRows(int numberOfRows) throws XMLStreamException{ + return getNRows(this, eventHandler, numberOfRows); + } + + public boolean hasMoreRows(){ + return xmlParser.hasNext(); + } + + + public XMLEventReader getXmlParser() { + return xmlParser; + } + + public void setXmlParser(XMLEventReader xmlParser) { + this.xmlParser = xmlParser; + } + + public void setSharedStringsTable(SharedStringsTable sharedStringsTable) { + this.sharedStringsTable = sharedStringsTable; + } + + public void setStylesTable(StylesTable stylesTable) { + this.stylesTable = stylesTable; + } + + public void createEventHandler(){ + eventHandler = new StreamedSheetEventHandler(sharedStringsTable, stylesTable); + } + + + public int getNumberOfColumns() { + return numberOfColumns; + } + + + public void setNumberOfColumns(int numberOfColumns) { + this.numberOfColumns = numberOfColumns; + } + + + public int getSheetNumber() { + return sheetNumber; + } + + + public void setSheetNumber(int sheetNumber) { + this.sheetNumber = sheetNumber; + } + +/* @Override + protected void finalize() throws Throwable { + super.finalize(); + + xmlParser = null; + sharedStringsTable = null; + stylesTable = null; + eventHandler = null; + + }*/ + + /** + * reads all data from sheet + * @param xmlParser + * @param sharedStringsTable + * @param stylesTable + * @return + * @throws XMLStreamException + */ + private Iterator getAllRows(StreamedSheet sheet, + SharedStringsTable sharedStringsTable, StylesTable stylesTable) throws XMLStreamException { + List dataList = new ArrayList(); + StreamedSheetEventHandler eventHandler = new StreamedSheetEventHandler(sharedStringsTable, stylesTable); + while(sheet.getXmlParser().hasNext()){ + XMLEvent event = sheet.getXmlParser().nextEvent(); + eventHandler.handleEvent(event); + if(eventHandler.isEndOfRow()){ + dataList.add(eventHandler.getRow()); + eventHandler.setEndOfRow(false); + } + sheet.setNumberOfColumns(eventHandler.getNumberOfColumns()); + } + + return dataList.iterator(); + } + + /** + * Reads N Rows from excel + * @param sheet + * @param sharedStringsTable + * @param stylesTable + * @param numberOFRows + * @return + * @throws XMLStreamException + */ + private Iterator getNRows(StreamedSheet sheet, StreamedSheetEventHandler eventHandler, int numberOfRows) + throws XMLStreamException { + List dataList = new ArrayList(); + while(sheet.getXmlParser().hasNext()){ + XMLEvent event = sheet.getXmlParser().nextEvent(); + eventHandler.handleEvent(event); + if(eventHandler.isEndOfRow()){ + dataList.add(eventHandler.getRow()); + eventHandler.setEndOfRow(false); + } + sheet.setNumberOfColumns(eventHandler.getNumberOfColumns()); + if(dataList.size() == numberOfRows){ + break; + } + } + + return dataList.iterator(); + } + + /** + * Not supported. Refer getAllRows or getNRows + * @exception UnsupportedOperationException + */ + public Iterator iterator() { + throw new UnsupportedOperationException("Not supported. Refer getAllRows or getNRows"); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public Row createRow(int rownum) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void removeRow(Row row) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     *  Not supported due to memory footprint.
+     * 
+ * @exception UnsupportedOperationException + */ + public Row getRow(int rownum) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     *  Not supported due to memory footprint.
+     * 
+ * @exception UnsupportedOperationException + */ + public int getPhysicalNumberOfRows() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Will be supported in the future.
+     * 
+ * @exception UnsupportedOperationException + */ + public int getFirstRowNum() { + throw new UnsupportedOperationException("Not yet implemented."); + } + + /** + *
+     *  Not supported due to memory footprint.
+     * 
+ * @exception UnsupportedOperationException + */ + public int getLastRowNum() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + * + */ + public void setColumnHidden(int columnIndex, boolean hidden) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     *  Not supported due to memory footprint.
+     * 
+ * @exception UnsupportedOperationException + */ + public boolean isColumnHidden(int columnIndex) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + * + */ + public void setRightToLeft(boolean value) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public boolean isRightToLeft() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void setColumnWidth(int columnIndex, int width) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ */ + public int getColumnWidth(int columnIndex) { + throw new UnsupportedOperationException("Operation not supported."); + } + + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public float getColumnWidthInPixels(int columnIndex) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void setDefaultColumnWidth(int width) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public int getDefaultColumnWidth() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public short getDefaultRowHeight() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public float getDefaultRowHeightInPoints() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + * + */ + public void setDefaultRowHeight(short height) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + * + */ + public void setDefaultRowHeightInPoints(float height) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public CellStyle getColumnStyle(int column) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + * + */ + public int addMergedRegion(CellRangeAddress region) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + * + */ + public int addMergedRegionUnsafe(CellRangeAddress region) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public void validateMergedRegions() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + * + */ + public void setVerticallyCenter(boolean value) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void setHorizontallyCenter(boolean value) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public boolean getHorizontallyCenter() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public boolean getVerticallyCenter() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + * + */ + public void removeMergedRegion(int index) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + * + */ + public void removeMergedRegions(Collection indices) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public int getNumMergedRegions() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public CellRangeAddress getMergedRegion(int index) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public List getMergedRegions() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported. Use getAllRows or getNRows
+     * 
+ * @exception UnsupportedOperationException + */ + public Iterator rowIterator() { + throw new UnsupportedOperationException("Operation not supported.Use getAllRows or getNRows."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + * + */ + public void setForceFormulaRecalculation(boolean value) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public boolean getForceFormulaRecalculation() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void setAutobreaks(boolean value) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void setDisplayGuts(boolean value) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     *  supports only reading.
+     *  
+ * @exception UnsupportedOperationException + */ + public void setDisplayZeros(boolean value) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public boolean isDisplayZeros() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void setFitToPage(boolean value) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void setRowSumsBelow(boolean value) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void setRowSumsRight(boolean value) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public boolean getAutobreaks() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint..
+     *  
+ */ + public boolean getDisplayGuts() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public boolean getFitToPage() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public boolean getRowSumsBelow() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public boolean getRowSumsRight() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public boolean isPrintGridlines() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void setPrintGridlines(boolean show) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public boolean isPrintRowAndColumnHeadings() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + * + */ + public void setPrintRowAndColumnHeadings(boolean show) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public PrintSetup getPrintSetup() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Will be supported in future.
+     *  
+ * @exception UnsupportedOperationException + */ + public Header getHeader() { + // + throw new UnsupportedOperationException("Not implemented yet."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public Footer getFooter() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void setSelected(boolean value) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public double getMargin(short margin) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + * + */ + public void setMargin(short margin, double size) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public boolean getProtect() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + * + */ + public void protectSheet(String password) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public boolean getScenarioProtect() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + * + */ + public void setZoom(int numerator, int denominator) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + * + */ + public void setZoom(int scale) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Will be supported in future.
+     *  
+ * @exception UnsupportedOperationException + */ + public short getTopRow() { + // + throw new UnsupportedOperationException("Not implemented yet."); + } + + /** + *
+     * Will be supported in future.
+     *  
+ * @exception UnsupportedOperationException + */ + public short getLeftCol() { + // + throw new UnsupportedOperationException("Not implemented yet."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public void showInPane(int toprow, int leftcol) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + * + */ + public void shiftRows(int startRow, int endRow, int n) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + * + */ + public void shiftRows(int startRow, int endRow, int n, + boolean copyRowHeight, boolean resetOriginalRowHeight) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + * + */ + public void createFreezePane(int colSplit, int rowSplit, + int leftmostColumn, int topRow) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void createFreezePane(int colSplit, int rowSplit) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void createSplitPane(int xSplitPos, int ySplitPos, + int leftmostColumn, int topRow, int activePane) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public PaneInformation getPaneInformation() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void setDisplayGridlines(boolean show) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public boolean isDisplayGridlines() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + * + */ + public void setDisplayFormulas(boolean show) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public boolean isDisplayFormulas() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void setDisplayRowColHeadings(boolean show) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public boolean isDisplayRowColHeadings() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void setRowBreak(int row) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Will be supported in future.
+     *  
+ * @exception UnsupportedOperationException + */ + public boolean isRowBroken(int row) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void removeRowBreak(int row) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public int[] getRowBreaks() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public int[] getColumnBreaks() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void setColumnBreak(int column) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public boolean isColumnBroken(int column) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void removeColumnBreak(int column) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void setColumnGroupCollapsed(int columnNumber, boolean collapsed) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void groupColumn(int fromColumn, int toColumn) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void ungroupColumn(int fromColumn, int toColumn) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void groupRow(int fromRow, int toRow) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void ungroupRow(int fromRow, int toRow) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void setRowGroupCollapsed(int row, boolean collapse) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void setDefaultColumnStyle(int column, CellStyle style) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void autoSizeColumn(int column) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void autoSizeColumn(int column, boolean useMergedCells) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public Comment getCellComment(int row, int column) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public Comment getCellComment(CellAddress ref) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * WNot supported due to memory footprint..
+     *  
+ * @exception UnsupportedOperationException + */ + public Map getCellComments() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public Drawing getDrawingPatriarch() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public Drawing createDrawingPatriarch() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Will not be supported due to memory constraints
+     * 
+ * @exception UnsupportedOperationException + */ + public Workbook getWorkbook() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     *  Returns sheet name
+     *  
+ * @return String + */ + public String getSheetName() { + return sheetName; + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     *  
+ * + */ + public void setSheetName(String sheetName) { + //to avoid setting sheet name by user + if(this.sheetName == null)this.sheetName = sheetName; + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public boolean isSelected() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public CellRange setArrayFormula(String formula, + CellRangeAddress range) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public CellRange removeArrayFormula(Cell cell) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public DataValidationHelper getDataValidationHelper() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public List getDataValidations() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void addValidationData(DataValidation dataValidation) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public AutoFilter setAutoFilter(CellRangeAddress range) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public SheetConditionalFormatting getSheetConditionalFormatting() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public CellRangeAddress getRepeatingRows() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ */ + public CellRangeAddress getRepeatingColumns() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void setRepeatingRows(CellRangeAddress rowRangeRef) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void setRepeatingColumns(CellRangeAddress columnRangeRef) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public int getColumnOutlineLevel(int columnIndex) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public Hyperlink getHyperlink(int row, int column) { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public Hyperlink getHyperlink(CellAddress addr) { + throw new UnsupportedOperationException("Operation not supported."); + } + + + /** + *
+     * Will not be supported due to memory constraints
+     * 
+ * @exception UnsupportedOperationException + */ + public List getHyperlinkList() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported due to memory footprint.
+     *  
+ * @exception UnsupportedOperationException + */ + public CellAddress getActiveCell() { + throw new UnsupportedOperationException("Operation not supported."); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + * + */ + public void setActiveCell(CellAddress address) { + throw new UnsupportedOperationException("Operation not supported."); + } + + + + + + +} diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/reader/StreamedSheetEventHandler.java b/src/ooxml/java/org/apache/poi/xssf/streaming/reader/StreamedSheetEventHandler.java new file mode 100644 index 0000000..86bec07 --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/reader/StreamedSheetEventHandler.java @@ -0,0 +1,447 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.xssf.streaming.reader; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +import javax.xml.namespace.QName; +import javax.xml.stream.events.Attribute; +import javax.xml.stream.events.Characters; +import javax.xml.stream.events.EndElement; +import javax.xml.stream.events.StartElement; +import javax.xml.stream.events.XMLEvent; + +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellType; +import org.apache.poi.ss.usermodel.DataFormatter; +import org.apache.poi.xssf.model.SharedStringsTable; +import org.apache.poi.xssf.model.StylesTable; +import org.apache.poi.xssf.usermodel.XSSFCellStyle; +import org.apache.poi.xssf.usermodel.XSSFRichTextString; + +/** + * Handler class that handles the event generated by StAX parser + * + */ +public class StreamedSheetEventHandler { + + private boolean isEndOfRow = false; + private boolean isRow = false; + //private boolean mergeCell = false; + + private String formatString = null; + private int formatIndex = 0; + private String cellContentType; + private int rowNumber = 0; + private int numberOfColumns = 0; + private List cellPositions = null; + private String currentCellPosition = null; + private int previousCellIndex = -1; + //private List mergeList = null; //merge cell not supported right now. Complexity involved if multiple rows are merged. + + private SharedStringsTable sharedStringsTable; + private StylesTable stylesTable; + + private DataFormatter formatter = new DataFormatter(); + private Properties properties = new Properties(); + private Map attributes = new HashMap(); + private StringBuffer sb = new StringBuffer(250); + private List dateFormatsAry = new ArrayList(); + + private StreamedRow row; + private StreamedCell cell; + + private final String dateFormat = "m/d/yy|DD/MM/YY|[$-F800]dddd\\,\\ mmmm\\ dd\\,\\ yyyy|m/d;@|m/d/yy;@|mm/dd/yy;@|" + + "[$-409]d\\-mmm;@|[$-409]d\\-mmm\\-yy;@|[$-409]dd\\-mmm\\-yy;@|[$-409]mmm\\-yy;@|[$-409]mmmm\\-yy;@|" + + "[$-409]mmmm\\ d\\,\\ yyyy;@|[$-409]m/d/yy\\ h:mm\\ AM/PM;@|m/d/yy\\ h:mm;@|[$-409]mmmmm;@|" + + "[$-409]mmmmm\\-yy;@|m/d/yyyy;@|[$-409]d\\-mmm\\-yyyy;@|[$-F400]h:mm:ss\\ AM/PM|" + + "h:mm;@|[$-409]h:mm\\ AM/PM;@|h:mm:ss;@|[$-409]h:mm:ss\\ AM/PM;@|mm:ss.0;@|[h]:mm:ss;@|" + + "[$-409]m/d/yy\\ h:mm\\ AM/PM;@|DD/MM/YYYY"; + + public static final String DEFAULT_CELL_TYPE = "DEFAULT"; + public static final String STRING_CELL_TYPE = "STRING"; + public static final String BOOLEAN_CELL_TYPE = "BOOLEAN"; + public static final String DATE_CELL_TYPE = "DATE"; + public static final String FORMULA_CELL_TYPE = "FORMULA"; + public static final String NUMERIC_CELL_TYPE = "NUMERIC"; + + + /** + * Constructor + * @param sharedStringsTable + * @param stylesTable + */ + public StreamedSheetEventHandler(SharedStringsTable sharedStringsTable, StylesTable stylesTable){ + this.sharedStringsTable = sharedStringsTable; + this.stylesTable = stylesTable; + /*try{ + properties.load(getClass().getClassLoader().getResourceAsStream("excelreader.properties")); + }catch(Exception e){ + e.printStackTrace(); + }*/ + + /*if(properties.get("date.formatStrings") != null){ + dateFormat = properties.get("date.formatStrings").toString(); + }*/ + + dateFormatsAry.addAll(Arrays.asList(dateFormat.split("\\|"))); + + } + + /** + * Handler method for handling the event generated for StAX parser. + * For each XML event(Start Tag, End Tag & Characters), StAX parser will + * generate event. This event will be handled by this method. + * Attributes of each element are put in a map with key as attribute + * and value as attribute value. + * @param event + */ + public void handleEvent(XMLEvent event){ + switch(event.getEventType()){ + case XMLEvent.START_ELEMENT: + StartElement startElemet = event.asStartElement(); + QName startName = startElemet.getName(); + String name = startName.getLocalPart(); + Iterator iterator = startElemet.getAttributes(); + while(iterator.hasNext()){ + Attribute attribute = iterator.next(); + attributes.put(attribute.getName().getLocalPart(), attribute.getValue()); + } + if((name.trim().equals("row")) && (attributes.size() > 0)){ + isRow = true; + } + + /*else if(name.trim().equals("mergeCell")){ + mergeCell = true; + } + + if(isRow || mergeCell){ + startElement(name); + }*/ + + if(isRow){ + startElement(name); + } + break; + case XMLEvent.CHARACTERS: + Characters chars = event.asCharacters(); + if(isRow){ + characters(chars.getData()); + } + break; + case XMLEvent.END_ELEMENT: + EndElement endElement = event.asEndElement(); + QName endName = endElement.getName(); + String endTageName = endName.getLocalPart(); + endElement(endTageName); + attributes.clear(); + break; + } + } + + /** + * All the start element events are handled here. + * It identifies the things like rowNumber, cell content type etc + * @param name + */ + private void startElement(String name) { + if(name.equals("row")) { + previousCellIndex = -1; + isEndOfRow = false; + rowNumber = (Integer.parseInt(attributes.get("r")) - 1); + row = new StreamedRow(rowNumber); + if(rowNumber == 0){ + cellPositions = new ArrayList(); + } + }else if(name.equals("c")){ + String cellPosition = attributes.get("r").toUpperCase(); + cellPosition = cellPosition.replaceAll(String.valueOf(rowNumber+1), "").trim(); + currentCellPosition = cellPosition; + if(rowNumber == 0){ + numberOfColumns ++; + cellPositions.add(cellPosition); + } + + cellContentType = getCellContentType(); + }else if(name.equals("f")){ + cellContentType = FORMULA_CELL_TYPE; + } + + /*else if(name.equals("mergeCell")){ + if(mergeList == null){ + mergeList = + } + }*/ + } + + + /** + * This method is invoked on end element event. + * Mostly used for setting the default values before handling + * next start element. + * All data captured in characters event are stored here. + * Data will be stored only on reaching the end element. + * @param name + */ + private void endElement(String name) { + if(name.equals("c")){ + if(isRow){ + storeData(sb.toString()); + } + cellContentType = DEFAULT_CELL_TYPE; + formatString = null; + formatIndex = 0; + currentCellPosition = null; + previousCellIndex++; + sb.delete(0, sb.length()); + }else if(name.equals("row")){ + isEndOfRow = true; + isRow = false; + }else if(name.equals("f")){ + cellContentType = DEFAULT_CELL_TYPE; + } + + /*else if(name.equals("mergeCell")){ + mergeCell = false; + }*/ + } + + /** + * Captures the data + * @param data + */ + private void characters(String data) { + if(!(cellContentType.equals(FORMULA_CELL_TYPE))){ + sb.append(data); + } + } + + /** + * Creates cells and store data + * @param data + */ + private void storeData(String data){ + if (!(cellContentType.equals(FORMULA_CELL_TYPE))) { + if(cellContentType.equals(STRING_CELL_TYPE)){ + data = getStringData(data); + }else if(cellContentType.equals(DATE_CELL_TYPE)){ + data = getDateAsString(data); + }else if(cellContentType.equals(BOOLEAN_CELL_TYPE)){ + if(data.trim().equals("1")){ + data = "TRUE"; + }else if(data.trim().equals("0")){ + data = "FALSE"; + }else if(data.trim().equals("")){ + data = null; + }else{ + try{ + int value = Integer.parseInt(data); + if(value > 0){ + data = "TRUE"; + } + }catch(Exception e){ + data = null; + } + } + } + + if(cellPositions != null){ + fillEmptyCells(); + } + + cell = new StreamedCell(); + cell.setValue(data); + cell.setCellNumber(previousCellIndex+1); + setCellType(cell, cellContentType, data); + row.getCells().add(cell); + + } + } + + /** + * Set the content type of cell. + * + * + * @param cell + * @param contentType + * @param data + */ + private void setCellType(Cell cell, String contentType, String data){ + + if(contentType.equals(BOOLEAN_CELL_TYPE)){ + cell.setCellType(CellType.BOOLEAN); + } + else if(contentType.equals(STRING_CELL_TYPE)){ + if(data != null && data.trim().isEmpty()){ + cell.setCellType(CellType.BLANK); + }else{ + cell.setCellType(CellType.STRING); + } + }else if(contentType.equals(FORMULA_CELL_TYPE)) { + cell.setCellType(CellType.FORMULA); + } + + } + + + /** + * Identifies the cell content type + * @return + */ + private String getCellContentType(){ + String cellConentType = DEFAULT_CELL_TYPE; + String cellType = attributes.get("t"); + if((null != cellType) && (cellType.equals("s"))){ + cellConentType = STRING_CELL_TYPE; + }else if((null != cellType) && (equals("b"))){ + cellConentType = BOOLEAN_CELL_TYPE; + }else{ + + if(checkForDate()){ + cellConentType = DATE_CELL_TYPE; + }else if(checkForBoolean()){ + cellConentType = BOOLEAN_CELL_TYPE; + } + } + return cellConentType; + } + + + + /** + * Identifies if the given cell is a boolean or not + * @return + */ + private boolean checkForBoolean(){ + String cellStyleType = attributes.get("s"); + if(cellStyleType != null){ + int styleIndex = Integer.parseInt(cellStyleType); + XSSFCellStyle style = stylesTable.getStyleAt(styleIndex); + formatIndex = style.getDataFormat(); + formatString = style.getDataFormatString(); + if(formatString.contains("TRUE")){ + return true; + }else{ + return false; + } + }else{ + return false; + } + } + + /** + * identifies if the given cell is a Date cell or not + * @return + */ + private boolean checkForDate(){ + boolean isDate = false; + String cellStyleType = attributes.get("s"); + if(cellStyleType != null){ + int styleIndex = Integer.parseInt(cellStyleType); + XSSFCellStyle style = stylesTable.getStyleAt(styleIndex); + formatIndex = style.getDataFormat(); + formatString = style.getDataFormatString(); + if(dateFormatsAry.contains(formatString.trim())){ + isDate = true; + } + } + + return isDate; + } + + + /** + * fetches the String data from SharedStringsTable.xml + * @param data + * @return + */ + private String getStringData(String data){ + int index = Integer.parseInt(data); + return new XSSFRichTextString(sharedStringsTable.getEntryAt(index)).toString().trim(); + } + + /** + * check the styles applied to the cell and fetches the exact content of date cell + * @param data + * @return + */ + private String getDateAsString(String data) { + if ((data != null) && (!(data.trim().equals("")))) { + /*return formatter.formatRawCellContents(Double.parseDouble(data), + formatIndex, formatString);*/ + + /* + * All dates are formatted with 'DD/MM/YYYY', this is to avoid confusion between + * 10/12/2017 & 10/12/1917. Otherwise both will be read as 10/12/17 if the format + * is DD/MM/YY + */ + return formatter.formatRawCellContents(Double.parseDouble(data), + formatIndex, "DD/MM/YYYY"); + } else { + return null; + } + } + + + /** + * Used to fill empty cells as null + */ + private void fillEmptyCells() { + int currentCellIndex = cellPositions.indexOf(currentCellPosition); + if ((currentCellIndex - previousCellIndex) > 1) { + for (int i = (previousCellIndex + 1); i < currentCellIndex; i++) { + cell = new StreamedCell(); + cell.setValue(null); + row.getCells().add(cell); + } + + previousCellIndex = currentCellIndex; + } + } + + public boolean isEndOfRow() { + return isEndOfRow; + } + + public void setEndOfRow(boolean isEndOfRow) { + this.isEndOfRow = isEndOfRow; + } + + public int getRowNumber() { + return rowNumber; + } + + public int getNumberOfColumns() { + return numberOfColumns; + } + + public List getCellPositions() { + return cellPositions; + } + + public StreamedRow getRow() { + return row; + } + + +} diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/reader/StreamedWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/streaming/reader/StreamedWorkbook.java new file mode 100644 index 0000000..9a4c153 --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/reader/StreamedWorkbook.java @@ -0,0 +1,1022 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.xssf.streaming.reader; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.stream.XMLEventReader; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamException; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpression; +import javax.xml.xpath.XPathFactory; + +import org.apache.poi.openxml4j.opc.OPCPackage; +import org.apache.poi.ss.SpreadsheetVersion; +import org.apache.poi.ss.formula.udf.UDFFinder; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.CreationHelper; +import org.apache.poi.ss.usermodel.DataFormat; +import org.apache.poi.ss.usermodel.Font; +import org.apache.poi.ss.usermodel.Name; +import org.apache.poi.ss.usermodel.PictureData; +import org.apache.poi.ss.usermodel.Row.MissingCellPolicy; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.SheetVisibility; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.util.POILogFactory; +import org.apache.poi.util.POILogger; +import org.apache.poi.xssf.eventusermodel.XSSFReader; +import org.apache.poi.xssf.model.SharedStringsTable; +import org.apache.poi.xssf.model.StylesTable; +import org.w3c.dom.Document; +import org.w3c.dom.NodeList; + +/** + * Represents the excel workbook. + * This version of Workbook is used fore reading very large excel files + * with a less memory footprint. It does not support creation of a workbook. + * It uses StAX(Streaming API for XML) to stream the spreadsheet. + * In order to reduce the memory footprint, only minimal functionalities are + * supported. + * + */ +public class StreamedWorkbook implements Workbook{ + + private static final POILogger logger = POILogFactory.getLogger(StreamedWorkbook.class); + + private File inputFile; + + private Iterator sheetIterator; + private SharedStringsTable sharedStringsTable; + private StylesTable stylesTable; + private XSSFReader globalReader; + private List sheetList = null; + private boolean isClosed; + + private static final String ACTIVE_SHEET_PATH = "//workbookView/@activeTab"; + private static final String SHEET_NAME_PATH = "//sheets/sheet/@name"; + private static final String VISIBLE_SHEET_PATH = "//sheets/sheet[@state='visible']/@sheetId"; + private static final String HIDDEN_SHEET_PATH = "//sheets/sheet[@state='hidden']/@sheetId"; + private static final String VERY_HIDDEN_SHEET_PATH = "//sheets/sheet[@state='veryHidden']/@sheetId"; + + /** + *
+     * Accepts the file path and return an instance of StreamedWorkBook
+     *
+ * @param filePath + * @throws Exception + */ + public StreamedWorkbook(String filePath) throws Exception{ + if((filePath != null) && !(filePath.trim().isEmpty())){ + inputFile = new File(filePath); + + if(inputFile != null){ + XSSFReader reader = getExcelReader(inputFile); + if(reader != null){ + this.sheetIterator = reader.getSheetsData(); + this.sharedStringsTable = reader.getSharedStringsTable(); + this.stylesTable = reader.getStylesTable(); + this.globalReader = reader; + } + } + + }else{ + throw new Exception("No sheets found"); + } + } + + /** + *
+     * Fetch all sheets from given excel file
+     * 
+ * @return Iterator + * @throws Exception + */ + public Iterator getSheetIterator() throws Exception{ + + return getAllSheets(); + } + + + /** + * Returns the list of sheets for the given excel file + * @param file + * @return Iterator + */ + private Iterator getAllSheets(){ + + return getStreamedSheetIterator(); + } + + private Iterator getStreamedSheetIterator(){ + XMLInputFactory factory = null; + + if(sheetIterator != null){ + factory = XMLInputFactory.newInstance(); + sheetList = new ArrayList(); + int sheetNumber = 0; + Object[] sheetNames = getValuesFromWorkbookData(SHEET_NAME_PATH); + while(sheetIterator.hasNext()){ + InputStream sheetInputStream = sheetIterator.next(); + StreamedSheet sheet = null; + try { + sheet = createStreamedSheet(factory.createXMLEventReader(sheetInputStream), sheetNumber, sheetNames[sheetNumber].toString()); + sheetList.add(sheet); + sheetNumber++; + } catch (XMLStreamException e) { + logger.log(POILogger.ERROR, "Exception while reading the workbook. "+e.getMessage()); + } + } + }else{ + throw new RuntimeException("Workbook already closed"); + } + + return sheetList.iterator(); + + } + + + /** + * Creates and returns the instance of StreamedSheet + * + * @param parser + * @param sheetNumber + * @param sheetName + * @return StreamedSheet + */ + private StreamedSheet createStreamedSheet(XMLEventReader parser,int sheetNumber, String sheetName){ + StreamedSheet sheet = new StreamedSheet(); + sheet.setXmlParser(parser); + sheet.setSharedStringsTable(sharedStringsTable); + sheet.setStylesTable(stylesTable); + sheet.setSheetNumber(sheetNumber); + sheet.setSheetName(sheetName); + sheet.createEventHandler(); + + return sheet; + } + + + + /** + * Receives the excel file and returns the file excel file reader + * @param inputStream + * @return + * @throws Exception + */ + private XSSFReader getExcelReader(File file) throws Exception{ + XSSFReader reader = null; + OPCPackage pkg = null; + pkg = OPCPackage.open(file); + reader = new XSSFReader(pkg); + return reader; + } + + + + /** + *
+     * Convenience method to get the active sheet.  The active sheet is is the sheet
+     * which is currently displayed when the workbook is viewed in Excel.
+     * 'Selected' sheet(s) is a distinct concept.
+     * 
+ * + * @return int + */ + public int getActiveSheetIndex() { + + return Integer.parseInt(getValuesFromWorkbookData(ACTIVE_SHEET_PATH)[0].toString()); + + } + + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * + * @exception UnsupportedOperationException + */ + public void setActiveSheet(int sheetIndex) { + throw new UnsupportedOperationException("Not supported due to memory footprint"); + } + + /** + *
+     * Returns the sheet number of the first visible sheet.
+     * 
+ * @return int + */ + public int getFirstVisibleTab() { + Object[] visibleTabs = getValuesFromWorkbookData(VISIBLE_SHEET_PATH); + + return (Integer.parseInt(visibleTabs[0].toString())-1); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + * + */ + public void setFirstVisibleTab(int sheetIndex) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + /** + *
+     * Will be supported in the future.
+     * 
+ * @exception UnsupportedOperationException + * + */ + public void setSheetOrder(String sheetname, int pos) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + /** + *
+     * Will be supported in the future.
+     * 
+ * @exception UnsupportedOperationException + * + */ + public void setSelectedTab(int index) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + * + */ + public void setSheetName(int sheet, String name) { + throw new UnsupportedOperationException("Not supported due to memory footprint"); + } + + /** + *
+     * Returns the sheet name of specified index
+     * 
+ * @return String; + * + */ + public String getSheetName(int sheet) { + return getValuesFromWorkbookData(SHEET_NAME_PATH)[sheet].toString(); + } + + /** + *
+     * Returns the sheet index for the sheet name given
+     * 
+ * @return int + * + */ + public int getSheetIndex(String name) { + int index = 0; + for(Object sheetName: getValuesFromWorkbookData(SHEET_NAME_PATH)){ + if(sheetName.toString().equals(name)){ + return index; + } + index++; + } + + return -1; + } + + /** + *
+     * Will be supported in the future.
+     * 
+ * @exception UnsupportedOperationException + * + */ + public int getSheetIndex(Sheet sheet) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + * + */ + public Sheet createSheet() { + throw new UnsupportedOperationException("Not supported due to memory footprint"); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + * + */ + public Sheet createSheet(String sheetname) { + throw new UnsupportedOperationException("Not supported due to memory footprint"); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + * + */ + public Sheet cloneSheet(int sheetNum) { + throw new UnsupportedOperationException("Not supported due to memory footprint"); + } + + /** + *
+     * Use Iterator getSheetIterator() instead.
+     * 
+ * @exception UnsupportedOperationException + */ + public Iterator sheetIterator() { + throw new UnsupportedOperationException("Not implemented yet. Use getSheetIterator()"); + } + + /** + *
+     *  Returns the number of sheets,
+     * 
+ * @return int representing the number of sheets in workbook + */ + public int getNumberOfSheets() { + int sheetCount = 0; + + if(this.sheetIterator != null){ + while(sheetIterator.hasNext()){ + sheetIterator.next(); + sheetCount++; + } + }else{ + logger.log(POILogger.ERROR, "Workbook already closed"); + } + + + return sheetCount; + } + + /** + *
+     *  Returns sheet at specified index
+     * 
+ * @return Sheet for the specified index + */ + public Sheet getSheetAt(int index) { + if(sheetList == null && !isClosed){ + getAllSheets(); + } + + if(sheetList != null){ + for(StreamedSheet sheet: sheetList){ + if(sheet.getSheetNumber() == index)return sheet; + } + } + + return null; + } + + /** + *
+     * Returns the sheet with specified name
+     * 
+ * + */ + public Sheet getSheet(String name) { + + if(sheetList == null && !isClosed){ + getAllSheets(); + } + + if(sheetList != null){ + for(StreamedSheet sheet: sheetList){ + if(sheet.getSheetName().equals(name))return sheet; + } + } + + return null; + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void removeSheetAt(int index) { + throw new UnsupportedOperationException("Not supported due to memory footprint"); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public Font createFont() { + throw new UnsupportedOperationException("Not supported due to memory footprint"); + } + + + /** + *
+     * Will be supported in the future.
+     * 
+ * @exception UnsupportedOperationException + */ + public Font findFont(short boldWeight, short color, short fontHeight, + String name, boolean italic, boolean strikeout, short typeOffset, + byte underline) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + /** + *
+     * Will be supported in the future.
+     * 
+ * @exception UnsupportedOperationException + */ + public Font findFont(boolean bold, short color, short fontHeight, + String name, boolean italic, boolean strikeout, short typeOffset, + byte underline) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + /** + *
+     * Will be supported in the future.
+     * 
+ * @exception UnsupportedOperationException + */ + public short getNumberOfFonts() { + throw new UnsupportedOperationException("Not implemented yet"); + } + + /** + *
+     * Will be supported in the future.
+     * 
+ * @exception UnsupportedOperationException + */ + public Font getFontAt(short idx) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public CellStyle createCellStyle() { + throw new UnsupportedOperationException("Not supported due to memory footprint"); + } + + + /** + *
+     * Will be supported in the future.
+     * 
+ * @exception UnsupportedOperationException + */ + public int getNumCellStyles() { + throw new UnsupportedOperationException("Not implemented yet"); + } + + /** + *
+     * Not Suppored due to memory footprint.
+     * 
+ * @exception UnsupportedOperationException + */ + public CellStyle getCellStyleAt(int idx) { + throw new UnsupportedOperationException("Not supported due to memory footprint"); + } + + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void write(OutputStream stream) throws IOException { + throw new UnsupportedOperationException("Not supported due to memory footprint"); + } + + /** + *
+     * Close the workbook
+     * 
+ * + */ + public void close() throws IOException { + if(sheetIterator != null){ + while(sheetIterator.hasNext()){ + sheetIterator.next().close(); + } + sheetIterator = null; + } + + if(sheetList != null){ + sheetList.clear(); + sheetList = null; + } + + isClosed = true; + } + + /** + *
+     * Will be supported in the future.
+     * 
+ * @exception UnsupportedOperationException + */ + public int getNumberOfNames() { + throw new UnsupportedOperationException("Not implemented yet"); + } + + /** + *
+     * Will be supported in the future.
+     * 
+ * @exception UnsupportedOperationException + */ + public Name getName(String name) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + /** + *
+     * Will be supported in the future.
+     * 
+ * @exception UnsupportedOperationException + */ + public List getNames(String name) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + /** + *
+     * Will be supported in the future.
+     * 
+ * @exception UnsupportedOperationException + */ + public List getAllNames() { + throw new UnsupportedOperationException("Not implemented yet"); + } + + /** + *
+     * Will be supported in the future.
+     * 
+ * @exception UnsupportedOperationException + */ + public Name getNameAt(int nameIndex) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public Name createName() { + throw new UnsupportedOperationException("Not supported due to memory footprint"); + } + + /** + *
+     * Will be supported in the future.
+     * 
+ * @exception UnsupportedOperationException + */ + public int getNameIndex(String name) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void removeName(int index) { + throw new UnsupportedOperationException("Not supported due to memory footprint"); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void removeName(String name) { + throw new UnsupportedOperationException("Not supported due to memory footprint"); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void removeName(Name name) { + throw new UnsupportedOperationException("Not supported due to memory footprint"); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public int linkExternalWorkbook(String name, Workbook workbook) { + throw new UnsupportedOperationException("Not supported due to memory footprint"); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * + */ + public void setPrintArea(int sheetIndex, String reference) { + throw new UnsupportedOperationException("Not supported due to memory footprint"); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void setPrintArea(int sheetIndex, int startColumn, int endColumn, + int startRow, int endRow) { + throw new UnsupportedOperationException("Not supported due to memory footprint"); + } + + /** + *
+     * Will be supported in the future.
+     * 
+ * @exception UnsupportedOperationException + */ + public String getPrintArea(int sheetIndex) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void removePrintArea(int sheetIndex) { + throw new UnsupportedOperationException("Not supported due to memory footprint"); + } + + /** + *
+     * Will be supported in the future.
+     * 
+ * @exception UnsupportedOperationException + */ + public MissingCellPolicy getMissingCellPolicy() { + throw new UnsupportedOperationException("Not implemented yet"); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void setMissingCellPolicy(MissingCellPolicy missingCellPolicy) { + throw new UnsupportedOperationException("Not supported due to memory footprint"); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public DataFormat createDataFormat() { + throw new UnsupportedOperationException("Not supported due to memory footprint"); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public int addPicture(byte[] pictureData, int format) { + throw new UnsupportedOperationException("Not supported due to memory footprint"); + } + + /** + *
+     * Not supported due to memory footprint.
+     * 
+ * @exception UnsupportedOperationException + */ + public List getAllPictures() { + throw new UnsupportedOperationException("Not supported due to memory footprint"); + } + + /** + *
+     * Will be supported in the future.
+     * 
+ * @exception UnsupportedOperationException + */ + public CreationHelper getCreationHelper() { + throw new UnsupportedOperationException("Not implemented yet"); + } + + /** + *
+     * Will be supported in the future.
+     * 
+ * @exception UnsupportedOperationException + */ + public boolean isHidden() { + throw new UnsupportedOperationException("Not implemented yet"); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void setHidden(boolean hiddenFlag) { + throw new UnsupportedOperationException("Not supported due to memory footprint"); + } + + /** + *
+     * Return true if the sheet with given index is hidden
+     * 
+ * @return boolean + */ + public boolean isSheetHidden(int sheetIx) { + //throw new UnsupportedOperationException("Not implemented yet"); + + Object[] hiddenSheetIdxAry = getValuesFromWorkbookData(HIDDEN_SHEET_PATH); + + for(Object o: hiddenSheetIdxAry){ + int index = (Integer.parseInt(o.toString())-1); + if(index == sheetIx)return true; + } + + return false; + } + + /** + *
+     * Return true if the sheet with given index is veryHidden
+     * 
+ * @return boolean + */ + public boolean isSheetVeryHidden(int sheetIx) { + Object[] veryHiddedSheetIdxAry = getValuesFromWorkbookData(VERY_HIDDEN_SHEET_PATH); + + for(Object o: veryHiddedSheetIdxAry){ + int index = (Integer.parseInt(o.toString())-1); + if(index == sheetIx)return true; + } + + + return false; + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void setSheetHidden(int sheetIx, boolean hidden) { + throw new UnsupportedOperationException("Not supported due to memory footprint"); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void setSheetHidden(int sheetIx, int hidden) { + throw new UnsupportedOperationException("Not supported due to memory footprint"); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void addToolPack(UDFFinder toopack) { + throw new UnsupportedOperationException("Not supported due to memory footprint"); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void setForceFormulaRecalculation(boolean value) { + throw new UnsupportedOperationException("Not supported due to memory footprint"); + } + + /** + *
+     * Will be supported in the future.
+     * 
+ * + */ + public boolean getForceFormulaRecalculation() { + throw new UnsupportedOperationException("Not implemented yet"); + } + + /** + *
+     * Will be supported in the future.
+     * 
+ * @exception UnsupportedOperationException + */ + public SpreadsheetVersion getSpreadsheetVersion() { + throw new UnsupportedOperationException("Not implemented yet"); + } + + + /** + *
+     * Use Iterator getSheetIterator() instead.
+     * 
+ * @exception UnsupportedOperationException + */ + public Iterator iterator() { + throw new UnsupportedOperationException("Not implemented yet. Use getSheetIterator()"); + } + + /** + *
+     * Returns the visibility of sheet for the given index. 
+     * 
+ * @return SheetVisibility[Hidden/Visible/VeryHidden] + */ + public SheetVisibility getSheetVisibility(int sheetIx) { + Object[] hiddenSheetIdxAry = getValuesFromWorkbookData(HIDDEN_SHEET_PATH); + + for(Object o: hiddenSheetIdxAry){ + int index = (Integer.parseInt(o.toString())-1); + if(index == sheetIx)return SheetVisibility.HIDDEN; + } + + Object[] visibleSheetIdxAry = getValuesFromWorkbookData(VISIBLE_SHEET_PATH); + + for(Object o: visibleSheetIdxAry){ + int index = (Integer.parseInt(o.toString())-1); + if(index == sheetIx)return SheetVisibility.VISIBLE; + } + + Object[] veryHiddedSheetIdxAry = getValuesFromWorkbookData(VERY_HIDDEN_SHEET_PATH); + + for(Object o: veryHiddedSheetIdxAry){ + int index = (Integer.parseInt(o.toString())-1); + if(index == sheetIx)return SheetVisibility.VERY_HIDDEN; + } + + /** + * TO DO: Very Hidden + */ + + throw new IllegalArgumentException("Invalid sheet index"); + + + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public void setSheetVisibility(int sheetIx, SheetVisibility visibility) { + throw new UnsupportedOperationException("Not supported due to memory footprint"); + } + + /** + *
+     * Not supported right now, as StreamedWorkbook
+     * supports only reading.
+     * 
+ * @exception UnsupportedOperationException + */ + public int addOlePackage(byte[] oleData, String label, String fileName, + String command) throws IOException { + throw new UnsupportedOperationException("Not supported due to memory footprint"); + } + + /** + * Strictly for reading any specific value from any node in workbook.xml + * DOM Parser is used here assuming that workbook.xml will never contribute + * towards the size of excel file. + * + * @param xml + * @param xpathQuery + * @return Object[] + * @throws Exception + */ + private Object[] getValuesFromWorkbookData(String xpathQuery){ + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + List valueAry = new ArrayList(); + DocumentBuilder builder; + Document doc = null; + NodeList nodeList = null; + try{ + builder = factory.newDocumentBuilder(); + doc = builder.parse(globalReader.getWorkbookData()); + XPathFactory xPathfactory = XPathFactory.newInstance(); + XPath xpath = xPathfactory.newXPath(); + doc.normalize(); + + + XPathExpression xPathExpr = xpath.compile(xpathQuery); + nodeList = (NodeList)xPathExpr.evaluate(doc, XPathConstants.NODESET); + + for(int i=0; i streamedSheetIterator = streamedWorkbook.getSheetIterator(); + + while(streamedSheetIterator.hasNext()){ + streamedSheetIterator.next(); + streamedSheetcount++; + } + + Iterator sheetIterator = workbook.sheetIterator(); + + while(sheetIterator.hasNext()){ + sheetIterator.next(); + count++; + } + + assertEquals(count, streamedSheetcount); + } catch (Exception e) { + + } + + streamedWorkbook.close(); + workbook.close(); + + } + + @Test + public void testTotalNumberOfSheets() throws Exception{ + POIDataSamples files = POIDataSamples.getSpreadSheetInstance(); + File f= files.getFile("SpreadSheetSample04022017.xlsx"); + StreamedWorkbook streamedWorkbook = new StreamedWorkbook(f.getAbsolutePath()); + Workbook workbook = new XSSFWorkbook(f); + + try { + int sheetCount = workbook.getNumberOfSheets(); + int streamedSheetCount = streamedWorkbook.getNumberOfSheets(); + + assertEquals(sheetCount, streamedSheetCount); + } catch (Exception e) { + } + + workbook.close(); + streamedWorkbook.close(); + } + + @Test + public void testTotalRowCount() throws Exception{ + POIDataSamples files = POIDataSamples.getSpreadSheetInstance(); + File f= files.getFile("SpreadSheetSample04022017.xlsx"); + StreamedWorkbook streamedWorkbook = new StreamedWorkbook(f.getAbsolutePath()); + Workbook workbook = new XSSFWorkbook(f); + int streamedSheetCount = 0; + + long streamedRowCount = 0; + + Iterator streamedSheetIterator = streamedWorkbook.getSheetIterator(); + + while((streamedSheetIterator.hasNext()) && (streamedSheetCount == 0)){ + StreamedSheet sheet = streamedSheetIterator.next(); + + Iterator rows = sheet.getAllRows(); + + while(rows.hasNext()){ + rows.next(); + streamedRowCount++; + } + + streamedSheetCount++; + } + + int sheetCount= 0; + int rowCount = 0; + Iterator sheetIterator = workbook.sheetIterator(); + while(sheetIterator.hasNext() && sheetCount ==0){ + Sheet sheet = sheetIterator.next(); + Iterator rowIterator = sheet.iterator(); + + while(rowIterator.hasNext()){ + rowIterator.next(); + rowCount++; + } + + sheetCount++; + } + + assertEquals(rowCount, streamedRowCount); + + streamedWorkbook.close(); + workbook.close(); + } + + @Test + public void testNRowCount() throws Exception{ + POIDataSamples files = POIDataSamples.getSpreadSheetInstance(); + File f= files.getFile("SpreadSheetSample04022017.xlsx"); + StreamedWorkbook workbook = new StreamedWorkbook(f.getAbsolutePath()); + + long count = 0; + int sheetCount = 0; + + Iterator sheetIterator = workbook.getSheetIterator(); + + while((sheetIterator.hasNext()) && (sheetCount == 0)){ + StreamedSheet sheet = sheetIterator.next(); + + Iterator rows = sheet.getNRows(4); + + while(rows.hasNext()){ + rows.next(); + count++; + } + + assertEquals(4, count); + sheetCount++; + } + + workbook.close(); + } + + @Test + public void testTotalCellCount() throws Exception{ + POIDataSamples files = POIDataSamples.getSpreadSheetInstance(); + File f= files.getFile("SpreadSheetSample04022017.xlsx"); + StreamedWorkbook streamedWorkbook = new StreamedWorkbook(f.getAbsolutePath()); + Workbook workbook = new XSSFWorkbook(f); + int streamedSheetCount = 0; + long streamedCellCount = 0; + long cellCount = 0; + + Iterator streamedSheetIterator = streamedWorkbook.getSheetIterator(); + + while((streamedSheetIterator.hasNext()) && (streamedSheetCount == 0)){ + StreamedSheet streamedSheet = streamedSheetIterator.next(); + + Iterator streamedRows = streamedSheet.getAllRows(); + + while(streamedRows.hasNext()){ + StreamedRow streamedRow = streamedRows.next(); + + Iterator streamedCellIterator = streamedRow.getCellIterator(); + + while(streamedCellIterator.hasNext()){ + streamedCellIterator.next(); + streamedCellCount++; + } + + } + + streamedSheetCount++; + } + + int sheetCount = 0; + Iterator sheetIterator = workbook.sheetIterator(); + + while((sheetIterator.hasNext()) && (sheetCount == 0)){ + Sheet sheet = sheetIterator.next(); + + Iterator rowIterator = sheet.iterator(); + while(rowIterator.hasNext()){ + Row row = rowIterator.next(); + + Iterator cellIterator = row.cellIterator(); + + while(cellIterator.hasNext()){ + cellIterator.next(); + cellCount++; + } + } + + sheetCount++; + } + + assertEquals(cellCount, streamedCellCount); + + streamedWorkbook.close(); + workbook.close(); + } + + + @Test + public void testStartingRowAndCellNumber() throws Exception{ + POIDataSamples files = POIDataSamples.getSpreadSheetInstance(); + File f= files.getFile("SpreadSheetSample04022017.xlsx"); + StreamedWorkbook streamedWorkbook = new StreamedWorkbook(f.getAbsolutePath()); + Workbook workbook = new XSSFWorkbook(f); + + List workbookStartingCellNumbers = new ArrayList(); + List streamedWorkbookStartingCellNumbers = new ArrayList(); + + int workbookStartingRowNum = 0; + int streamedWorkbookStartingRowNumber = 0; + + Sheet sheet = workbook.getSheetAt(0); + Iterator rowIterator = sheet.iterator(); + + int workbookRowCount = 0; + + while(rowIterator.hasNext()){ + Row row = rowIterator.next(); + if(workbookRowCount == 0){ + workbookStartingRowNum = row.getRowNum(); + workbookRowCount++; + } + + if(row != null) + workbookStartingCellNumbers.add(row.getFirstCellNum()); + + } + + StreamedSheet streamedSheet = (StreamedSheet)streamedWorkbook.getSheetAt(0); + Iterator streamedRowIterator = streamedSheet.getAllRows(); + int streamedWorkbookRowCount = 0; + + while(streamedRowIterator.hasNext()){ + StreamedRow row = streamedRowIterator.next(); + if(streamedWorkbookRowCount == 0){ + streamedWorkbookStartingRowNumber = row.getRowNum(); + streamedWorkbookRowCount++; + } + + if(row != null) + streamedWorkbookStartingCellNumbers.add(row.getFirstCellNum()); + } + + assertEquals(workbookStartingRowNum, streamedWorkbookStartingRowNumber); + assertEquals(workbookStartingCellNumbers, streamedWorkbookStartingCellNumbers); + + workbook.close(); + streamedWorkbook.close(); + } + + @Test + public void testSheetData() throws Exception{ + POIDataSamples files = POIDataSamples.getSpreadSheetInstance(); + File f= files.getFile("SpreadSheetSample04022017.xlsx"); + StreamedWorkbook workbook = new StreamedWorkbook(f.getAbsolutePath()); + int sheetCount = 0; + + + Iterator sheetIterator = workbook.getSheetIterator(); + + while((sheetIterator.hasNext()) && (sheetCount == 0)){ + int rowCount = 0; + + StreamedSheet sheet = sheetIterator.next(); + + Iterator rows = sheet.getAllRows(); + + while(rows.hasNext()){ + + StreamedRow row = rows.next(); + + int cellCount = 0; + + Iterator cellIterator = row.getCellIterator(); + + while(cellIterator.hasNext()){ + StreamedCell cell = cellIterator.next(); + if(rowCount == 1){ + + if(cellCount == 0){ + assertEquals("1", cell.getValue()); + }else if(cellCount == 1){ + assertEquals("Item1", cell.getValue()); + }else if(cellCount == 2){ + assertEquals("201", cell.getValue()); + }else if(cellCount == 3){ + assertEquals("100.11", cell.getValue()); + }else if(cellCount == 4){ + assertEquals("TRUE", cell.getValue()); + }else if(cellCount == 5){ + assertEquals("04/02/1917", cell.getValue()); + }else if(cellCount == 6){ + assertEquals("90.11", cell.getValue()); + } + }else if(rowCount == 3){ + if(cellCount == 4){ + assertEquals(null, cell.getValue()); + } + } + + cellCount++; + + } + + + rowCount++; + + } + + sheetCount++; + } + + workbook.close(); + } + + @Test + public void testBatchData() throws Exception{ + + POIDataSamples files = POIDataSamples.getSpreadSheetInstance(); + File f= files.getFile("SpreadSheetSample04022017.xlsx"); + StreamedWorkbook workbook = new StreamedWorkbook(f.getAbsolutePath()); + int sheetCount = 0; + + + Iterator sheetIterator = workbook.getSheetIterator(); + + while(sheetIterator.hasNext()){ + + StreamedSheet sheet = sheetIterator.next(); + + if(sheetCount == 1){ + Iterator rows = sheet.getNRows(1); + + while(rows.hasNext()){ + StreamedRow row = rows.next(); + assertEquals("Row Number:0 --> Item | item description | Strore | Price | Promotion applied | MFD | Discount rate |", row.toString().trim()); + } + + + rows = sheet.getNRows(4); + + while(rows.hasNext()){ + StreamedRow row = rows.next(); + assertEquals("Row Number:1 --> 1 | Item1 | 201 | 100.11 | TRUE | 04/02/1917 | 90.11 |", row.toString().trim()); + row = rows.next(); + assertEquals("Row Number:2 --> 2 | Item2 | 202 | 101.11 | TRUE | 05/02/1917 | 91.11 |", row.toString().trim()); + row = rows.next(); + assertEquals("Row Number:3 --> 3 | Item3 | 203 | 102.11 | TRUE | 06/02/1917 | 92.11 |", row.toString().trim()); + row = rows.next(); + assertEquals("Row Number:4 --> 4 | Item4 | 204 | 103.11 | TRUE | 07/02/1917 | 93.11 |", row.toString().trim()); + } + + + + rows = sheet.getNRows(4); + + while(rows.hasNext()){ + StreamedRow row = rows.next(); + assertEquals("Row Number:5 --> 5 | Item5 | 205 | 104.11 | TRUE | 08/02/1917 | 94.11 |", row.toString().trim()); + row = rows.next(); + assertEquals("Row Number:6 --> 6 | Item6 | 206 | 105.11 | TRUE | 09/02/1917 | 95.11 |", row.toString().trim()); + row = rows.next(); + assertEquals("Row Number:7 --> 7 | Item7 | 207 | 106.11 | TRUE | 10/02/1917 | 96.11 |", row.toString().trim()); + row = rows.next(); + assertEquals("Row Number:8 --> 8 | Item8 | 208 | 107.11 | FALSE | 11/02/1917 | 97.11 |", row.toString().trim()); + } + + } + + + sheetCount++; + + } + + workbook.close(); + + } + + + @Test + public void testGetCell() throws Exception{ + POIDataSamples files = POIDataSamples.getSpreadSheetInstance(); + File f= files.getFile("SpreadSheetSample04022017.xlsx"); + StreamedWorkbook workbook = new StreamedWorkbook(f.getAbsolutePath()); + int sheetCount = 0; + + + Iterator sheetIterator = workbook.getSheetIterator(); + + while((sheetIterator.hasNext()) && (sheetCount == 0)){ + + StreamedSheet sheet = sheetIterator.next(); + + Iterator rows = sheet.getAllRows(); + + while(rows.hasNext()){ + + StreamedRow row = rows.next(); + + if(row.getRowNum() == 3){ + StreamedCell cell = (StreamedCell)row.getCell(5); + assertEquals("06/02/1917", cell.getValue()); + + try{ + cell = (StreamedCell)row.getCell(10); + }catch(Exception exception){ + assertEquals(true, exception instanceof IndexOutOfBoundsException); + } + } + + + } + + sheetCount++; + } + + workbook.close(); + } + + + @Test + public void testGetFirstAndLastCellNum() throws Exception{ + POIDataSamples files = POIDataSamples.getSpreadSheetInstance(); + File f= files.getFile("SpreadSheetSample04022017.xlsx"); + StreamedWorkbook workbook = new StreamedWorkbook(f.getAbsolutePath()); + int sheetCount = 0; + + + Iterator sheetIterator = workbook.getSheetIterator(); + + while((sheetIterator.hasNext()) && (sheetCount == 0)){ + + StreamedSheet sheet = sheetIterator.next(); + + Iterator rows = sheet.getAllRows(); + + while(rows.hasNext()){ + + StreamedRow row = rows.next(); + + if(row.getRowNum() == 3){ + StreamedCell cell = (StreamedCell)row.getCell(row.getFirstCellNum()); + assertEquals("3", cell.getValue()); + assertEquals("3", cell.getStringCellValue()); + + cell = (StreamedCell)row.getCell(row.getLastCellNum()-1); + assertEquals("92.11", cell.getValue()); + assertEquals("92.11", cell.getStringCellValue()); + } + } + + sheetCount++; + } + + workbook.close(); + } + + @Test + public void testReopenClosedWorkbook(){ + + try{ + POIDataSamples files = POIDataSamples.getSpreadSheetInstance(); + File f= files.getFile("SpreadSheetSample04022017.xlsx"); + StreamedWorkbook workbook = new StreamedWorkbook(f.getAbsolutePath()); + + workbook.close(); + + Iterator sheetIterator = workbook.getSheetIterator(); + }catch(Exception e){ + assertEquals("Workbook already closed", e.getMessage()); + } + + + } + + @Test + public void testGetActiveSheetIndex() throws Exception{ + POIDataSamples files = POIDataSamples.getSpreadSheetInstance(); + File f= files.getFile("SpreadSheetSample04022017.xlsx"); + StreamedWorkbook streamedWorkbook = new StreamedWorkbook(f.getAbsolutePath()); + + Workbook workbook = new XSSFWorkbook(f); + + assertEquals(workbook.getActiveSheetIndex(), streamedWorkbook.getActiveSheetIndex()); + streamedWorkbook.close(); + workbook.close(); + } + + @Test + public void testGetSheetNameWithSheetIndex() throws Exception{ + POIDataSamples files = POIDataSamples.getSpreadSheetInstance(); + File f= files.getFile("SpreadSheetSample04022017.xlsx"); + StreamedWorkbook streamedWorkbook = new StreamedWorkbook(f.getAbsolutePath()); + + Workbook workbook = new XSSFWorkbook(f); + + assertEquals(workbook.getSheetName(1), streamedWorkbook.getSheetName(1)); + streamedWorkbook.close(); + workbook.close(); + } + + @Test + public void testGetSheetIndex() throws Exception{ + POIDataSamples files = POIDataSamples.getSpreadSheetInstance(); + File f= files.getFile("SpreadSheetSample04022017.xlsx"); + StreamedWorkbook streamedWorkbook = new StreamedWorkbook(f.getAbsolutePath()); + + Workbook workbook = new XSSFWorkbook(f); + + assertEquals(workbook.getSheetIndex("Sheet3"), streamedWorkbook.getSheetIndex("Sheet3")); + streamedWorkbook.close(); + workbook.close(); + } + + @Test + public void testGetFirstVisibleTab() throws Exception{ + POIDataSamples files = POIDataSamples.getSpreadSheetInstance(); + File f= files.getFile("SpreadSheetSample04022017.xlsx"); + StreamedWorkbook streamedWorkbook = new StreamedWorkbook(f.getAbsolutePath()); + + Workbook workbook = new XSSFWorkbook(f); + + assertEquals(workbook.getFirstVisibleTab(), streamedWorkbook.getFirstVisibleTab()); + streamedWorkbook.close(); + workbook.close(); + } + + @Test + public void testGetSheetName() throws Exception{ + POIDataSamples files = POIDataSamples.getSpreadSheetInstance(); + File f= files.getFile("SpreadSheetSample04022017.xlsx"); + StreamedWorkbook streamedWorkbook = new StreamedWorkbook(f.getAbsolutePath()); + + List workbookSheetNames = new ArrayList(); + List streamedWorkbookSheetNames = new ArrayList(); + + Workbook workbook = new XSSFWorkbook(f); + + Iterator sheetIterator = workbook.sheetIterator(); + + while(sheetIterator.hasNext()){ + workbookSheetNames.add(sheetIterator.next().getSheetName()); + } + + + + Iterator streamedSheetIterator = streamedWorkbook.getSheetIterator(); + + while(streamedSheetIterator.hasNext()){ + streamedWorkbookSheetNames.add(streamedSheetIterator.next().getSheetName()); + } + + + assertEquals(workbookSheetNames, streamedWorkbookSheetNames); + + streamedWorkbook.close(); + workbook.close(); + } + + @Test + public void testIsSheetHidden() throws Exception{ + POIDataSamples files = POIDataSamples.getSpreadSheetInstance(); + File f= files.getFile("SpreadSheetSample04022017.xlsx"); + StreamedWorkbook streamedWorkbook = new StreamedWorkbook(f.getAbsolutePath()); + Workbook workbook = new XSSFWorkbook(f); + + + assertEquals(workbook.isSheetHidden(2), streamedWorkbook.isSheetHidden(2)); + assertEquals(workbook.isSheetHidden(1), streamedWorkbook.isSheetHidden(1)); + + streamedWorkbook.close(); + workbook.close(); + + } + + @Test + public void testGetSheetVisibility() throws Exception{ + POIDataSamples files = POIDataSamples.getSpreadSheetInstance(); + File f= files.getFile("SpreadSheetSample04022017.xlsx"); + StreamedWorkbook streamedWorkbook = new StreamedWorkbook(f.getAbsolutePath()); + Workbook workbook = new XSSFWorkbook(f); + + + assertEquals(workbook.getSheetVisibility(2), streamedWorkbook.getSheetVisibility(2)); + assertEquals(workbook.getSheetVisibility(1), streamedWorkbook.getSheetVisibility(1)); + + streamedWorkbook.close(); + workbook.close(); + + } + + @Test + public void testGetSheet() throws Exception{ + POIDataSamples files = POIDataSamples.getSpreadSheetInstance(); + File f= files.getFile("SpreadSheetSample04022017.xlsx"); + StreamedWorkbook streamedWorkbook = new StreamedWorkbook(f.getAbsolutePath()); + Workbook workbook = new XSSFWorkbook(f); + + Sheet sheet = workbook.getSheet("Sheet2"); + Sheet streamedSheet = streamedWorkbook.getSheet("Sheet2"); + + assertEquals(sheet.getSheetName(), streamedSheet.getSheetName()); + + streamedWorkbook.close(); + workbook.close(); + + } + + @Test(expected=NullPointerException.class) + public void testGetSheetAfterClose() throws Exception{ + POIDataSamples files = POIDataSamples.getSpreadSheetInstance(); + File f= files.getFile("SpreadSheetSample04022017.xlsx"); + StreamedWorkbook streamedWorkbook = new StreamedWorkbook(f.getAbsolutePath()); + streamedWorkbook.close(); + Sheet streamedSheet = streamedWorkbook.getSheet("Sheet2"); + System.out.println(streamedSheet.getSheetName()); + } + + @Test + public void testGetSheetAt() throws Exception{ + POIDataSamples files = POIDataSamples.getSpreadSheetInstance(); + File f= files.getFile("SpreadSheetSample04022017.xlsx"); + StreamedWorkbook streamedWorkbook = new StreamedWorkbook(f.getAbsolutePath()); + Workbook workbook = new XSSFWorkbook(f); + + Sheet sheet = workbook.getSheetAt(0); + Sheet streamedSheet = streamedWorkbook.getSheetAt(0); + + assertEquals(sheet.getSheetName(), streamedSheet.getSheetName()); + + streamedWorkbook.close(); + workbook.close(); + + } + + @Test(expected=NullPointerException.class) + public void testGetSheetAtAfterClose() throws Exception{ + POIDataSamples files = POIDataSamples.getSpreadSheetInstance(); + File f= files.getFile("SpreadSheetSample04022017.xlsx"); + StreamedWorkbook streamedWorkbook = new StreamedWorkbook(f.getAbsolutePath()); + streamedWorkbook.close(); + + Sheet streamedSheet = streamedWorkbook.getSheetAt(0); + System.out.println(streamedSheet.getSheetName()); + + } + + + @Test(expected=NullPointerException.class) + public void testGetSheetException() throws Exception{ + POIDataSamples files = POIDataSamples.getSpreadSheetInstance(); + File f= files.getFile("SpreadSheetSample04022017.xlsx"); + StreamedWorkbook streamedWorkbook = new StreamedWorkbook(f.getAbsolutePath()); + + Sheet sheet = streamedWorkbook.getSheet("Sheet200"); + + sheet.getSheetName(); + + streamedWorkbook.close(); + + } + + @Test + public void testGetFirstCellNumber() throws Exception{ + POIDataSamples files = POIDataSamples.getSpreadSheetInstance(); + File f= files.getFile("SpreadSheetSample04022017.xlsx"); + StreamedWorkbook streamedWorkbook = new StreamedWorkbook(f.getAbsolutePath()); + Workbook workbook = new XSSFWorkbook(f); + + List workbookFirstCellNumList = new ArrayList(); + List sworkbookFirstCellNumList = new ArrayList(); + + Iterator workbookSheetIterator = workbook.iterator(); + while(workbookSheetIterator.hasNext()){ + Sheet sheet = workbookSheetIterator.next(); + Iterator rowIterator = sheet.iterator(); + while(rowIterator.hasNext()){ + Row row = rowIterator.next(); + workbookFirstCellNumList.add(row.getFirstCellNum()); + } + } + + + Iterator sworkbookSheetIterator = streamedWorkbook.getSheetIterator(); + while(sworkbookSheetIterator.hasNext()){ + StreamedSheet sheet = sworkbookSheetIterator.next(); + Iterator rowIterator = sheet.getAllRows(); + while(rowIterator.hasNext()){ + Row row = rowIterator.next(); + sworkbookFirstCellNumList.add(row.getFirstCellNum()); + } + } + + + assertEquals(workbookFirstCellNumList, sworkbookFirstCellNumList); + + streamedWorkbook.close(); + workbook.close(); + + } + + @Test + public void testGetLastCellNumber() throws Exception{ + POIDataSamples files = POIDataSamples.getSpreadSheetInstance(); + File f= files.getFile("SpreadSheetSample04022017.xlsx"); + StreamedWorkbook streamedWorkbook = new StreamedWorkbook(f.getAbsolutePath()); + Workbook workbook = new XSSFWorkbook(f); + + List workbookLastCellNumList = new ArrayList(); + List sworkbookLastCellNumList = new ArrayList(); + + Iterator workbookSheetIterator = workbook.iterator(); + while(workbookSheetIterator.hasNext()){ + Sheet sheet = workbookSheetIterator.next(); + Iterator rowIterator = sheet.iterator(); + while(rowIterator.hasNext()){ + Row row = rowIterator.next(); + workbookLastCellNumList.add(row.getLastCellNum()); + } + } + + + Iterator sworkbookSheetIterator = streamedWorkbook.getSheetIterator(); + while(sworkbookSheetIterator.hasNext()){ + StreamedSheet sheet = sworkbookSheetIterator.next(); + Iterator rowIterator = sheet.getAllRows(); + while(rowIterator.hasNext()){ + Row row = rowIterator.next(); + sworkbookLastCellNumList.add(row.getLastCellNum()); + } + } + + + assertEquals(workbookLastCellNumList, sworkbookLastCellNumList); + + streamedWorkbook.close(); + workbook.close(); + + } + + @Test + public void testGetPhysicalNumOfCells() throws Exception{ + POIDataSamples files = POIDataSamples.getSpreadSheetInstance(); + File f= files.getFile("SpreadSheetSample04022017.xlsx"); + StreamedWorkbook streamedWorkbook = new StreamedWorkbook(f.getAbsolutePath()); + Workbook workbook = new XSSFWorkbook(f); + + List workbookPhysicalCellNums = new ArrayList(); + List sworkbookPhysicalCellNums = new ArrayList(); + + Iterator workbookSheetIterator = workbook.iterator(); + while(workbookSheetIterator.hasNext()){ + Sheet sheet = workbookSheetIterator.next(); + Iterator rowIterator = sheet.iterator(); + while(rowIterator.hasNext()){ + Row row = rowIterator.next(); + workbookPhysicalCellNums.add(row.getPhysicalNumberOfCells()); + } + } + + + Iterator sworkbookSheetIterator = streamedWorkbook.getSheetIterator(); + while(sworkbookSheetIterator.hasNext()){ + StreamedSheet sheet = sworkbookSheetIterator.next(); + Iterator rowIterator = sheet.getAllRows(); + while(rowIterator.hasNext()){ + Row row = rowIterator.next(); + sworkbookPhysicalCellNums.add(row.getPhysicalNumberOfCells()); + } + } + + + assertEquals(workbookPhysicalCellNums, sworkbookPhysicalCellNums); + + streamedWorkbook.close(); + workbook.close(); + + } + + + + +} diff --git a/test-data/spreadsheet/SpreadSheetSample04022017.xlsx b/test-data/spreadsheet/SpreadSheetSample04022017.xlsx new file mode 100644 index 0000000..e1da613 Binary files /dev/null and b/test-data/spreadsheet/SpreadSheetSample04022017.xlsx differ