Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@

package org.springframework.batch.extensions.excel;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -63,6 +67,8 @@ public abstract class AbstractExcelItemReader<T> extends AbstractItemCountingIte

private RowSetFactory rowSetFactory = new DefaultRowSetFactory();

private Set<String> sheetNames = Collections.emptySet();

private RowSet rs;

private String password;
Expand Down Expand Up @@ -188,6 +194,10 @@ protected void doOpen() throws Exception {
private boolean nextSheet() {
while (this.currentSheet < this.getNumberOfSheets()) {
final Sheet sheet = this.getSheet(this.currentSheet);
if (!this.sheetNames.isEmpty() && !this.sheetNames.contains(sheet.getName())) {
this.currentSheet++;
continue;
}
this.rs = this.rowSetFactory.create(sheet);
if (this.logger.isDebugEnabled()) {
this.logger.debug("Opening sheet " + sheet.getName() + ".");
Expand All @@ -214,6 +224,16 @@ protected void doClose() throws Exception {
this.rs = null;
}

/**
* Select sheets by name. By default, the reader processes every sheet in workbook
* order.
* @param sheetNames sheet names to process, or no names to process every sheet
*/
public void setSheetNames(String... sheetNames) {
Assert.noNullElements(sheetNames, "Sheet names must not contain null elements");
this.sheetNames = new HashSet<>(Arrays.asList(sheetNames));
}

/**
* Public setter for the input resource.
* @param resource the {@code Resource} pointing to the Excelfile
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2026 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.batch.extensions.excel;

import java.util.ArrayList;
import java.util.List;

import org.junit.jupiter.api.Test;

import org.springframework.batch.extensions.excel.mapping.PassThroughRowMapper;
import org.springframework.batch.infrastructure.item.ExecutionContext;

import static org.assertj.core.api.Assertions.assertThat;

class SheetSelectionTests {

@Test
void readsAllSheetsByDefault() throws Exception {
assertThat(read(reader())).containsExactly("first", "second", "third");
}

@Test
void readsSelectedSheetsInWorkbookOrder() throws Exception {
MockExcelItemReader<String[]> reader = reader();
reader.setSheetNames("third", "first");
assertThat(read(reader)).containsExactly("first", "third");
}

@Test
void readsNoRowsWhenNoSheetMatches() throws Exception {
MockExcelItemReader<String[]> reader = reader();
reader.setSheetNames("missing");
assertThat(read(reader)).isEmpty();
}

private MockExcelItemReader<String[]> reader() {
List<MockSheet> sheets = new ArrayList<>(List.of(sheet("first"), sheet("second"), sheet("third")));
MockExcelItemReader<String[]> reader = new MockExcelItemReader<>(sheets);
reader.setRowMapper(new PassThroughRowMapper());
reader.afterPropertiesSet();
return reader;
}

private MockSheet sheet(String name) {
return new MockSheet(name, List.<String[]>of(new String[] { name }));
}

private List<String> read(MockExcelItemReader<String[]> reader) throws Exception {
List<String> rows = new ArrayList<>();
reader.open(new ExecutionContext());
try {
String[] row;
while ((row = reader.read()) != null) {
rows.add(row[0]);
}
return rows;
}
finally {
reader.close();
}
}

}