Package io.github.dornol.excelkit.excel
Class ExcelWorkbook
java.lang.Object
io.github.dornol.excelkit.excel.ExcelWorkbook
- All Implemented Interfaces:
AutoCloseable
Orchestrates multi-sheet Excel workbook creation where each sheet can have
a different data type.
Unlike ExcelWriter which handles automatic sheet rollover for a single data type,
ExcelWorkbook allows explicitly writing different data types to separate sheets.
try (ExcelWorkbook workbook = ExcelWorkbook.create().headerColor(ExcelColor.STEEL_BLUE)) {
workbook.<User>sheet("Users")
.column("Name", u -> u.getName())
.column("Status", u -> u.getStatus(), c -> c.dropdown("Active", "Inactive"))
.write(userStream);
workbook.<Order>sheet("Orders")
.column("ID", o -> o.getId())
.column("Amount", o -> o.getAmount(), c -> c.type(ExcelDataType.DOUBLE))
.write(orderStream);
ExcelHandler handler = workbook.finish();
handler.write(outputStream);
}
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the underlying workbook if it has not been finished.static ExcelWorkbookcreate()Creates a new ExcelWorkbook with default initialization (white header, 1000 row window).static ExcelWorkbookcreate(Consumer<ExcelWorkbook.InitOptions> configurer) Creates a new ExcelWorkbook with initialization options.finish()Finishes the workbook and returns anExcelHandlerfor output.headerColor(ExcelColor color) Sets the header background color for all sheets.headerFontName(String fontName) Sets the header font name for all sheets.headerFontSize(int fontSize) Sets the header font size for all sheets.Sets the file encryption password.protectWorkbook(String password) Protects the workbook structure with the given password.<T> ExcelSheetWriter<T> Creates a new sheet with the given name and returns a typed writer for it.
-
Method Details
-
create
Creates a new ExcelWorkbook with default initialization (white header, 1000 row window).- Returns:
- a new workbook instance (implements AutoCloseable)
- Since:
- 0.17.0
-
create
Creates a new ExcelWorkbook with initialization options.try (ExcelWorkbook wb = ExcelWorkbook.create(opts -> opts.rowAccessWindowSize(500)) .headerColor(ExcelColor.STEEL_BLUE)) { wb.<User>sheet("Users").column("Name", User::getName).write(stream); wb.finish().write(out); }- Parameters:
configurer- consumer that configuresExcelWorkbook.InitOptions- Returns:
- a new workbook instance (implements AutoCloseable)
- Since:
- 0.17.0
-
headerColor
Sets the header background color for all sheets. Must be called before anysheet(String)that relies on the header style.- Parameters:
color- header color (must not be null)- Returns:
- this workbook for chaining
- Since:
- 0.17.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:
- this workbook 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:
- this workbook for chaining
-
headerFontName
Sets the header font name for all sheets.- Parameters:
fontName- the font name (e.g., "Arial", "맑은 고딕")- Returns:
- this workbook for chaining
-
headerFontSize
Sets the header font size for all sheets.- Parameters:
fontSize- font size in points (must be positive)- Returns:
- this workbook for chaining
-
sheet
Creates a new sheet with the given name and returns a typed writer for it.- Type Parameters:
T- the data type for this sheet's rows- Parameters:
name- the sheet name (must be unique within this workbook)- Returns:
- an
ExcelSheetWriterfor configuring and writing the sheet - Throws:
ExcelWriteException- if the workbook is already finished or the sheet name is duplicate
-
finish
Finishes the workbook and returns anExcelHandlerfor output.After calling this method, no more sheets can be added.
- Returns:
- ExcelHandler wrapping the workbook
-
close
public void close()Closes the underlying workbook if it has not been finished. Iffinish()was called, the workbook lifecycle is managed byExcelHandler.- Specified by:
closein interfaceAutoCloseable
-