Package csv.impl
Class ExcelReader
java.lang.Object
csv.impl.AbstractTableReader
csv.impl.AbstractStreamTableReader
csv.impl.ExcelReader
- All Implemented Interfaces:
TableReader,Iterable<Object[]>,Iterator<Object[]>
Implements Excel reading.
This class reads Excel sheets like a stream, meaning
delivering rows one by one from the current sheet.
Use this reader if you want to load an Excel file by creating a File
and passing it to the constructor.
Example:
java.io.File f = new java.io.File("excel-test.xls");
ExcelReader in = new ExcelReader(f);
while (in.hasNext()) {
Object columns[] = in.next();
// Do something here
}
in.close();
- Author:
- RalphSchuster
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor.ExcelReader(File file) Constructor for reading from a file.Constructor to read from an existing stream.ExcelReader(String file) Constructor for reading from a file.ExcelReader(org.apache.poi.ss.usermodel.Workbook workbook) Constructor to read from an existing workbook. -
Method Summary
Modifier and TypeMethodDescriptionintComputes the max row length of any rows in this sheet.evaluateCellValue(org.apache.poi.ss.usermodel.Cell cell) Returns the evaluated cell content.org.apache.poi.ss.usermodel.FormulaEvaluatorReturns a formula evaluator for the current workbook.org.apache.poi.ss.usermodel.RowReturns the last delivered row.intReturns the number of rows in the current sheet.protected org.apache.poi.ss.usermodel.RowgetOrCreateRow(int rowNum) Ensures that the sheet contains a row at the given index.org.apache.poi.ss.usermodel.SheetgetSheet()Returns the current sheet.getValue(int rownum, int cellNum) Returns the value of the specified cell.getValue(org.apache.poi.ss.usermodel.Cell cell) Returns the value of the specified cell.getValue(org.apache.poi.ss.usermodel.Row row, int cellNum) Returns the value of the specified cell.Object[]getValues(int rowNum) Returns the row at the given index.Object[]getValues(org.apache.poi.ss.usermodel.Row row) Returns the row as Java objects.org.apache.poi.ss.usermodel.WorkbookReturns the workbook.booleanhasNext()Returns whether there is a row to be read in the current sheet.booleanReturns whether formulas shall be evaluated or not (default istrue).booleanReturns whether date/time values shall be returns as LocalDateTime object or simple Dates.booleanReturns whether blank rows will be skipped or not while reading.Object[]next()Returns the next row.voidopen()Opens the stream by retrieving the workbook and selecting the first sheet.protected voidReads the header row from next line.voidreset()Resets the reader by resetting the current row indexprotected voidRetrieves the next row from the current sheet.protected booleanrowHasOnlyBlankCells(org.apache.poi.ss.usermodel.Row row) Checks whether row has only blank cells.org.apache.poi.ss.usermodel.SheetselectSheet(int index) Select the given sheet to be read from.org.apache.poi.ss.usermodel.SheetselectSheet(String name) Select the given sheet to be read from.org.apache.poi.ss.usermodel.SheetselectSheet(org.apache.poi.ss.usermodel.Sheet sheet) Select the given sheet to be read from.voidsetEvaluateFormulas(boolean evaluateFormulas) Sets whether formulas shall be evaluated or not (default istrue).voidsetJavaTimeEnabled(boolean javaTimeEnabled) Sets whether date/time values shall be returns as LocalDateTime object or simple Dates.voidsetSkipBlankRows(boolean skipBlankRows) Sets whether blank rows will be skipped or not while reading.Methods inherited from class csv.impl.AbstractStreamTableReader
close, createInputStreamReader, createReader, getCharsetDecoder, getInputStream, getReader, remove, setCharset, setCharset, setCharsetDecoder, setInputStreamMethods inherited from class csv.impl.AbstractTableReader
convert, convert, convertArray, convertArray, get, getColumnIndex, getColumnType, getHeaderRow, getLineCount, getMapper, getMinimumColumnCount, getRowCount, hasHeaderRow, incrementLineCount, incrementRowCount, isHeaderRowRead, iterator, notifyComment, registerCommentCallBack, setColumnType, setHasHeaderRow, setHeaderRow, setHeaderRowRead, setMapper, setMinimumColumnCount, unregisterCommentCallBackMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliteratorMethods inherited from interface java.util.Iterator
forEachRemaining
-
Constructor Details
-
ExcelReader
public ExcelReader()Default constructor. -
ExcelReader
Constructor for reading from a file.- Parameters:
file- file to read from- Throws:
FileNotFoundException- when file does not exist
-
ExcelReader
Constructor to read from an existing stream.- Parameters:
in- input stream to be used
-
ExcelReader
public ExcelReader(org.apache.poi.ss.usermodel.Workbook workbook) Constructor to read from an existing workbook.- Parameters:
workbook- the workbook be used
-
ExcelReader
Constructor for reading from a file.- Parameters:
file- file to read from- Throws:
FileNotFoundException- when file does not exist
-
-
Method Details
-
open
public void open()Opens the stream by retrieving the workbook and selecting the first sheet.- Specified by:
openin interfaceTableReader- Overrides:
openin classAbstractTableReader- See Also:
-
getWorkbook
public org.apache.poi.ss.usermodel.Workbook getWorkbook()Returns the workbook.- Returns:
- workbook
-
isSkipBlankRows
public boolean isSkipBlankRows()Returns whether blank rows will be skipped or not while reading.- Returns:
truewhen blank rows are skipped (default),falseotherwise
-
setSkipBlankRows
public void setSkipBlankRows(boolean skipBlankRows) Sets whether blank rows will be skipped or not while reading.- Parameters:
skipBlankRows-truewhen blank rows are skipped (default),falseotherwise
-
computeMaxColumnCount
public int computeMaxColumnCount()Computes the max row length of any rows in this sheet.- Returns:
- int length
-
selectSheet
Select the given sheet to be read from.- Parameters:
name- name of sheet- Returns:
- sheet selected
-
selectSheet
public org.apache.poi.ss.usermodel.Sheet selectSheet(org.apache.poi.ss.usermodel.Sheet sheet) Select the given sheet to be read from.- Parameters:
sheet- sheet to be selected- Returns:
- sheet selected
-
getNumRows
public int getNumRows()Returns the number of rows in the current sheet.- Returns:
- number of rows in sheet or
-1if no sheet is selected - Since:
- 2.9.1
-
selectSheet
public org.apache.poi.ss.usermodel.Sheet selectSheet(int index) Select the given sheet to be read from.- Parameters:
index- index of sheet- Returns:
- sheet selected
-
getSheet
public org.apache.poi.ss.usermodel.Sheet getSheet()Returns the current sheet.- Returns:
- the current sheet.
-
getLastExcelRow
public org.apache.poi.ss.usermodel.Row getLastExcelRow()Returns the last delivered row. This is the row delivered by last call tonext().- Returns:
- the last row delivered by
next()
-
reset
public void reset()Resets the reader by resetting the current row index- Specified by:
resetin interfaceTableReader- Overrides:
resetin classAbstractStreamTableReader- See Also:
-
hasNext
public boolean hasNext()Returns whether there is a row to be read in the current sheet. This implementation stops reading when last row from a sheet was read. You might need to manually select the next sheet if you want to read more rows from other sheets.- Returns:
- true if a row is available in current sheet.
- See Also:
-
next
Returns the next row. This method increases the internal row index and delivers the next row in the sheet. Values in the array are Java objects depending on the cell type. If the cell contained a formula, the formula is evaluated before returning the row.- Returns:
- values in row
- See Also:
-
getValues
Returns the row at the given index. Values in the array are Java objects depending on the cell type. If the cell contained a formula, the formula is evaluated before returning the row.- Parameters:
rowNum- row index to read- Returns:
- values of row
-
getValues
Returns the row as Java objects. Values in the array are Java objects depending on the cell type. If the cell contained a formula, the formula is evaluated before returning the row.- Parameters:
row- row to read- Returns:
- values in row
-
getValue
Returns the value of the specified cell. If the cell contained a formula, the formula is evaluated before returning the row.- Parameters:
rownum- row indexcellNum- column index- Returns:
- value of cell
-
getValue
Returns the value of the specified cell. If the cell contained a formula, the formula is evaluated before returning the row.- Parameters:
row- row objectcellNum- column index- Returns:
- value of cell
-
getValue
Returns the value of the specified cell. If the cell contained a formula, the formula is evaluated before returning the row.- Parameters:
cell- cell object- Returns:
- value of cell
-
evaluateCellValue
Returns the evaluated cell content. This assumes the cell contains a formula.- Parameters:
cell- cell to evaluate- Returns:
- cell value
-
getFormulaEvaluator
public org.apache.poi.ss.usermodel.FormulaEvaluator getFormulaEvaluator()Returns a formula evaluator for the current workbook. This is for convinience.- Returns:
- the formula evaluator
-
isEvaluateFormulas
public boolean isEvaluateFormulas()Returns whether formulas shall be evaluated or not (default istrue).- Returns:
truewhen formulas are evaluated
-
setEvaluateFormulas
public void setEvaluateFormulas(boolean evaluateFormulas) Sets whether formulas shall be evaluated or not (default istrue).- Parameters:
evaluateFormulas-trueorfalse
-
isJavaTimeEnabled
public boolean isJavaTimeEnabled()Returns whether date/time values shall be returns as LocalDateTime object or simple Dates.- Returns:
- whether date/time values shall be returns as LocalDateTime object or simple Dates
-
setJavaTimeEnabled
public void setJavaTimeEnabled(boolean javaTimeEnabled) Sets whether date/time values shall be returns as LocalDateTime object or simple Dates.- Parameters:
javaTimeEnabled- whether date/time values shall be returns as LocalDateTime object or simple Dates
-
readHeaderRow
protected void readHeaderRow()Reads the header row from next line.- Overrides:
readHeaderRowin classAbstractTableReader- See Also:
-
retrieveNextRow
protected void retrieveNextRow()Retrieves the next row from the current sheet. The row is then internally stored for evaluation ofhasNext()andnext(). Blank rows are skipped when isSkipBlankRows() returntrue. -
rowHasOnlyBlankCells
protected boolean rowHasOnlyBlankCells(org.apache.poi.ss.usermodel.Row row) Checks whether row has only blank cells. The method is called fromretrieveNextRow().- Parameters:
row- the row to check- Returns:
- boolean when the row has only blank cells
-
getOrCreateRow
protected org.apache.poi.ss.usermodel.Row getOrCreateRow(int rowNum) Ensures that the sheet contains a row at the given index.- Parameters:
rowNum- index of row- Returns:
- the row from the sheet or a new blank row
-