Class ExcelReader<T>
- Type Parameters:
T- The type of the object that represents one Excel row
ExcelReader allows you to define how each Excel cell maps to your target object T,
and optionally integrates Bean Validation support.
Once configuration is complete, use build(InputStream) to create a ExcelReadHandler.
- Since:
- 2025-07-19
-
Constructor Summary
ConstructorsConstructorDescriptionExcelReader(Supplier<T> instanceSupplier) Constructs an ExcelReader in setter mode without Bean Validation.ExcelReader(Supplier<T> instanceSupplier, @Nullable jakarta.validation.Validator validator) Constructs an ExcelReader in setter mode with instance supplier and optional validator. -
Method Summary
Modifier and TypeMethodDescriptionbuild(InputStream inputStream) Finalizes the configuration and builds anExcelReadHandlerfor parsing the given Excel stream.column(String headerName, BiConsumer<T, CellData> setter) Registers a name-based column mapping.column(BiConsumer<T, CellData> setter) Registers a positional column mapping.columnAt(int columnIndex, BiConsumer<T, CellData> setter) Registers an index-based column mapping.static ExcelReader<Map<String, String>> forMap()Creates a reader that parses Excel files intoMap<String, String>rows by auto-discovering columns from the header row.static ExcelReader<Map<String, String>> Creates a reader that parses Excel files intoMap<String, String>rows, including only the specified columns.getSheetHeaders(InputStream inputStream, int sheetIndex, int headerRowIndex) Returns the header names from a specific sheet.static List<ExcelSheetInfo> getSheetNames(InputStream inputStream) Returns the list of sheet names and indices from an Excel file.headerRowIndex(int headerRowIndex) Sets the zero-based row index of the header row.static <T> ExcelReader<T> Creates an ExcelReader in mapping mode for immutable object construction.static <T> ExcelReader<T> Creates an ExcelReader in mapping mode with Bean Validation support.onProgress(int interval, ProgressCallback callback) Registers a progress callback that fires everyintervalrows during reading.Sets the password for reading encrypted Excel files.required()Marks the last registered column as required.static <T> ExcelReader<T> Creates an ExcelReader in setter mode.static <T> ExcelReader<T> Creates an ExcelReader in setter mode with Bean Validation.sheetIndex(int sheetIndex) Sets the zero-based sheet index to read from.Skips one column during reading by registering a no-op mapping at the next positional slot.skipColumns(int count) Skips the specified number of positional columns.
-
Constructor Details
-
ExcelReader
Constructs an ExcelReader in setter mode with instance supplier and optional validator.- Parameters:
instanceSupplier- A supplier to create new instances ofTfor each rowvalidator- Optional Bean Validation validator (nullable)
-
ExcelReader
Constructs an ExcelReader in setter mode without Bean Validation.- Parameters:
instanceSupplier- A supplier to create new instances ofTfor each row
-
-
Method Details
-
setter
Creates an ExcelReader in setter mode. Symmetric withmapping(Function)andforMap()— all three read modes start with a static factory.ExcelReader.setter(User::new) .column("Name", (u, cell) -> u.name = cell.asString()) .build(inputStream) .read(result -> { ... });- Type Parameters:
T- The row data type- Parameters:
instanceSupplier- A supplier to create new instances ofTfor each row- Returns:
- A new ExcelReader configured in setter mode
- Since:
- 0.14.0
-
setter
public static <T> ExcelReader<T> setter(Supplier<T> instanceSupplier, @Nullable jakarta.validation.Validator validator) Creates an ExcelReader in setter mode with Bean Validation.- Type Parameters:
T- The row data type- Parameters:
instanceSupplier- A supplier to create new instances ofTfor each rowvalidator- Bean Validation validator- Returns:
- A new ExcelReader configured in setter mode
- Since:
- 0.14.0
-
mapping
Creates an ExcelReader in mapping mode for immutable object construction.In this mode, each row is passed as a
RowDatato the mapping function, which creates the target object in a single step. Column definitions are not needed — access columns by header name or index within the mapping function.ExcelReader.mapping(row -> new PersonRecord( row.get("Name").asString(), row.get("Age").asInt(), row.get("Email").asString() )).build(inputStream).read(result -> { ... });- Type Parameters:
T- The type of the object that represents one Excel row- Parameters:
rowMapper- A function that creates an instance ofTfrom aRowData- Returns:
- A new ExcelReader configured in mapping mode
-
mapping
public static <T> ExcelReader<T> mapping(Function<RowData, T> rowMapper, @Nullable jakarta.validation.Validator validator) Creates an ExcelReader in mapping mode with Bean Validation support.- Type Parameters:
T- The type of the object that represents one Excel row- Parameters:
rowMapper- A function that creates an instance ofTfrom aRowDatavalidator- Optional Bean Validation validator (nullable)- Returns:
- A new ExcelReader configured in mapping mode
- See Also:
-
forMap
Creates a reader that parses Excel files intoMap<String, String>rows by auto-discovering columns from the header row.The returned reader exposes the standard fluent API (
sheetIndex(int),headerRowIndex(int),onProgress(int, ProgressCallback)) but rejectscolumn(BiConsumer),column(String, BiConsumer),columnAt(int, BiConsumer),skipColumn(), andskipColumns(int)at runtime — map mode infers columns automatically from the header row and does not use the setter API.ExcelReader.forMap() .sheetIndex(0) .headerRowIndex(0) .build(inputStream) .read(result -> { Map<String, String> row = result.data(); String name = row.get("Name"); });- Returns:
- a new ExcelReader in map mode
- Since:
- 0.12.0
-
forMap
Creates a reader that parses Excel files intoMap<String, String>rows, including only the specified columns. Columns not listed are ignored.ExcelReader.forMap("Name", "Age") .build(inputStream) .read(result -> { // result.data() contains only "Name" and "Age" keys });- Parameters:
columnNames- the header names to include (others are filtered out)- Returns:
- a new ExcelReader in map mode with column filtering
- Since:
- 0.14.0
-
sheetIndex
Sets the zero-based sheet index to read from. Defaults to 0 (the first sheet).- Parameters:
sheetIndex- The zero-based index of the sheet to read- Returns:
- This ExcelReader instance for chaining
-
headerRowIndex
Sets the zero-based row index of the header row. Rows before this index will be skipped during reading. Defaults to 0 (the first row).- Parameters:
headerRowIndex- The zero-based index of the header row- Returns:
- This ExcelReader instance for chaining
-
password
Sets the password for reading encrypted Excel files.If the file is encrypted with the "agile" encryption mode (as produced by
ExcelWriter.password(String)), this password will be used to decrypt it before parsing.- Parameters:
password- the file password- Returns:
- this reader for chaining
- Since:
- 0.14.0
-
column
Registers a positional column mapping. Columns are matched to the spreadsheet in the order they are registered (afterheaderRowIndex(int)is accounted for).- Parameters:
setter- aBiConsumerthat writes a cell value into the row object- Returns:
- this reader for chaining
-
column
Registers a name-based column mapping. The column is matched to the spreadsheet column whose header cell equalsheaderNamein the header row.- Parameters:
headerName- the header name to matchsetter- aBiConsumerthat writes a cell value into the row object- Returns:
- this reader for chaining
-
columnAt
Registers an index-based column mapping. The column is matched to the spreadsheet column at the given 0-based index, regardless of header or registration order.- Parameters:
columnIndex- 0-based column index in the Excel filesetter- aBiConsumerthat writes a cell value into the row object- Returns:
- this reader for chaining
-
required
Marks the last registered column as required.A required column will produce a validation error if its cell value is blank or empty.
ExcelReader.setter(Person::new) .column("Name", (p, c) -> p.setName(c.asString())).required() .column("Age", (p, c) -> p.setAge(c.asInt())) .build(inputStream) .read(result -> { ... });- Returns:
- this reader for chaining
- Throws:
IllegalStateException- if no columns have been registered
-
skipColumn
Skips one column during reading by registering a no-op mapping at the next positional slot.- Returns:
- this reader for chaining
-
skipColumns
Skips the specified number of positional columns.- Parameters:
count- the number of columns to skip (must be non-negative)- Returns:
- this reader for chaining
- Throws:
IllegalArgumentException- ifcountis negative
-
onProgress
Registers a progress callback that fires everyintervalrows during reading.- Parameters:
interval- the number of rows between each callback invocation (must be positive)callback- the callback to invoke- Returns:
- This ExcelReader instance for chaining
-
build
Finalizes the configuration and builds anExcelReadHandlerfor parsing the given Excel stream.- Parameters:
inputStream- The input stream of the Excel file- Returns:
- A handler to execute Excel parsing
-
getSheetNames
Returns the list of sheet names and indices from an Excel file.- Parameters:
inputStream- The input stream of the Excel file (will be consumed)- Returns:
- A list of
ExcelSheetInforecords containing sheet names and indices
-
getSheetHeaders
public static List<String> getSheetHeaders(InputStream inputStream, int sheetIndex, int headerRowIndex) Returns the header names from a specific sheet.- Parameters:
inputStream- The input stream of the Excel file (will be consumed)sheetIndex- The 0-based sheet indexheaderRowIndex- The 0-based header row index- Returns:
- A list of header names
-