Class ExcelWriter<T>
- Type Parameters:
T- The data type of each row to be written into the Excel file
- Since:
- 2025-07-19
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionafterAll(AfterDataWriter afterAllWriter) Registers a callback that writes custom content once on the last sheet after all data.afterData(AfterDataWriter afterDataWriter) Registers a callback that writes custom content after all data rows on each sheet.autoFilter(boolean autoFilter) Enables or disables auto-filter on the header row.autoWidthSampleRows(int rows) Sets the number of rows sampled for auto column width calculation.beforeHeader(BeforeHeaderWriter beforeHeaderWriter) Registers a callback that writes custom content before the column header row.chart(Consumer<ExcelChartConfig> configurer) Configures a chart to be added after all data is written.column(String name, RowFunction<T, @Nullable Object> function) Adds a column with cursor access using an RowFunction.column(String name, RowFunction<T, @Nullable Object> function, Consumer<ExcelColumn.ExcelColumnBuilder<T>> configurer) Adds a column with cursor access and additional configuration.Adds a column with default STRING type using a simple Function.column(String name, Function<T, @Nullable Object> function, Consumer<ExcelColumn.ExcelColumnBuilder<T>> configurer) Adds a column with additional configuration using a configurer consumer.columnIf(String name, boolean condition, RowFunction<T, @Nullable Object> function) Conditionally adds a column with cursor access using an RowFunction.columnIf(String name, boolean condition, RowFunction<T, @Nullable Object> function, Consumer<ExcelColumn.ExcelColumnBuilder<T>> configurer) Conditionally adds a column with cursor access and additional configuration.Conditionally adds a column with default STRING type using a simple Function.columnIf(String name, boolean condition, Function<T, @Nullable Object> function, Consumer<ExcelColumn.ExcelColumnBuilder<T>> configurer) Conditionally adds a column with additional configuration.conditionalFormatting(Consumer<ExcelConditionalRule> configurer) Adds a conditional formatting rule.constColumn(String name, @Nullable Object value) Adds a column with a constant value for all rows.constColumn(String name, @Nullable Object value, Consumer<ExcelColumn.ExcelColumnBuilder<T>> configurer) Adds a column with a constant value for all rows, with additional configuration.constColumnIf(String name, boolean condition, @Nullable Object value) Conditionally adds a column with a constant value for all rows.static <T> ExcelWriter<T> create()Creates a new ExcelWriter with default initialization (white header, 1,000,000 max rows, 1000 row window).static <T> ExcelWriter<T> create(Consumer<ExcelWriter.InitOptions> configurer) Creates a new ExcelWriter with initialization options.defaultStyle(Consumer<ColumnStyleConfig.DefaultStyleConfig<T>> configurer) Sets default column styles that apply to all columns unless overridden per-column.static ExcelWriter<Map<String, Object>> Creates an ExcelWriter pre-configured to write rows ofMap<String, Object>, with one column per given column name.static ExcelWriter<Map<String, Object>> forMap(String[] columnNames, Consumer<ExcelColumn.ExcelColumnBuilder<Map<String, Object>>>... configurers) Creates an ExcelWriter pre-configured forMap<String, Object>rows, with per-column configurers that can adjust type, format, styling, etc.static ExcelWriter<Map<String, Object>> forMap(Consumer<ExcelWriter.InitOptions> configurer, String... columnNames) Creates a map-valued ExcelWriter with initialization options (currently onlyrowAccessWindowSize).freezeCols(int cols) Freezes the given number of columns from the left edge.freezePane(int cols, int rows) Sets the number of columns and rows to freeze.freezeRows(int rows) Freezes the given number of rows below the header row.headerColor(ExcelColor color) Sets the header background color.headerFontName(String fontName) Sets the header font name.headerFontSize(int fontSize) Sets the header font size in points.maxRows(int maxRows) Sets the maximum number of rows per sheet before a new sheet is created.onProgress(int interval, ProgressCallback callback) Registers a progress callback that fires everyintervalrows.Sets the file encryption password.printSetup(Consumer<ExcelPrintSetup> configurer) Configures print setup (page layout) for all sheets.protectSheet(String password) Protects all sheets with the given password.protectWorkbook(String password) Protects the workbook structure with the given password.rowColor(Function<T, @Nullable ExcelColor> rowColorFunction) Sets a function that determines the background color for each row.rowHeight(float rowHeightInPoints) Sets the row height for data rows in points.Sets a fixed sheet name.Sets a function that generates sheet names based on the sheet index (0-based).summary(Consumer<ExcelSummary> configurer) Configures summary (footer) rows with formulas such as SUM, AVERAGE, COUNT, MIN, MAX.tabColor(int r, int g, int b) Sets the sheet tab color using RGB values.tabColor(ExcelColor color) Sets the sheet tab color using a preset color.Writes the stream of row data into Excel file without row-level callback.write(Stream<T> stream, WriteRowCallback<T> consumer) Writes the stream of row data into an Excel file using custom row-level callback.
-
Method Details
-
create
Creates a new ExcelWriter with default initialization (white header, 1,000,000 max rows, 1000 row window).- Type Parameters:
T- the row data type- Returns:
- a new ExcelWriter instance
-
create
Creates a new ExcelWriter with initialization options.The
ExcelWriter.InitOptionspassed to the configurer contains settings that must be fixed at workbook creation time — currently onlyrowAccessWindowSize(SXSSF's in-memory row window, which cannot be changed after the workbook exists). All other configuration (header color, max rows, columns, filters, callbacks, etc.) is set via fluent methods on the returned writer.ExcelWriter<User> writer = ExcelWriter.<User>create(opts -> opts .rowAccessWindowSize(500));- Type Parameters:
T- the row data type- Parameters:
configurer- consumer that configuresExcelWriter.InitOptions- Returns:
- a new ExcelWriter instance
-
forMap
Creates an ExcelWriter pre-configured to write rows ofMap<String, Object>, with one column per given column name. Each column reads its value from the map by using the column name as the key.Use this when your data is already in map form and you don't need per-column customization beyond the header labels. The returned writer is a regular
ExcelWriter, so all of its fluent configuration methods (row height, auto filter, freeze pane, sheet name, password, etc.) are available.ExcelWriter.forMap("Name", "Age", "Email") .rowHeight(22) .autoFilter(true) .write(stream) .write(out);- Parameters:
columnNames- the column names (used as both header labels and map keys)- Returns:
- a new ExcelWriter with the columns registered
- Since:
- 0.11.0
-
forMap
public static ExcelWriter<Map<String,Object>> forMap(Consumer<ExcelWriter.InitOptions> configurer, String... columnNames) Creates a map-valued ExcelWriter with initialization options (currently onlyrowAccessWindowSize). Header color and max rows are set via fluent methods on the returned writer.ExcelWriter.forMap( opts -> opts.rowAccessWindowSize(500), "Name", "Age", "City") .headerColor(ExcelColor.STEEL_BLUE) .maxRows(500_000) .autoFilter(true) .write(stream) .write(out);- Parameters:
configurer- consumer that configuresExcelWriter.InitOptionscolumnNames- the column names (used as both header labels and map keys)- Returns:
- a new ExcelWriter with the columns registered
- Since:
- 0.13.0
-
forMap
@SafeVarargs public static ExcelWriter<Map<String,Object>> forMap(String[] columnNames, Consumer<ExcelColumn.ExcelColumnBuilder<Map<String, Object>>>... configurers) Creates an ExcelWriter pre-configured forMap<String, Object>rows, with per-column configurers that can adjust type, format, styling, etc.Each configurer applies to the column at the matching index. Extra column names beyond the
configurersarray get no configurer (plain column).ExcelWriter.forMap( new String[]{"Name", "Price", "Date"}, cfg -> cfg.bold(true), cfg -> cfg.type(ExcelDataType.INTEGER), cfg -> cfg.type(ExcelDataType.DATE)) .write(stream) .write(out);- Parameters:
columnNames- the column namesconfigurers- per-column configurers (length must not exceedcolumnNames.length)- Returns:
- a new ExcelWriter with the columns registered
- Throws:
IllegalArgumentException- ifconfigurers.length > columnNames.length- Since:
- 0.11.0
-
headerColor
Sets the header background color. Must be called beforewrite(Stream).Use presets like
ExcelColor.STEEL_BLUEor custom viaExcelColor.of(int, int, int). Defaults toExcelColor.WHITE.- Parameters:
color- header color (must not be null)- Returns:
- Current ExcelWriter instance for chaining
- Since:
- 0.17.0
-
maxRows
Sets the maximum number of rows per sheet before a new sheet is created. Must be called beforewrite(Stream). Defaults to 1,000,000.- Parameters:
maxRows- maximum rows per sheet (must be positive)- Returns:
- Current ExcelWriter instance for chaining
- Since:
- 0.17.0
-
rowHeight
Sets the row height for data rows in points. Defaults to 20 points.- Parameters:
rowHeightInPoints- Row height in points (must be positive)- Returns:
- Current ExcelWriter instance for chaining
-
autoFilter
Enables or disables auto-filter on the header row.- Parameters:
autoFilter- Whether to apply auto-filter- Returns:
- Current ExcelWriter instance for chaining
-
freezeRows
Freezes the given number of rows below the header row.For both-axes freezing use
freezePane(int, int); for columns-only usefreezeCols(int).- Parameters:
rows- Number of rows to freeze (must be non-negative)- Returns:
- Current ExcelWriter instance for chaining
- Since:
- 0.16.6
-
freezeCols
Freezes the given number of columns from the left edge.For both-axes freezing use
freezePane(int, int); for rows-only usefreezeRows(int).- Parameters:
cols- Number of columns to freeze (must be non-negative)- Returns:
- Current ExcelWriter instance for chaining
- Since:
- 0.16.6
-
freezePane
Sets the number of columns and rows to freeze.Columns are frozen from the left edge, rows are frozen below the header row. This is useful for data entry forms and ledgers where both ID columns and header rows should remain visible while scrolling.
- Parameters:
cols- Number of columns to freeze from the left (must be non-negative)rows- Number of rows to freeze below the header (must be non-negative)- Returns:
- Current ExcelWriter instance for chaining
-
beforeHeader
Registers a callback that writes custom content before the column header row.The callback is invoked on every sheet, including rollover sheets, so it must always produce the same number of rows.
- Parameters:
beforeHeaderWriter- the callback to invoke before writing column headers- Returns:
- Current ExcelWriter instance for chaining
-
afterData
Registers a callback that writes custom content after all data rows on each sheet.Called on every sheet (including rollover sheets) after its data rows are written. On the last sheet, this is called before
afterAll.- Parameters:
afterDataWriter- the callback to invoke after data rows- Returns:
- Current ExcelWriter instance for chaining
-
afterAll
Registers a callback that writes custom content once on the last sheet after all data.Called only once, on the last sheet, after
afterData(if set). Useful for writing grand totals or summary rows.- Parameters:
afterAllWriter- the callback to invoke after all data is written- Returns:
- Current ExcelWriter instance for chaining
-
sheetName
Sets a function that generates sheet names based on the sheet index (0-based).- Parameters:
sheetNameFunction- a function that takes the sheet index and returns the sheet name- Returns:
- Current ExcelWriter instance for chaining
-
sheetName
Sets a fixed sheet name. When sheets roll over, subsequent sheets are named "{name} (2)", "{name} (3)", etc.- Parameters:
name- the base sheet name- Returns:
- Current ExcelWriter instance for chaining
-
rowColor
Sets a function that determines the background color for each row.When set, the function is called for each row of data. If it returns a non-null
ExcelColor, that color is applied as the background to all cells in the row, overriding any column-level background color.- Parameters:
rowColorFunction- function that takes row data and returns an ExcelColor (or null for no override)- Returns:
- Current ExcelWriter instance for chaining
-
onProgress
Registers a progress callback that fires everyintervalrows.- Parameters:
interval- the number of rows between each callback invocation (must be positive)callback- the callback to invoke- Returns:
- Current ExcelWriter instance for chaining
-
autoWidthSampleRows
Sets the number of rows sampled for auto column width calculation.Only the first N data rows are measured to determine column widths. Set to 0 to disable auto-width (all columns use minimum width). Defaults to 100.
- Parameters:
rows- number of rows to sample (0 to disable)- Returns:
- Current ExcelWriter instance for chaining
-
protectSheet
Protects all sheets with the given password.When sheet protection is enabled, cells are locked by default. Use
ColumnStyleConfig.locked(boolean)to allow editing specific columns.- Parameters:
password- the protection password- Returns:
- Current ExcelWriter instance for chaining
-
conditionalFormatting
Adds a conditional formatting rule.- Parameters:
configurer- consumer to configure the rule- Returns:
- Current ExcelWriter instance for chaining
-
chart
Configures a chart to be added after all data is written.- Parameters:
configurer- consumer to configure the chart- Returns:
- Current ExcelWriter instance for chaining
-
printSetup
Configures print setup (page layout) for all sheets.Controls orientation, paper size, margins, headers/footers, repeat rows, and fit-to-page.
- Parameters:
configurer- consumer to configure the print setup- Returns:
- Current ExcelWriter instance for chaining
-
tabColor
Sets the sheet tab color using RGB values.- Parameters:
r- Red component (0–255)g- Green component (0–255)b- Blue component (0–255)- Returns:
- Current ExcelWriter instance for chaining
- Since:
- 0.7.0
-
tabColor
Sets the sheet tab color using a preset color.- Parameters:
color- Preset color- Returns:
- Current ExcelWriter instance for chaining
- Since:
- 0.7.0
-
protectWorkbook
Protects the workbook structure with the given password.When enabled, users cannot add, delete, rename, or reorder sheets.
- Parameters:
password- the protection password- Returns:
- Current ExcelWriter instance for chaining
-
password
Sets the file encryption password.When set, the resulting Excel file will be encrypted using the "agile" encryption mode, and
ExcelHandler.writeTo(java.io.OutputStream)will automatically apply encryption — no need to pass the password toExcelHandler.writeTo(java.io.OutputStream, String).- Parameters:
password- the encryption password (must not be null or blank)- Returns:
- Current ExcelWriter instance for chaining
-
headerFontName
Sets the header font name.- Parameters:
fontName- the font name (e.g., "Arial", "맑은 고딕")- Returns:
- Current ExcelWriter instance for chaining
-
headerFontSize
Sets the header font size in points.- Parameters:
fontSize- font size in points (must be positive)- Returns:
- Current ExcelWriter instance for chaining
-
defaultStyle
Sets default column styles that apply to all columns unless overridden per-column.- Parameters:
configurer- consumer to configure default style properties- Returns:
- Current ExcelWriter instance for chaining
-
summary
Configures summary (footer) rows with formulas such as SUM, AVERAGE, COUNT, MIN, MAX.Summary rows are appended after data rows on each sheet.
- Parameters:
configurer- consumer to configure the summary- Returns:
- Current ExcelWriter instance for chaining
-
column
Adds a column with default STRING type using a simple Function. Useful for schema-based column registration.- Parameters:
name- Column header namefunction- Function to extract cell value from row- Returns:
- Current ExcelWriter instance for chaining
-
column
public ExcelWriter<T> column(String name, Function<T, @Nullable Object> function, Consumer<ExcelColumn.ExcelColumnBuilder<T>> configurer) Adds a column with additional configuration using a configurer consumer.The configurer receives an
ExcelColumn.ExcelColumnBuilderto set column properties such as type, format, alignment, width, etc.writer.column("Price", Book::getPrice, c -> c.type(ExcelDataType.INTEGER).format("#,##0"));- Parameters:
name- Column header namefunction- Function to extract cell value from rowconfigurer- Consumer to configure column properties- Returns:
- Current ExcelWriter instance for chaining
-
column
Adds a column with cursor access using an RowFunction. Useful when the column value depends on row position (e.g., row number).- Parameters:
name- Column header namefunction- Function to extract cell value from row with cursor access- Returns:
- Current ExcelWriter instance for chaining
-
column
public ExcelWriter<T> column(String name, RowFunction<T, @Nullable Object> function, Consumer<ExcelColumn.ExcelColumnBuilder<T>> configurer) Adds a column with cursor access and additional configuration.- Parameters:
name- Column header namefunction- Function to extract cell value from row with cursor accessconfigurer- Consumer to configure column properties- Returns:
- Current ExcelWriter instance for chaining
-
columnIf
public ExcelWriter<T> columnIf(String name, boolean condition, Function<T, @Nullable Object> function) Conditionally adds a column with default STRING type using a simple Function. If condition is false, the column is not added.- Parameters:
name- Column header namecondition- Whether to include this columnfunction- Function to extract cell value from row- Returns:
- Current ExcelWriter instance for chaining
-
columnIf
public ExcelWriter<T> columnIf(String name, boolean condition, Function<T, @Nullable Object> function, Consumer<ExcelColumn.ExcelColumnBuilder<T>> configurer) Conditionally adds a column with additional configuration. If condition is false, the column is not added.- Parameters:
name- Column header namecondition- Whether to include this columnfunction- Function to extract cell value from rowconfigurer- Consumer to configure column properties- Returns:
- Current ExcelWriter instance for chaining
-
columnIf
public ExcelWriter<T> columnIf(String name, boolean condition, RowFunction<T, @Nullable Object> function) Conditionally adds a column with cursor access using an RowFunction. If condition is false, the column is not added.- Parameters:
name- Column header namecondition- Whether to include this columnfunction- Function to extract cell value from row with cursor access- Returns:
- Current ExcelWriter instance for chaining
-
columnIf
public ExcelWriter<T> columnIf(String name, boolean condition, RowFunction<T, @Nullable Object> function, Consumer<ExcelColumn.ExcelColumnBuilder<T>> configurer) Conditionally adds a column with cursor access and additional configuration. If condition is false, the column is not added.- Parameters:
name- Column header namecondition- Whether to include this columnfunction- Function to extract cell value from row with cursor accessconfigurer- Consumer to configure column properties- Returns:
- Current ExcelWriter instance for chaining
-
constColumn
Adds a column with a constant value for all rows.- Parameters:
name- Column header namevalue- Constant value to be used in all rows- Returns:
- Current ExcelWriter instance for chaining
-
constColumn
public ExcelWriter<T> constColumn(String name, @Nullable Object value, Consumer<ExcelColumn.ExcelColumnBuilder<T>> configurer) Adds a column with a constant value for all rows, with additional configuration.- Parameters:
name- Column header namevalue- Constant value to be used in all rowsconfigurer- Consumer to configure column properties- Returns:
- Current ExcelWriter instance for chaining
-
constColumnIf
Conditionally adds a column with a constant value for all rows. If condition is false, the column is not added.- Parameters:
name- Column header namecondition- Whether to include this columnvalue- Constant value to be used in all rows- Returns:
- Current ExcelWriter instance for chaining
-
write
Writes the stream of row data into an Excel file using custom row-level callback.- Parameters:
stream- The data streamconsumer- Custom consumer for post-processing row with cursor- Returns:
- ExcelHandler wrapping the workbook
-
write
Writes the stream of row data into Excel file without row-level callback.- Parameters:
stream- The data stream- Returns:
- ExcelHandler wrapping the workbook
-