|
| 1 | +# spring-batch-excel |
| 2 | + |
| 3 | +image::https://circleci.com/gh/mdeinum/spring-batch-excel.svg?style=svg[CircleCI] |
| 4 | +image::https://api.codacy.com/project/badge/Grade/c52a51f788a543a9964cb8a148cfcd92[Codacy Badge] |
| 5 | + |
| 6 | +Spring Batch extension which contains an `ItemReader` implementation for Excel based on https://poi.apache.org[Apache POI]. |
| 7 | + |
| 8 | +## Configuration |
| 9 | + |
| 10 | +Next to the https://docs.spring.io/spring-batch/reference/html/configureJob.html[configuration of Spring Batch] one needs to configure the `PoiItemReader`. |
| 11 | + |
| 12 | +Configuration of can be done in XML or Java Config. |
| 13 | + |
| 14 | +### XML |
| 15 | + |
| 16 | +```xml |
| 17 | + <bean id="excelReader" class="biz.deinum.batch.item.excel.poi.PoiItemReader" scope="step"> |
| 18 | + <property name="resource" value="file:/path/to/your/excel/file" /> |
| 19 | + <property name="rowMapper"> |
| 20 | + <bean class="PassThroughRowMapper" /> |
| 21 | + </property> |
| 22 | + </bean> |
| 23 | +``` |
| 24 | + |
| 25 | +### Java Config |
| 26 | + |
| 27 | +```java |
| 28 | +@Bean |
| 29 | +@StepScope |
| 30 | +public PoiItemReader excelReader() { |
| 31 | + PoiItemReader reader = new PoiItemReader(); |
| 32 | + reader.setResource(new FileSystemResource("/path/to/your/excel/file")); |
| 33 | + reader.setRowMapper(rowMapper()); |
| 34 | + return reader; |
| 35 | +} |
| 36 | + |
| 37 | +@Bean |
| 38 | +public RowMapper rowMapper() { |
| 39 | + return new PassThroughRowMapper(); |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +[cols="1,1,1,4"] |
| 44 | +.Properties for `PoiItemReader` |
| 45 | +|=== |
| 46 | +| Property | Required | Default | Description |
| 47 | + |
| 48 | +| `resource` | yes | `null` | Location of the excel file to read, can be any resource supported by Spring. |
| 49 | +| `rowMapper` | yes | `null` | transforms the rows read from the sheet(s) to an object which you can use in the rest of the process. |
| 50 | +| `skippedRowsCallback` | no | `null` | When rows are skipped an optional `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. |
| 51 | + |
| 52 | +| `linesToSkip` | no | 0 | The number of lines to skip, this applies to each sheet in the Excel file, can be useful if the first couple of lines provide header information. |
| 53 | + |
| 54 | +| `strict` | no | `true` | This controls wether or not an exception is thrown if the file doesn't exists or isn't readable, by default an exception will be thrown. |
| 55 | +| `rowSetFactory` | no | `DefaultRowSetFactory` | For reading rows a `RowSet` abstraction is used. To construct a `RowSet` for the current `Sheet` a `RowSetFactory` is needed. The `DefaultRowSetFactory` constructs a `DefaultRowSet` and `DefaultRowSetMetaData`. For construction of the latter a `ColumnNameExtractor` is needed. At the moment there are 2 implementations |
| 56 | + |
| 57 | +| `endAfterBlankLines` | no | 1 | The number of blank lines before stopping to read. |
| 58 | + |
| 59 | +|=== |
| 60 | + |
| 61 | + - `StaticColumnNameExtractor` uses a preset list of column names. |
| 62 | + - `RowNumberColumnNameExtractor` (**the default**) reads a given row (default 0) to determine the column names of the current sheet |
| 63 | + |
| 64 | +## RowMappers |
| 65 | +To map a read row a `RowMapper` is needed. Out-of-the-box there are 2 implementations. The `PassThroughRowMapper` and `BeanWrapperRowMapper`. |
| 66 | + |
| 67 | +### PassThroughRowMapper |
| 68 | +Transforms the read row from excel into a `String[]`. |
| 69 | + |
| 70 | +### BeanWrapperRowMapper |
| 71 | +Uses a `BeanWrapper` to convert a given row into an object. Uses the column names of the given `RowSet` to map column to properties of the `targetType` or prototype bean. |
| 72 | + |
| 73 | +```java |
| 74 | +<bean id="excelReader" class="biz.deinum.batch.item.excel.poi.PoiItemReader" scope="step"> |
| 75 | + <property name="resource" value="file:/path/to/your/excel/file" /> |
| 76 | + <property name="rowMapper"> |
| 77 | + <bean class="biz.deinum.batch.item.excel.mapping.BeanWrapperRowMapper"> |
| 78 | + <property name="targetType" value="com.your.package.Player" /> |
| 79 | + <bean> |
| 80 | + </property> |
| 81 | +</bean> |
| 82 | +``` |
0 commit comments