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
    Modifier and Type
    Field
    Description
    protected final org.apache.commons.logging.Log
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    protected void
     
    protected void
     
    protected T
     
    protected abstract int
    The number of sheets in the underlying workbook.
    protected abstract Sheet
    getSheet(int sheet)
    Get the sheet based on the given sheet index.
    protected void
    jumpToItem(int itemIndex)
    On restart this will increment rowSet to where job left off previously.
    protected abstract void
    openExcelFile(org.springframework.core.io.Resource resource, String password)
    Opens the excel file and reads the file and sheet metadata.
     
    void
    setEndAfterBlankLines(int endAfterBlankLines)
     
    void
    setLinesToSkip(int linesToSkip)
    Set the number of lines to skip.
    void
    setPassword(String password)
    The password used to protect the file to open.
    void
    setResource(org.springframework.core.io.Resource resource)
    Public setter for the input resource.
    void
    setRowMapper(RowMapper<T> rowMapper)
    Public setter for the rowMapper.
    void
    Public setter for the rowSetFactory.
    void
    Set the callback handler to call when a row is being skipped.
    void
    setStrict(boolean strict)
    In strict mode the reader will throw an exception on AbstractItemCountingItemStreamItemReader.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, update

    Methods inherited from class org.springframework.batch.item.ItemStreamSupport

    getExecutionContextKey, setExecutionContextName, setName

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods 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

      public T read() throws Exception
      Specified by:
      read in interface org.springframework.batch.item.ItemReader<T>
      Overrides:
      read in class org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader<T>
      Throws:
      Exception
    • doRead

      protected T doRead()
      Specified by:
      doRead in class org.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 configured RowMapper so we can use the #doRead method and reuse the logic in there, but without actually map to instances (this to save memory and have better performance).
      Overrides:
      jumpToItem in class org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader<T>
    • doOpen

      protected void doOpen() throws Exception
      Specified by:
      doOpen in class org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader<T>
      Throws:
      Exception
    • doClose

      protected void doClose() throws Exception
      Specified by:
      doClose in class org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader<T>
      Throws:
      Exception
    • setResource

      public void setResource(org.springframework.core.io.Resource resource)
      Public setter for the input resource.
      Specified by:
      setResource in interface org.springframework.batch.item.file.ResourceAwareItemReaderItemStream<T>
      Parameters:
      resource - the Resource pointing to the Excelfile
    • afterPropertiesSet

      public void afterPropertiesSet() throws Exception
      Specified by:
      afterPropertiesSet in interface org.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

      protected abstract Sheet getSheet(int sheet)
      Get the sheet based on the given sheet index.
      Parameters:
      sheet - the sheet index
      Returns:
      the sheet or null when 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 a Resource to read the sheets, this file can optionally be password protected.
      Parameters:
      resource - Resource pointing to the Excel file to read
      password - 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 on AbstractItemCountingItemStreamItemReader.open(org.springframework.batch.item.ExecutionContext) if the input resource does not exist.
      Parameters:
      strict - true by default
    • setRowMapper

      public void setRowMapper(RowMapper<T> rowMapper)
      Public setter for the rowMapper. Used to map a row read from the underlying Excel workbook.
      Parameters:
      rowMapper - the RowMapper to use.
    • setRowSetFactory

      public void setRowSetFactory(RowSetFactory rowSetFactory)
      Public setter for the rowSetFactory. Used to create a RowSet implemenation. By default the DefaultRowSetFactory is used.
      Parameters:
      rowSetFactory - the RowSetFactory to use.
    • setSkippedRowsCallback

      public void setSkippedRowsCallback(RowCallbackHandler skippedRowsCallback)
      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

      public void setPassword(String password)
      The password used to protect the file to open.
      Parameters:
      password - the password