Added readme.

This commit is contained in:
Marten Deinum
2014-03-24 20:42:47 +01:00
committed by Michael Minella
parent bec581912f
commit 3f89cb5869
18 changed files with 68 additions and 7 deletions

View File

@@ -0,0 +1,49 @@
# spring-batch-excel
Spring Batch extension which contains ItemReader implementations for Excel. Support for both JExcel and Apache POI is available.
## Configuration
Next to the [configuration of Spring Batch](http://docs.spring.io/spring-batch/2.2.x/reference/html/configureJob.html) one needs to configure an `ItemReader` which knows how to read an Excel file.
There are 2 `ItemReaders` one can configure:
- `org.springframework.batch.item.excel.jxl.JxlItemReader`
- `org.springframework.batch.item.excel.poi.PoiItemReader`
Configuration of both readers is the same, the difference is the used technology ([JExcel](http://jexcelapi.sourceforge.net) or [Apache Poi](http://poi.apache.org)) and cababilities of both technologies (JExcel cannot read xlsx files, whereas Apache POI can read those).
<bean id="excelReader" class="org.springframework.batch.item.excel.poi.PoiItemReader">
<property name="resource" value="/path/to/your/excel/file" />
<property name="rowMapper">
<bean class="org.springframework.batch.item.excel.mapping.PassThroughRowMapper" />
</property>
</bean>
Each reader takes a `resource` which is the excel file to read and a `rowMapper` which transforms the row in excel to an object which you can use in the rest of the process.
The project has 2 default `org.springframework.batch.item.excel.RowMapper` implementations.
Optionally one can also set the `skippedRowsCallback`, `linesToSkip` and `strict` property.
##### skippedRowsCallback
When rows are skipped an optional `org.springframework.batch.item.excel.RowCallbackHandler` is called with the skipped row. This comes in handy when one needs to write the skipped rows to another file or create some logging.
##### linesToSkip
The number of lines to skip, this applies to each sheet in the Excel file.
##### strict
By default `true`. This controls wether or not an exception is thrown if the file doesn't exists, by default an exception will be thrown.
### PassThroughRowMapper
Transforms the read row from excel into a `String[]`.
### DefaultRowMapper
Tries to convert the given row into an object, for this a `org.springframework.batch.item.excel.transform.RowTokenizer` and a `org.springframework.batch.item.file.mapping.FieldSetMapper` (from [the Spring Batch project](http://docs.spring.io/spring-batch/2.2.x/reference/html/readersAndWriters.html#fieldSetMapper)) is needed.
By default the `org.springframework.batch.item.excel.transform.DefaultRowTokenizer` is used. This implementation assumes that the first row contains the column names. The column names are used to map to attributes on the object we are trying to create. How this mapping is done is configurable through a `org.springframework.batch.item.excel.transform.ColumnToAttributeConverter` implementation.
Again 2 implementations are provided by the framework
- `org.springframework.batch.item.excel.transform.PassThroughColumnToAttributeConverter`
- `org.springframework.batch.item.excel.transform.MappingColumnToAttributeConverter`
The `PassThroughColumnToAttributeConverter` simply assumes that the column name corresponds to a property on the object that is being created.
The `MappingColumnToAttributeConverter` contains a (optional) column-name to property-name mapping.

View File

@@ -35,6 +35,7 @@ import java.io.InputStream;
*
* @param <T> the type
* @author Marten Deinum
* @since 1.1.0
*/
public abstract class AbstractExcelItemReader<T> extends AbstractItemCountingItemStreamItemReader<T> implements
ResourceAwareItemReaderItemStream<T>, InitializingBean {

View File

@@ -23,7 +23,7 @@ import org.springframework.batch.item.ParseException;
* simply dependencies to make it is generic as possible.
*
* @author Marten Deinum
*
* @since 1.0.0
*/
public class ExcelFileParseException extends ParseException {

View File

@@ -20,6 +20,7 @@ package org.springframework.batch.item.excel;
* Callback to handle skipped lines. Useful for header/footer processing.
*
* @author Marten Deinum
* @since 1.1.0
*/
public interface RowCallbackHandler {

View File

@@ -19,6 +19,7 @@ package org.springframework.batch.item.excel;
* Map rows from an excel sheet to an object.
*
* @author Marten Deinum
* @since 1.0.0
*
* @param <T>
*/

View File

@@ -19,6 +19,7 @@ package org.springframework.batch.item.excel;
* Interface to wrap different Excel implementations like JExcel, JXL or Apache POI.
*
* @author Marten Deinum
* @since 1.0.0
*
*/
public interface Sheet {

View File

@@ -30,7 +30,8 @@ import org.springframework.util.ClassUtils;
* the {@link org.springframework.batch.item.file.FlatFileItemReader}
*
* @author Marten Deinum
*
* @since 1.0.0
*
* @param <T> the type
*/
public class JxlItemReader<T> extends AbstractExcelItemReader<T> {

View File

@@ -24,7 +24,7 @@ import org.springframework.batch.item.excel.Sheet;
* {@link org.springframework.batch.item.excel.Sheet} implementation for JXL.
*
* @author Marten Deinum
*
* @since 1.0.0
*/
public class JxlSheet implements Sheet {

View File

@@ -27,7 +27,7 @@ import java.util.List;
* Class containing utility methods to work with JXL.
*
* @author Marten Deinum
*
* @since 1.0.0
*/
public final class JxlUtils {

View File

@@ -30,6 +30,7 @@ import org.springframework.util.Assert;
* of fields and construction of objects.
*
* @author Marten Deinum
* @since 1.0.0
*
* @param <T>
*/

View File

@@ -23,7 +23,8 @@ import org.springframework.batch.item.excel.Sheet;
* back directly rather than a mapped object.
*
* @author Marten Deinum
*
* @since 1.0.0
*
*/
public class PassThroughRowMapper implements RowMapper<String[]> {

View File

@@ -28,6 +28,7 @@ import org.springframework.core.io.Resource;
* the {@link org.springframework.batch.item.file.FlatFileItemReader}
*
* @author Marten Deinum
* @since 1.2.0
*
* @param <T> the type
*/

View File

@@ -28,7 +28,7 @@ import java.util.List;
* Sheet implementation for Apache POI.
*
* @author Marten Deinum
*
* @since 1.2.0
*/
public class PoiSheet implements Sheet {

View File

@@ -18,6 +18,7 @@
* Convert a column name to an attribute name and vice versa.
*
* @author Marten Deinum
* @since 1.0.0
*/
public interface ColumnToAttributeConverter {

View File

@@ -27,6 +27,7 @@ import org.springframework.util.StringUtils;
* {@link RowTokenizer} which assumes the column names are on the first row in the sheet.
*
* @author Marten Deinum
* @since 1.0.0
*/
public class DefaultRowTokenizer implements RowTokenizer, InitializingBean {

View File

@@ -25,6 +25,7 @@ import java.util.Map;
* configuration. If a mapping cannot be found it returns the name as is.
*
* @author Marten Deinum
* @since 1.0.0
*/
public class MappingColumnToAttributeConverter implements ColumnToAttributeConverter {

View File

@@ -19,6 +19,7 @@ package org.springframework.batch.item.excel.transform;
* {@link ColumnToAttributeConverter} which simply returns the given value.
*
* @author Marten Deinum
* @since 1.0.0
*/
public class PassThroughColumnToAttributeConverter implements ColumnToAttributeConverter {

View File

@@ -22,8 +22,8 @@ import org.springframework.batch.item.file.transform.FieldSet;
* Interface that is used by framework to convert a Cell[] into a {@link org.springframework.batch.item.file.transform.FieldSet}.
*
* @author Marten Deinum
* @since 1.0.0
*/
public interface RowTokenizer {
FieldSet tokenize(Sheet sheet, String[] row);