Class AbstractExcelItemReader<T>
java.lang.Object
org.springframework.batch.item.ItemStreamSupport
org.springframework.batch.item.support.AbstractItemStreamItemReader<T>
org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader<T>
org.springframework.batch.extensions.excel.AbstractExcelItemReader<T>
- Type Parameters:
T- the type
- All Implemented Interfaces:
org.springframework.batch.item.file.ResourceAwareItemReaderItemStream<T>,org.springframework.batch.item.ItemReader<T>,org.springframework.batch.item.ItemStream,org.springframework.batch.item.ItemStreamReader<T>,org.springframework.beans.factory.InitializingBean
- Direct Known Subclasses:
PoiItemReader,StreamingXlsxItemReader
public abstract class AbstractExcelItemReader<T>
extends org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader<T>
implements org.springframework.batch.item.file.ResourceAwareItemReaderItemStream<T>, org.springframework.beans.factory.InitializingBean
ItemReader implementation to read an Excel file.
It will read the file sheet for sheet and row for row. It is loosy based on the
FlatFileItemReader- Since:
- 0.1.0
- Author:
- Marten Deinum
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidprotected voiddoClose()protected voiddoOpen()protected TdoRead()protected abstract intThe number of sheets in the underlying workbook.protected abstract SheetgetSheet(int sheet) Get the sheet based on the given sheet index.protected voidjumpToItem(int itemIndex) On restart this will increment rowSet to where job left off previously.protected abstract voidopenExcelFile(org.springframework.core.io.Resource resource, String password) Opens the excel file and reads the file and sheet metadata.read()voidsetEndAfterBlankLines(int endAfterBlankLines) voidsetLinesToSkip(int linesToSkip) Set the number of lines to skip.voidsetPassword(String password) The password used to protect the file to open.voidsetResource(org.springframework.core.io.Resource resource) Public setter for the input resource.voidsetRowMapper(RowMapper<T> rowMapper) Public setter for therowMapper.voidsetRowSetFactory(RowSetFactory rowSetFactory) Public setter for therowSetFactory.voidsetSkippedRowsCallback(RowCallbackHandler skippedRowsCallback) Set the callback handler to call when a row is being skipped.voidsetStrict(boolean strict) In strict mode the reader will throw an exception onAbstractItemCountingItemStreamItemReader.open(org.springframework.batch.item.ExecutionContext)if the input resource does not exist.Methods inherited from class org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader
close, getCurrentItemCount, isSaveState, open, setCurrentItemCount, setMaxItemCount, setSaveState, updateMethods inherited from class org.springframework.batch.item.ItemStreamSupport
getExecutionContextKey, setExecutionContextName, setNameMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.batch.item.ItemStream
close, open, update
-
Field Details
-
logger
protected final org.apache.commons.logging.Log logger
-
-
Constructor Details
-
AbstractExcelItemReader
public AbstractExcelItemReader()
-
-
Method Details
-
read
-
doRead
- Specified by:
doReadin classorg.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader<T>
-
jumpToItem
protected void jumpToItem(int itemIndex) On restart this will increment rowSet to where job left off previously. Temporarily switch out the configuredRowMapperso we can use the#doReadmethod and reuse the logic in there, but without actually map to instances (this to save memory and have better performance).- Overrides:
jumpToItemin classorg.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader<T>
-
doOpen
-
doClose
-
setResource
public void setResource(org.springframework.core.io.Resource resource) Public setter for the input resource.- Specified by:
setResourcein interfaceorg.springframework.batch.item.file.ResourceAwareItemReaderItemStream<T>- Parameters:
resource- theResourcepointing to the Excelfile
-
afterPropertiesSet
- Specified by:
afterPropertiesSetin interfaceorg.springframework.beans.factory.InitializingBean- Throws:
Exception
-
setLinesToSkip
public void setLinesToSkip(int linesToSkip) Set the number of lines to skip. This number is applied to all worksheet in the excel file! default to 0- Parameters:
linesToSkip- number of lines to skip
-
getSheet
Get the sheet based on the given sheet index.- Parameters:
sheet- the sheet index- Returns:
- the sheet or
nullwhen no sheet available.
-
getNumberOfSheets
protected abstract int getNumberOfSheets()The number of sheets in the underlying workbook.- Returns:
- the number of sheets.
-
openExcelFile
protected abstract void openExcelFile(org.springframework.core.io.Resource resource, String password) throws Exception Opens the excel file and reads the file and sheet metadata. Uses aResourceto read the sheets, this file can optionally be password protected.- Parameters:
resource-Resourcepointing to the Excel file to readpassword- optional password- Throws:
Exception- when the Excel sheet cannot be accessed
-
setStrict
public void setStrict(boolean strict) In strict mode the reader will throw an exception onAbstractItemCountingItemStreamItemReader.open(org.springframework.batch.item.ExecutionContext)if the input resource does not exist.- Parameters:
strict- true by default
-
setRowMapper
Public setter for therowMapper. Used to map a row read from the underlying Excel workbook.- Parameters:
rowMapper- theRowMapperto use.
-
setRowSetFactory
Public setter for therowSetFactory. Used to create aRowSetimplemenation. By default theDefaultRowSetFactoryis used.- Parameters:
rowSetFactory- theRowSetFactoryto use.
-
setSkippedRowsCallback
Set the callback handler to call when a row is being skipped.- Parameters:
skippedRowsCallback- will be called for each one of the initial skipped lines before any items are read.
-
setEndAfterBlankLines
public void setEndAfterBlankLines(int endAfterBlankLines) -
setPassword
The password used to protect the file to open.- Parameters:
password- the password
-