Skip to content

Commit 66d387e

Browse files
authored
Merge pull request #144 from spring-projects/excel-documentation
Update documentation
2 parents 6335a69 + 4fb5fb9 commit 66d387e

File tree

2 files changed

+149
-4
lines changed

2 files changed

+149
-4
lines changed

spring-batch-excel/README.adoc

+87-4
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,98 @@ Uses a `BeanWrapper` to convert a given row into an object. Uses the column name
128128
</bean>
129129
----
130130

131+
or the same in Java configuration.
132+
133+
[source,java]
134+
----
135+
@Bean
136+
public PoiItemReader excelReader(BeanWrapperRowMapper rowMapper) {
137+
var excelReader = new PoiItemReader();
138+
excelReader.setResource(new FileSystemResouce("file:/path/to/your/excel/file"));
139+
excelReader.setRowMapper(rowMapper);
140+
return excelReader;
141+
}
142+
143+
@Bean
144+
public BeanWrapperRowMapper rowMapper() {
145+
var rowMapper = new BeanWrapperRowMapper<Player>();
146+
rowMapper.setTargetType(Player.class);
147+
return rowMapper;
148+
}
149+
----
150+
151+
NOTE: When using the `BeanWrapperRowMapper` with the `StreamingXlsxItemReader` it is required to use the `StaticColumnNameExtractor` to provide the column names for mapping purposes. The reason for this is that we cannot read a specific row while streaming the results.
152+
153+
[source,xml]
154+
----
155+
<bean id="excelReader" class="org.springframework.batch.extensions.excel.streaming.StreamingXlsxItemReader" scope="step">
156+
<property name="resource" value="file:/path/to/your/excel/file" />
157+
<property name="rowMapper">
158+
<bean class="org.springframework.batch.extensions.excel.mapping.BeanWrapperRowMapper">
159+
<property name="targetType" value="org.springframework.batch.extensions.excel.Player" />
160+
</bean>
161+
</property>
162+
<property name="rowSetFactory">
163+
<bean class="org.springframework.batch.extensions.excel.support.rowset.DefaultRowSetFactory">
164+
<property name="columnNameExtractor">
165+
<bean class="org.springframework.batch.extensions.excel.support.rowset.StaticColumnNameExtractor">
166+
<constructor-arg value="id,position,lastName,firstName,birthYear,debutYear,comment" />
167+
</bean>
168+
</property>
169+
</bean>
170+
</property>
171+
</bean>
172+
----
173+
174+
And the same in Java Configuration.
175+
176+
[source,java]
177+
----
178+
@Bean
179+
public StreamingXlsxItemReader excelReader(BeanWrapperRowMapper rowMapper) {
180+
var columns = new String[] {"id", "position", "lastName", "firstName", "birthYear", "debutYear","comment"};
181+
var columnNameExtractor = new StaticColumnNameExtractor(columns);
182+
var rowSetFactory = new DefaultRowSetFactory();
183+
rowSetFactory.setColumnNameExtractor(columnNameExtractor);
184+
185+
var excelReader = new StreamingXlsxItemReader();
186+
excelReader.setResource(new FileSystemResouce("file:/path/to/your/excel/file"));
187+
excelReader.setRowMapper(rowMapper);
188+
excelReader.set
189+
return excelReader;
190+
}
191+
192+
@Bean
193+
public BeanWrapperRowMapper rowMapper() {
194+
var rowMapper = new BeanWrapperRowMapper<Player>();
195+
rowMapper.setTargetType(Player.class);
196+
return rowMapper;
197+
}
198+
----
199+
131200
== Frequently Asked Questions
132201

133202
=== Not able to open large Excel
134203
When opening large Excel files or Excel files with large amounts of data in a single cell it might fail with an error
135204

136-
```
205+
[source]
206+
----
137207
"Unexpected error Tried to allocate an array of length 162,386,364, but the maximum length for this record type is 100,000,000. If the file is not corrupt or large, please open an issue on bugzilla to request increasing the maximum allowable size for this record type. As a temporary workaround, consider setting a higher override value with IOUtils.setByteArrayMaxOverride()"
138-
```
208+
----
139209

140-
This is due to the maximum lenght for certain datatypes is limited. To prevent this from happening you can use the `IOUtils.setByteArrayMaxOverride()` method to increase the allowed size. It is however important that this is set before anything POI related has been processed/configured.
210+
This is due to the maximum lenght for certain datatypes is limited. To prevent this from happening you can use the `IOUtils.setByteArrayMaxOverride()` method to increase the allowed size. It is however important that this is set _before_ anything POI related has been processed/configured.
141211

142-
Ideally, when using Spring Boot, you can set this before launching the application or by putting this in a `static {}` initializer block of the Spring Batch job configuration.
212+
Ideally, when using Spring Boot, you can set this before launching the application or by putting this in a `static {}` initializer block of the Spring Batch job configuration.
213+
214+
[source,java]
215+
----
216+
import org.apache.poi.util.IOUtils;
217+
@SpringBootApplication
218+
public class MyBatchApplication {
219+
220+
public static void main(String[] args) {
221+
IOUtils.setByteArrayMaxOverride(Integer.MAX_VALUE);
222+
SpringApplication.run(MyBatchApplication.class, args);
223+
}
224+
}
225+
----
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.batch.extensions.excel.streaming;
18+
19+
import java.util.Locale;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
import org.springframework.batch.extensions.excel.Player;
24+
import org.springframework.batch.extensions.excel.ReflectionTestUtils;
25+
import org.springframework.batch.extensions.excel.mapping.BeanWrapperRowMapper;
26+
import org.springframework.batch.extensions.excel.support.rowset.DefaultRowSetFactory;
27+
import org.springframework.batch.extensions.excel.support.rowset.StaticColumnNameExtractor;
28+
import org.springframework.batch.item.ExecutionContext;
29+
import org.springframework.core.io.ClassPathResource;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
33+
class StreamingXlsxMappingTests {
34+
35+
@Test
36+
void readAndMapRowsUsingRowMapper() throws Exception {
37+
var columns = new String[] {"id", "position", "lastName", "firstName", "birthYear", "debutYear", "comment"};
38+
var rowSetFactory = new DefaultRowSetFactory();
39+
rowSetFactory.setColumnNameExtractor(new StaticColumnNameExtractor(columns));
40+
41+
var mapper = new BeanWrapperRowMapper<Player>();
42+
mapper.setTargetType(Player.class);
43+
44+
var reader = new StreamingXlsxItemReader<Player>();
45+
reader.setResource(new ClassPathResource("player.xlsx"));
46+
reader.setRowSetFactory(rowSetFactory);
47+
reader.setRowMapper(mapper);
48+
reader.setLinesToSkip(1); // Skip header
49+
reader.setUserLocale(Locale.US); // Use a Locale to not be dependent on environment
50+
reader.afterPropertiesSet();
51+
52+
reader.open(new ExecutionContext());
53+
Player row;
54+
do {
55+
row = reader.read();
56+
}
57+
while (row != null);
58+
59+
Integer readCount = (Integer) ReflectionTestUtils.getField(reader, "currentItemCount");
60+
assertThat(readCount).isEqualTo(4321);
61+
}
62+
}

0 commit comments

Comments
 (0)