Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.deanwong:spring-batch-excel:0.5.5'
}
dependencies {
implementation("com.github.deanwong:spring-batch-excel:0.5.5")
}
<dependency>
<groupId>com.github.deanwong</groupId>
<artifactId>spring-batch-excel</artifactId>
<version>0.5.5</version>
</dependency>
libraryDependencies += "com.github.deanwong" % "spring-batch-excel" % "0.5.5"
:dependencies [[com.github.deanwong/spring-batch-excel "0.5.5"]]
Spring Batch extension which contains ItemReader
implementations for Excel. Support for both JExcel and Apache POI is available. Simple xls documents can be read with both implementations, however for reading the newer xlsx format Apache POI is required.
Next to the configuration of Spring Batch one needs to configure the ItemReader
for the desired framework.
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.
<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>
@Bean
public PoiItemReader excelReader() {
PoiItemReader reader = new PoiItemReader();
reader.setResource(new ClassPathResource("/path/to/your/excel/file"));
reader.setRowMapper(rowMapper());
return reader;
}
@Bean
public RowMapper rowMapper() {
return new PassThroughRowMapper();
}
Each reader takes a resource
and a rowMapper
. The resource
is the location of the excel file to read and the rowMapper
transforms the rows in excel to an object which you can use in the rest of the process.
Optionally one can also set the skippedRowsCallback
, linesToSkip
, strict
and rowSetFactory
properties.
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.
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.
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.
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
StaticColumnNameExtractor
uses a preset list of column names.RowNumberColumnNameExtractor
(the default) reads a given row (default 0) to determine the column names of the current sheetNext to the default ItemReader
implementations there are also 2 RowMapper
implementations.
Transforms the read row from excel into a String[]
.
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.
<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.BeanWrapperowMapper">
<property name="targetType" value="com.your.package.Player" />
<bean>
</property>
</bean>