Package io.github.dornol.excelkit.excel
Class ExcelTemplateWriter
java.lang.Object
io.github.dornol.excelkit.excel.ExcelTemplateWriter
- All Implemented Interfaces:
AutoCloseable
Fills data into an existing Excel template (.xlsx) while preserving formatting,
images, charts, and merged regions.
Supports two write modes that can be combined:
- Cell-level writes — fill individual cells by reference (e.g., "B3")
- List streaming — write tabular data starting from a given row
Important: All writes must proceed in top-to-bottom row order. SXSSFWorkbook flushes rows from memory, so previously written rows cannot be revisited.
try (ExcelTemplateWriter writer = new ExcelTemplateWriter(templateStream)) {
writer.cell("B3", clientName)
.cell("B4", LocalDate.now());
writer.<Item>list(5)
.column("A", Item::getName)
.column("B", Item::getQty)
.column("C", Item::getAmount)
.afterData(ctx -> {
ctx.getSheet().createRow(ctx.getCurrentRow())
.createCell(0).setCellValue("Total: " + total);
return ctx.getCurrentRow() + 1;
})
.write(itemStream);
writer.finish().write(outputStream);
}
- Since:
- 0.8.2
-
Constructor Summary
ConstructorsConstructorDescriptionExcelTemplateWriter(InputStream templateStream) Opens a template from the given input stream.ExcelTemplateWriter(InputStream templateStream, int rowAccessWindowSize) Opens a template from the given input stream with a custom row access window size. -
Method Summary
Modifier and TypeMethodDescriptionWrites a value to the specified cell by row and column index.cell(int row, int col, @Nullable Object value, @Nullable org.apache.poi.ss.usermodel.CellStyle style) Writes a value with a custom style to the specified cell by row and column index.Writes a value to the specified cell on the active sheet.Writes a value with a custom style to the specified cell.voidclose()Closes the underlying workbook if it has not been finished.finish()Finishes the template and returns anExcelHandlerfor output.<T> TemplateListWriter<T> list(int startRow) Creates a list writer for streaming tabular data starting at the given row on the active sheet.<T> TemplateListWriter<T> list(int sheetIndex, int startRow) Creates a list writer for streaming tabular data into a specific sheet.sheet(int index) Selects the active sheet by index for subsequentcell(java.lang.String, java.lang.Object)calls.Selects the active sheet by name for subsequentcell(java.lang.String, java.lang.Object)calls.
-
Constructor Details
-
ExcelTemplateWriter
Opens a template from the given input stream.- Parameters:
templateStream- the .xlsx template file stream- Throws:
IOException- if the template cannot be read
-
ExcelTemplateWriter
Opens a template from the given input stream with a custom row access window size.- Parameters:
templateStream- the .xlsx template file streamrowAccessWindowSize- number of rows kept in memory by SXSSFWorkbook- Throws:
IOException- if the template cannot be read
-
-
Method Details
-
sheet
Selects the active sheet by index for subsequentcell(java.lang.String, java.lang.Object)calls.- Parameters:
index- 0-based sheet index- Returns:
- this writer for chaining
-
sheet
Selects the active sheet by name for subsequentcell(java.lang.String, java.lang.Object)calls.- Parameters:
name- the sheet name- Returns:
- this writer for chaining
-
cell
Writes a value to the specified cell on the active sheet.The value type is auto-detected: String, Number, Boolean, LocalDate, LocalDateTime, LocalTime, or null (blank cell).
- Parameters:
cellRef- Excel-notation cell reference (e.g., "B3", "AA10")value- the value to write- Returns:
- this writer for chaining
-
cell
public ExcelTemplateWriter cell(String cellRef, @Nullable Object value, org.apache.poi.ss.usermodel.CellStyle style) Writes a value with a custom style to the specified cell.- Parameters:
cellRef- Excel-notation cell referencevalue- the value to writestyle- the cell style to apply- Returns:
- this writer for chaining
-
cell
Writes a value to the specified cell by row and column index.- Parameters:
row- 0-based row indexcol- 0-based column indexvalue- the value to write- Returns:
- this writer for chaining
-
cell
public ExcelTemplateWriter cell(int row, int col, @Nullable Object value, @Nullable org.apache.poi.ss.usermodel.CellStyle style) Writes a value with a custom style to the specified cell by row and column index.- Parameters:
row- 0-based row indexcol- 0-based column indexvalue- the value to writestyle- the cell style to apply (null to keep existing)- Returns:
- this writer for chaining
-
list
Creates a list writer for streaming tabular data starting at the given row on the active sheet.The template's existing column headers (if any) are preserved. Data rows are written starting from
startRow.- Type Parameters:
T- the row data type- Parameters:
startRow- 0-based row index where data writing begins- Returns:
- a
TemplateListWriterfor configuring columns and writing data
-
list
Creates a list writer for streaming tabular data into a specific sheet.- Type Parameters:
T- the row data type- Parameters:
sheetIndex- 0-based sheet indexstartRow- 0-based row index where data writing begins- Returns:
- a
TemplateListWriterfor configuring columns and writing data
-
finish
Finishes the template and returns anExcelHandlerfor output.After calling this method, no more writes are allowed.
- Returns:
- ExcelHandler wrapping the workbook
-
close
public void close()Closes the underlying workbook if it has not been finished.- Specified by:
closein interfaceAutoCloseable
-