Class ExcelWorkbook

java.lang.Object
io.github.dornol.excelkit.excel.ExcelWorkbook
All Implemented Interfaces:
AutoCloseable

public class ExcelWorkbook extends Object implements 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);
 }
 
  • Method Details

    • create

      public static ExcelWorkbook 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

      public static ExcelWorkbook create(Consumer<ExcelWorkbook.InitOptions> configurer)
      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 configures ExcelWorkbook.InitOptions
      Returns:
      a new workbook instance (implements AutoCloseable)
      Since:
      0.17.0
    • headerColor

      public ExcelWorkbook headerColor(ExcelColor color)
      Sets the header background color for all sheets. Must be called before any sheet(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

      public ExcelWorkbook protectWorkbook(String password)
      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

      public ExcelWorkbook password(String 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 to ExcelHandler.writeTo(java.io.OutputStream, String).

      Parameters:
      password - the encryption password (must not be null or blank)
      Returns:
      this workbook for chaining
    • headerFontName

      public ExcelWorkbook headerFontName(String fontName)
      Sets the header font name for all sheets.
      Parameters:
      fontName - the font name (e.g., "Arial", "맑은 고딕")
      Returns:
      this workbook for chaining
    • headerFontSize

      public ExcelWorkbook headerFontSize(int fontSize)
      Sets the header font size for all sheets.
      Parameters:
      fontSize - font size in points (must be positive)
      Returns:
      this workbook for chaining
    • sheet

      public <T> ExcelSheetWriter<T> sheet(String name)
      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 ExcelSheetWriter for configuring and writing the sheet
      Throws:
      ExcelWriteException - if the workbook is already finished or the sheet name is duplicate
    • finish

      public ExcelHandler finish()
      Finishes the workbook and returns an ExcelHandler for 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. If finish() was called, the workbook lifecycle is managed by ExcelHandler.
      Specified by:
      close in interface AutoCloseable