org.gageot.excel.core
Class ExcelTemplate

java.lang.Object
  extended by org.gageot.excel.core.ExcelTemplate
All Implemented Interfaces:
org.springframework.beans.factory.InitializingBean

public class ExcelTemplate
extends java.lang.Object
implements org.springframework.beans.factory.InitializingBean

This is the central class in the Excel core package. It simplifies the use of Excel and helps to avoid common errors. It executes core Excel parsing workflow, leaving application code extract results.

Code using this class need only implement callback interfaces, giving them a clearly defined contract. The RowCallbackHandler interface extracts values from each row of a HSSFSheet.

Can be used within a service implementation via direct instantiation with a Resource reference, or get prepared in an application context and given to services as bean reference.

The motivation and design of this class is inspired from JdbcTemplate.

Because this class is parameterizable by the callback interfaces, it isn't necessary to subclass it.

Author:
David Gageot
See Also:
SheetExtractor, RowCallbackHandler, RowMapper

Constructor Summary
ExcelTemplate()
          Construct a new ExcelTemplate for bean usage.
ExcelTemplate(java.io.File aFile)
          Construct a new ExcelTemplate, given an Excel File.
ExcelTemplate(org.springframework.core.io.Resource aResource)
          Construct a new ExcelTemplate, given a Resource to obtain the Excel stream from.
ExcelTemplate(java.lang.String aPath, java.lang.Class aClass)
          Construct a new ExcelTemplate, given a path to an Excel file.
 
Method Summary
 void afterPropertiesSet()
           
 org.springframework.core.io.Resource getResource()
          Set the Resource to obtain the Excel stream from.
 java.lang.String[][] read(java.lang.String sheetName)
           
 void read(java.lang.String sheetName, CellCallbackHandler cellCallbackHandler)
           
 java.lang.Object[][] read(java.lang.String sheetName, CellMapper cellMapper)
           
 java.lang.Object[][] read(java.lang.String sheetName, CellMapper cellMapper, java.lang.Class clazz)
           
 void read(java.lang.String sheetName, RowCallbackHandler rowCallbackHandler)
           
 java.lang.Object read(java.lang.String sheetName, SheetExtractor sheetExtractor)
          Read the content of an Excel file for a given sheet name.
 java.util.List readBeans(java.lang.String sheetName, java.lang.Class clazz)
           
 java.util.List readList(java.lang.String sheetName)
           
 java.util.List readList(java.lang.String sheetName, CellMapper cellMapper)
           
 java.util.List readList(java.lang.String sheetName, RowMapper rowMapper)
           
 void setResource(org.springframework.core.io.Resource aResource)
          Return the Resource used by this template.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ExcelTemplate

public ExcelTemplate()
Construct a new ExcelTemplate for bean usage. Note: The Resource has to be set before using the instance. This constructor can be used to prepare a ExcelTemplate via a BeanFactory, typically setting the Resource via setResource.

See Also:
setResource(org.springframework.core.io.Resource)

ExcelTemplate

public ExcelTemplate(org.springframework.core.io.Resource aResource)
Construct a new ExcelTemplate, given a Resource to obtain the Excel stream from.

Parameters:
aResource - Resource to obtain the Excel stream from

ExcelTemplate

public ExcelTemplate(java.io.File aFile)
Construct a new ExcelTemplate, given an Excel File.

Parameters:
aFile - Excel file

ExcelTemplate

public ExcelTemplate(java.lang.String aPath,
                     java.lang.Class aClass)
Construct a new ExcelTemplate, given a path to an Excel file. The path can be relative to the given class, or absolute within the classpath via a leading slash.

Parameters:
aPath - relative or absolute path within the class path
aClass - the class to load resources with
See Also:
Class.getResourceAsStream(java.lang.String)
Method Detail

read

public java.lang.Object read(java.lang.String sheetName,
                             SheetExtractor sheetExtractor)
                      throws org.springframework.dao.DataAccessException
Read the content of an Excel file for a given sheet name. The content of the sheet is extracted using SheetExtractor.

Parameters:
sheetName - name of the excel sheet
sheetExtractor - object that will extract results
Returns:
an arbitrary result object, as returned by the ResultSetExtractor
Throws:
org.springframework.dao.DataAccessException - if there is any problem

read

public java.lang.String[][] read(java.lang.String sheetName)
                          throws org.springframework.dao.DataAccessException
Throws:
org.springframework.dao.DataAccessException

read

public java.lang.Object[][] read(java.lang.String sheetName,
                                 CellMapper cellMapper)
                          throws org.springframework.dao.DataAccessException
Throws:
org.springframework.dao.DataAccessException

read

public java.lang.Object[][] read(java.lang.String sheetName,
                                 CellMapper cellMapper,
                                 java.lang.Class clazz)
                          throws org.springframework.dao.DataAccessException
Throws:
org.springframework.dao.DataAccessException

readList

public java.util.List readList(java.lang.String sheetName,
                               RowMapper rowMapper)
                        throws org.springframework.dao.DataAccessException
Throws:
org.springframework.dao.DataAccessException

readList

public java.util.List readList(java.lang.String sheetName)
                        throws org.springframework.dao.DataAccessException
Throws:
org.springframework.dao.DataAccessException

readList

public java.util.List readList(java.lang.String sheetName,
                               CellMapper cellMapper)
                        throws org.springframework.dao.DataAccessException
Throws:
org.springframework.dao.DataAccessException

read

public void read(java.lang.String sheetName,
                 RowCallbackHandler rowCallbackHandler)
          throws org.springframework.dao.DataAccessException
Throws:
org.springframework.dao.DataAccessException

read

public void read(java.lang.String sheetName,
                 CellCallbackHandler cellCallbackHandler)
          throws org.springframework.dao.DataAccessException
Throws:
org.springframework.dao.DataAccessException

readBeans

public java.util.List readBeans(java.lang.String sheetName,
                                java.lang.Class clazz)
                         throws org.springframework.dao.DataAccessException
Throws:
org.springframework.dao.DataAccessException

getResource

public org.springframework.core.io.Resource getResource()
Set the Resource to obtain the Excel stream from.


setResource

public void setResource(org.springframework.core.io.Resource aResource)
Return the Resource used by this template.


afterPropertiesSet

public void afterPropertiesSet()
Specified by:
afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean


Copyright © 2007 gageot.net. All Rights Reserved.