Class SheetContext

java.lang.Object
io.github.dornol.excelkit.excel.SheetContext

public class SheetContext extends Object
Provides contextual information passed to BeforeHeaderWriter and AfterDataWriter callbacks, including the current sheet, workbook, row position, and column metadata.

A new instance is created for each callback invocation so that the sheet reference is always up-to-date (e.g. after sheet rollover).

Since:
0.3.0
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    columnLetter(int colIndex)
    Converts a zero-based column index to an Excel column letter.
    int
    Returns the number of columns configured for this sheet.
    Returns an unmodifiable list of column header names, in order.
    int
    Returns the current row index available for writing.
    int
    Returns the header row index (zero-based) for this sheet.
    org.apache.poi.xssf.streaming.SXSSFSheet
    Returns the current sheet being written to.
    org.apache.poi.xssf.streaming.SXSSFWorkbook
    Returns the workbook (useful for creating CellStyles, etc.).
    groupRows(int firstRow, int lastRow)
    Groups (outlines) a range of rows so they can be collapsed/expanded in Excel.
    groupRows(int firstRow, int lastRow, boolean collapsed)
    Groups (outlines) a range of rows, optionally collapsing them.
    mergeCells(int firstRow, int lastRow, int firstCol, int lastCol)
    Merges a rectangular region of cells identified by zero-based row and column indices.
    mergeCells(String cellRange)
    Merges a rectangular region of cells identified by an Excel-style range string such as "A1:C3".
    namedRange(String name, int col, int firstRow, int lastRow)
    Creates a workbook-scoped named range for a column range on the current sheet.
    namedRange(String name, String reference)
    Creates a workbook-scoped named range with the given reference formula.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getSheet

      public org.apache.poi.xssf.streaming.SXSSFSheet getSheet()
      Returns the current sheet being written to.
      Returns:
      the current sheet
    • getWorkbook

      public org.apache.poi.xssf.streaming.SXSSFWorkbook getWorkbook()
      Returns the workbook (useful for creating CellStyles, etc.).
      Returns:
      the workbook
    • getCurrentRow

      public int getCurrentRow()
      Returns the current row index available for writing.
      Returns:
      the current row index
    • getColumnCount

      public int getColumnCount()
      Returns the number of columns configured for this sheet.
      Returns:
      the column count
    • getHeaderRowIndex

      public int getHeaderRowIndex()
      Returns the header row index (zero-based) for this sheet.
      Returns:
      the header row index
    • getColumnNames

      public List<String> getColumnNames()
      Returns an unmodifiable list of column header names, in order.
      Returns:
      the column names
    • mergeCells

      public SheetContext mergeCells(int firstRow, int lastRow, int firstCol, int lastCol)
      Merges a rectangular region of cells identified by zero-based row and column indices.

      Example – merge the first three columns of row 0:

      ctx.mergeCells(0, 0, 0, 2);
      Parameters:
      firstRow - zero-based index of the first row in the region
      lastRow - zero-based index of the last row in the region
      firstCol - zero-based index of the first column in the region
      lastCol - zero-based index of the last column in the region
      Returns:
      this SheetContext for method chaining
    • mergeCells

      public SheetContext mergeCells(String cellRange)
      Merges a rectangular region of cells identified by an Excel-style range string such as "A1:C3".

      Example – merge cells A1 through C1:

      ctx.mergeCells("A1:C1");
      Parameters:
      cellRange - an Excel-notation range (e.g. "A1:C3")
      Returns:
      this SheetContext for method chaining
    • groupRows

      public SheetContext groupRows(int firstRow, int lastRow)
      Groups (outlines) a range of rows so they can be collapsed/expanded in Excel.

      Typically called from an AfterDataWriter callback.

      Parameters:
      firstRow - zero-based index of the first row to group
      lastRow - zero-based index of the last row to group
      Returns:
      this SheetContext for method chaining
      Since:
      0.7.0
    • groupRows

      public SheetContext groupRows(int firstRow, int lastRow, boolean collapsed)
      Groups (outlines) a range of rows, optionally collapsing them.
      Parameters:
      firstRow - zero-based index of the first row to group
      lastRow - zero-based index of the last row to group
      collapsed - whether the group should be initially collapsed
      Returns:
      this SheetContext for method chaining
      Since:
      0.7.0
    • namedRange

      public SheetContext namedRange(String name, String reference)
      Creates a workbook-scoped named range with the given reference formula.
      Parameters:
      name - the name for the range (e.g., "Categories")
      reference - the reference formula (e.g., "Sheet1!$A$1:$A$10")
      Returns:
      this SheetContext for method chaining
    • namedRange

      public SheetContext namedRange(String name, int col, int firstRow, int lastRow)
      Creates a workbook-scoped named range for a column range on the current sheet.
      Parameters:
      name - the name for the range
      col - zero-based column index
      firstRow - zero-based first row index
      lastRow - zero-based last row index
      Returns:
      this SheetContext for method chaining
    • columnLetter

      public static String columnLetter(int colIndex)
      Converts a zero-based column index to an Excel column letter.

      Examples:

      • 0 → "A"
      • 1 → "B"
      • 25 → "Z"
      • 26 → "AA"
      Parameters:
      colIndex - zero-based column index
      Returns:
      the Excel column letter(s)