Package ortus.boxlang.spreadsheet
Class SpreadsheetUtil
java.lang.Object
ortus.boxlang.spreadsheet.SpreadsheetUtil
This class provides spreadsheet utilities for the BoxLang runtime using Apache POI
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddImage(org.apache.poi.ss.usermodel.Sheet sheet, String filepath, int startRow, int startCol, int endRow, int endCol, org.apache.poi.ss.usermodel.Workbook workbook) Adds an image with custom anchor coordinatesstatic voidaddImage(org.apache.poi.ss.usermodel.Sheet sheet, String filepath, int row, int col, org.apache.poi.ss.usermodel.Workbook workbook) Adds an image to a sheet at a specified cell locationstatic voidaddPrintGridlines(org.apache.poi.ss.usermodel.Sheet sheet) Adds print gridlines to the sheet (gridlines will print when document is printed).static voidautoSizeColumn(org.apache.poi.ss.usermodel.Sheet sheet, int column) Auto-sizes a specific column in the specified sheetstatic voidautoSizeColumn(org.apache.poi.ss.usermodel.Workbook workbook, int column) Auto-sizes a specific column in the active sheetstatic voidautoSizeColumns(org.apache.poi.ss.usermodel.Sheet sheet) Auto-sizes all columns in the specified sheetstatic voidautoSizeColumns(org.apache.poi.ss.usermodel.Workbook workbook) Auto-sizes all columns in the active sheetstatic voidclearCell(org.apache.poi.ss.usermodel.Sheet sheet, int row, int col) Clears the specified cell of all styles and valuesstatic voidclearCellRange(org.apache.poi.ss.usermodel.Sheet sheet, int startRow, int startCol, int endRow, int endCol) Clears a range of cells of all styles and valuesstatic voidcreateSplitPane(org.apache.poi.ss.usermodel.Sheet sheet, int xSplitPos, int ySplitPos, int leftmostColumn, int topRow) Creates a split pane in a sheet with default active panestatic voidcreateSplitPane(org.apache.poi.ss.usermodel.Sheet sheet, int xSplitPos, int ySplitPos, int leftmostColumn, int topRow, int activePane) Creates a split pane in a sheetstatic org.apache.poi.ss.usermodel.WorkbookcreateWorkbook(boolean xmlFormat, String sheetName) Creates a new workbook based on the specified formatstatic org.apache.poi.ss.usermodel.WorkbookcreateXlsWorkbook(String sheetName) Creates a new .xls workbookstatic org.apache.poi.ss.usermodel.WorkbookcreateXlsxWorkbook(String sheetName) Creates a new .xlsx workbookstatic voiddeleteColumn(org.apache.poi.ss.usermodel.Sheet sheet, int column) Deletes a column by clearing all cells in that column Note: Apache POI doesn't support true column deletion, so we clear the column contentstatic voiddeleteColumns(org.apache.poi.ss.usermodel.Sheet sheet, int startColumn, int endColumn) Deletes multiple columns by clearing all cells in those columnsstatic voidformatCell(org.apache.poi.ss.usermodel.Cell cell, ortus.boxlang.runtime.types.IStruct format) Applies formatting to a specific cell based on a format structurestatic voidformatCellRange(org.apache.poi.ss.usermodel.Sheet sheet, ortus.boxlang.runtime.types.IStruct format, int startRow, int startCol, int endRow, int endCol, org.apache.poi.ss.usermodel.Workbook workbook) Formats a range of cells with a single format structstatic voidformatColumn(org.apache.poi.ss.usermodel.Sheet sheet, int column, ortus.boxlang.runtime.types.IStruct format) Formats all cells in a single columnstatic voidformatColumns(org.apache.poi.ss.usermodel.Sheet sheet, String columns, ortus.boxlang.runtime.types.IStruct format) Formats all cells in multiple columns specified by a comma-separated string or range Format: "1,3,5" or "1-5,7-10" or combinations like "1-3,5,7-10"static voidformatRow(org.apache.poi.ss.usermodel.Sheet sheet, int row, ortus.boxlang.runtime.types.IStruct format) Formats all cells in a single rowstatic voidformatRows(org.apache.poi.ss.usermodel.Sheet sheet, String rows, ortus.boxlang.runtime.types.IStruct format) Formats all cells in multiple rows specified by a comma-separated string or range Format: "1,3,5" or "1-5,7-10" or combinations like "1-3,5,7-10"static ortus.boxlang.runtime.types.IStructgetActiveCell(org.apache.poi.ss.usermodel.Sheet sheet) Gets the currently active/selected cell in a sheetstatic org.apache.poi.ss.usermodel.SheetgetActiveSheet(org.apache.poi.ss.usermodel.Workbook workbook) Gets the active sheet from the workbookstatic ortus.boxlang.runtime.types.ArraygetAllCellComments(org.apache.poi.ss.usermodel.Sheet sheet) Get all cell comments from a sheet as an array of structsstatic booleangetAutoCalculate(org.apache.poi.ss.usermodel.Workbook workbook) Gets whether formulas automatically calculatestatic org.apache.poi.ss.usermodel.CellgetCell(org.apache.poi.ss.usermodel.Sheet sheet, int row, int col) Gets a cell at the specified row and column, returning null if it doesn't existstatic StringgetCellAddress(org.apache.poi.ss.usermodel.Workbook workbook, int row, int column) Gets the cell address (e.g., "A1", "B5") for a given row and columnstatic StringgetCellComment(org.apache.poi.ss.usermodel.Cell cell) Gets a comment from a cellstatic ortus.boxlang.runtime.types.IStructgetCellCommentAsStruct(org.apache.poi.ss.usermodel.Cell cell) Get cell comment as a struct with Author, Column, Comment, Row keysstatic ortus.boxlang.runtime.types.IStructgetCellFormat(org.apache.poi.ss.usermodel.Cell cell) Gets the formatting information of a cellstatic ortus.boxlang.runtime.types.IStructgetCellHyperlink(org.apache.poi.ss.usermodel.Sheet sheet, int row, int column) Gets the hyperlink from a cellstatic StringgetCellType(org.apache.poi.ss.usermodel.Cell cell) Gets the Excel cell type of a cellstatic ObjectgetCellValue(org.apache.poi.ss.usermodel.Cell cell) Gets the cell value as an Object, handling different cell typesstatic intgetColumnCount(org.apache.poi.ss.usermodel.Sheet sheet) Gets the number of columns that contain data in the specified sheetstatic intgetColumnCount(org.apache.poi.ss.usermodel.Workbook workbook) Gets the number of columns that contain data in the active sheetstatic doublegetColumnWidth(org.apache.poi.ss.usermodel.Sheet sheet, int column, boolean returnWidthInPixels) Gets the width of a column.static ortus.boxlang.runtime.types.IStructgetInfo(org.apache.poi.ss.usermodel.Workbook workbook) Gets information about the spreadsheet as a structstatic intgetLastRowNumber(org.apache.poi.ss.usermodel.Sheet sheet) Gets the last row number with data in the sheet.static org.apache.poi.ss.usermodel.CellgetOrCreateCell(org.apache.poi.ss.usermodel.Sheet sheet, int row, int col) Gets a cell at the specified row and column, creating it if it doesn't existstatic StringgetPrintOrientation(org.apache.poi.ss.usermodel.Sheet sheet) Gets the print orientation of the sheet.static booleangetRecalculateFormulasOnNextOpen(org.apache.poi.ss.usermodel.Workbook workbook) Gets whether formulas will be recalculated when the file is openedstatic intgetRowCount(org.apache.poi.ss.usermodel.Sheet sheet) Gets the number of rows that contain data in the specified sheetstatic intgetRowCount(org.apache.poi.ss.usermodel.Workbook workbook) Gets the number of rows that contain data in the active sheetstatic ortus.boxlang.runtime.types.ArraygetSheetNames(org.apache.poi.ss.usermodel.Workbook workbook) Gets the names of all sheets in the workbookstatic ortus.boxlang.runtime.types.IStructgetSheetsVisibility(org.apache.poi.ss.usermodel.Workbook workbook) Gets the visibility status of all sheets in the workbookstatic voidgroupColumns(org.apache.poi.ss.usermodel.Sheet sheet, int startColumn, int endColumn, Boolean isCollapsed) Groups columns together in the active sheetstatic voidGroups rows together in the active sheetstatic booleanChecks if a sheet with the given name exists in the workbookstatic voidHides a sheet from viewstatic booleanisBinaryFormat(org.apache.poi.ss.usermodel.Workbook workbook) Determines if a workbook is in binary format (.xls)static booleanisColumnHidden(org.apache.poi.ss.usermodel.Sheet sheet, int column) Gets the hidden state of a columnstatic booleanisRowHidden(org.apache.poi.ss.usermodel.Sheet sheet, int row) Gets the hidden state of a rowstatic booleanisSpreadsheetFile(String filePath) Determines if a file is a valid spreadsheet filestatic booleanisXmlFormat(org.apache.poi.ss.usermodel.Workbook workbook) Determines if a workbook is in XML format (.xlsx)static voidmoveSheet(org.apache.poi.ss.usermodel.Workbook workbook, int fromIndex, int toIndex) Moves a sheet to a new position by sheet indexstatic voidMoves a sheet to a new position by sheet namestatic org.apache.poi.ss.usermodel.WorkbookreadWorkbook(String filePath) Reads a spreadsheet file from the given pathstatic org.apache.poi.ss.usermodel.WorkbookreadWorkbook(String filePath, String password) Reads a spreadsheet file from the given path with optional passwordstatic voidrecalculateAllFormulas(org.apache.poi.ss.usermodel.Workbook workbook) Recalculates all formulas in the workbookstatic voidremoveColumnBreak(org.apache.poi.ss.usermodel.Sheet sheet, int zeroBasedCol) Removes a page (column) break on the specified 0-based column index on the sheet.static voidremovePrintGridlines(org.apache.poi.ss.usermodel.Sheet sheet) Removes print gridlines from the sheet (gridlines will not print when document is printed).static voidremoveRowBreak(org.apache.poi.ss.usermodel.Sheet sheet, int zeroBasedRow) Removes a page (row) break on the specified 0-based row index on the sheet.static voidremoveSheetByNumber(org.apache.poi.ss.usermodel.Workbook workbook, int sheetNumber) Removes a sheet by index (1-based)static voidrenameSheet(org.apache.poi.ss.usermodel.Workbook workbook, String oldName, String newName) Renames an existing sheetstatic voidsetActiveCell(org.apache.poi.ss.usermodel.Sheet sheet, int row, int col) Sets the active/selected cell in a sheetstatic voidsetActiveSheet(org.apache.poi.ss.usermodel.Workbook workbook, int sheetIndex) Sets the active sheet by index (0-based)static voidsetActiveSheet(org.apache.poi.ss.usermodel.Workbook workbook, String sheetName) Sets the active sheet by namestatic voidsetAutoCalculate(org.apache.poi.ss.usermodel.Workbook workbook, boolean autoCalculate) Sets whether formulas automatically calculatestatic voidsetCellComment(org.apache.poi.ss.usermodel.Cell cell, Object commentObj, String author) Sets a comment on a cell with optional formattingstatic voidsetCellHyperlink(org.apache.poi.ss.usermodel.Sheet sheet, int row, int column, String hyperlink, String label, org.apache.poi.ss.usermodel.Workbook workbook) Sets a hyperlink on a cellstatic voidsetCellRangeValue(org.apache.poi.ss.usermodel.Sheet sheet, Object value, int startRow, int startCol, int endRow, int endCol) Sets the same value to a range of cellsstatic voidsetCellValue(org.apache.poi.ss.usermodel.Cell cell, Object value) Sets a cell value, automatically determining the appropriate cell typestatic voidsetColumnBreak(org.apache.poi.ss.usermodel.Sheet sheet, int zeroBasedCol) Sets a page (column) break on the specified 0-based column index on the sheet.static voidsetColumnHidden(org.apache.poi.ss.usermodel.Sheet sheet, int column, boolean hidden) Sets the hidden state of a columnstatic voidsetDocumentProperties(org.apache.poi.ss.usermodel.Workbook workbook, ortus.boxlang.runtime.types.IStruct properties) Sets document properties/metadata for a workbookstatic voidsetFitToPage(org.apache.poi.ss.usermodel.Sheet sheet, boolean fitToPage, int pagesWide, int pagesHigh) Sets the fit-to-page print option for a sheetstatic voidsetFooter(org.apache.poi.ss.usermodel.Sheet sheet, ortus.boxlang.runtime.types.IStruct footer) Sets the footer text for the specified sheet.static voidsetFooterImage(org.apache.poi.ss.usermodel.Sheet sheet, String alignment, String imagePath, org.apache.poi.ss.usermodel.Workbook workbook) Sets an image in the footer of the specified sheetstatic voidsetHeader(org.apache.poi.ss.usermodel.Sheet sheet, ortus.boxlang.runtime.types.IStruct header) Sets the header text for the specified sheet.static voidsetHeaderImage(org.apache.poi.ss.usermodel.Sheet sheet, String alignment, String imagePath, org.apache.poi.ss.usermodel.Workbook workbook) Sets an image in the header of the specified sheetstatic voidsetRecalculateFormulasOnNextOpen(org.apache.poi.ss.usermodel.Workbook workbook, boolean recalculate) Sets whether formulas should be recalculated when the file is openedstatic voidsetRepeatingColumns(org.apache.poi.ss.usermodel.Workbook workbook, org.apache.poi.ss.usermodel.Sheet sheet, int startCol, int endCol) Sets columns to repeat on every printed pagestatic voidsetRepeatingRows(org.apache.poi.ss.usermodel.Workbook workbook, org.apache.poi.ss.usermodel.Sheet sheet, int startRow, int endRow) Sets rows to repeat on every printed pagestatic voidsetRowBreak(org.apache.poi.ss.usermodel.Sheet sheet, int zeroBasedRow) Sets a page (row) break on the specified 0-based row index on the sheet.static voidsetRowHidden(org.apache.poi.ss.usermodel.Sheet sheet, int row, boolean hidden) Sets the hidden state of a rowstatic voidsetSheetPrintOrientation(org.apache.poi.ss.usermodel.Sheet sheet, String orientation) Sets the sheet print orientationstatic voidshiftColumns(org.apache.poi.ss.usermodel.Sheet sheet, int start, int end, int n) Shifts columns left or right in a sheet Note: Apache POI doesn't provide a native column shift operation, so we implement it manually by copying cell data from the source columns to the destination columns.static voidungroupColumns(org.apache.poi.ss.usermodel.Sheet sheet, int startColumn, int endColumn) Ungroups columns in the active sheetstatic voidungroupRows(org.apache.poi.ss.usermodel.Sheet sheet, int startRow, int endRow) Ungroups rows in the active sheetstatic voidunhideSheet(org.apache.poi.ss.usermodel.Workbook workbook, String sheetName) Unhides a sheet to make it visiblestatic voidwriteWorkbook(org.apache.poi.ss.usermodel.Workbook workbook, String filePath, boolean overwrite) Writes a workbook to the specified file pathstatic voidwriteWorkbook(org.apache.poi.ss.usermodel.Workbook workbook, String filePath, boolean overwrite, String password) Writes a workbook to the specified file path with optional password protection
-
Constructor Details
-
SpreadsheetUtil
public SpreadsheetUtil()
-
-
Method Details
-
createWorkbook
public static org.apache.poi.ss.usermodel.Workbook createWorkbook(boolean xmlFormat, String sheetName) Creates a new workbook based on the specified format- Parameters:
xmlFormat- True for .xlsx format, false for .xls formatsheetName- The name of the initial sheet (defaults to "Sheet1")- Returns:
- A new workbook instance
-
createXlsxWorkbook
Creates a new .xlsx workbook- Parameters:
sheetName- The name of the initial sheet- Returns:
- A new XLSX workbook
-
createXlsWorkbook
Creates a new .xls workbook- Parameters:
sheetName- The name of the initial sheet- Returns:
- A new XLS workbook
-
readWorkbook
Reads a spreadsheet file from the given path- Parameters:
filePath- The absolute path to the spreadsheet file- Returns:
- A workbook object
- Throws:
ortus.boxlang.runtime.types.exceptions.BoxIOException- If the file cannot be read or doesn't exist
-
readWorkbook
Reads a spreadsheet file from the given path with optional password- Parameters:
filePath- The absolute path to the spreadsheet filepassword- Optional password for encrypted files- Returns:
- A workbook object
- Throws:
ortus.boxlang.runtime.types.exceptions.BoxIOException- If the file cannot be read or doesn't exist
-
writeWorkbook
public static void writeWorkbook(org.apache.poi.ss.usermodel.Workbook workbook, String filePath, boolean overwrite) Writes a workbook to the specified file path- Parameters:
workbook- The workbook to writefilePath- The absolute path where to save the fileoverwrite- Whether to overwrite existing files- Throws:
ortus.boxlang.runtime.types.exceptions.BoxIOException- If the file cannot be written
-
writeWorkbook
public static void writeWorkbook(org.apache.poi.ss.usermodel.Workbook workbook, String filePath, boolean overwrite, String password) Writes a workbook to the specified file path with optional password protection- Parameters:
workbook- The workbook to writefilePath- The absolute path where to save the fileoverwrite- Whether to overwrite existing filespassword- Optional password to protect the file (only works with .xlsx format)- Throws:
ortus.boxlang.runtime.types.exceptions.BoxIOException- If the file cannot be written
-
getCellValue
Gets the cell value as an Object, handling different cell types- Parameters:
cell- The cell to get the value from- Returns:
- The cell value as an appropriate Java object
-
setCellValue
Sets a cell value, automatically determining the appropriate cell type- Parameters:
cell- The cell to set the value forvalue- The value to set
-
clearCell
public static void clearCell(org.apache.poi.ss.usermodel.Sheet sheet, int row, int col) Clears the specified cell of all styles and values- Parameters:
sheet- The sheet containing the cellrow- The 1-based row numbercol- The 1-based column number
-
clearCellRange
public static void clearCellRange(org.apache.poi.ss.usermodel.Sheet sheet, int startRow, int startCol, int endRow, int endCol) Clears a range of cells of all styles and values- Parameters:
sheet- The sheet containing the cellsstartRow- The 1-based starting row (inclusive)startCol- The 1-based starting column (inclusive)endRow- The 1-based ending row (inclusive)endCol- The 1-based ending column (inclusive)
-
getCellType
Gets the Excel cell type of a cell- Parameters:
cell- The cell to check- Returns:
- Cell type as a string: "BLANK", "BOOLEAN", "ERROR", "FORMULA", "NUMERIC", or "STRING"
-
getCellFormat
public static ortus.boxlang.runtime.types.IStruct getCellFormat(org.apache.poi.ss.usermodel.Cell cell) Gets the formatting information of a cell- Parameters:
cell- The cell to get formatting from- Returns:
- A struct containing all formatting attributes
-
setCellRangeValue
public static void setCellRangeValue(org.apache.poi.ss.usermodel.Sheet sheet, Object value, int startRow, int startCol, int endRow, int endCol) Sets the same value to a range of cells- Parameters:
sheet- The sheet containing the cellsvalue- The value to set for all cells in the rangestartRow- The 1-based starting row (inclusive)startCol- The 1-based starting column (inclusive)endRow- The 1-based ending row (inclusive)endCol- The 1-based ending column (inclusive)
-
getActiveCell
public static ortus.boxlang.runtime.types.IStruct getActiveCell(org.apache.poi.ss.usermodel.Sheet sheet) Gets the currently active/selected cell in a sheet- Parameters:
sheet- The sheet- Returns:
- A struct with keys "row" and "column" (both 1-based), or with 1,1 as default
-
setActiveCell
public static void setActiveCell(org.apache.poi.ss.usermodel.Sheet sheet, int row, int col) Sets the active/selected cell in a sheet- Parameters:
sheet- The sheetrow- The 1-based row numbercol- The 1-based column number
-
setRowBreak
public static void setRowBreak(org.apache.poi.ss.usermodel.Sheet sheet, int zeroBasedRow) Sets a page (row) break on the specified 0-based row index on the sheet.- Parameters:
sheet- the sheetzeroBasedRow- the 0-based row index
-
removeRowBreak
public static void removeRowBreak(org.apache.poi.ss.usermodel.Sheet sheet, int zeroBasedRow) Removes a page (row) break on the specified 0-based row index on the sheet.- Parameters:
sheet- the sheetzeroBasedRow- the 0-based row index
-
setColumnBreak
public static void setColumnBreak(org.apache.poi.ss.usermodel.Sheet sheet, int zeroBasedCol) Sets a page (column) break on the specified 0-based column index on the sheet.- Parameters:
sheet- the sheetzeroBasedCol- the 0-based column index
-
removeColumnBreak
public static void removeColumnBreak(org.apache.poi.ss.usermodel.Sheet sheet, int zeroBasedCol) Removes a page (column) break on the specified 0-based column index on the sheet.- Parameters:
sheet- the sheetzeroBasedCol- the 0-based column index
-
setFitToPage
public static void setFitToPage(org.apache.poi.ss.usermodel.Sheet sheet, boolean fitToPage, int pagesWide, int pagesHigh) Sets the fit-to-page print option for a sheet- Parameters:
sheet- The sheet to configurefitToPage- Whether to enable fit-to-pagepagesWide- Number of pages to fit columns into (0 = no horizontal fit)pagesHigh- Number of pages to fit rows into (0 = no vertical fit)- Throws:
ortus.boxlang.runtime.types.exceptions.BoxRuntimeException- if sheet is null
-
addPrintGridlines
public static void addPrintGridlines(org.apache.poi.ss.usermodel.Sheet sheet) Adds print gridlines to the sheet (gridlines will print when document is printed).- Parameters:
sheet- The sheet to modify- Throws:
ortus.boxlang.runtime.types.exceptions.BoxRuntimeException- if sheet is null
-
removePrintGridlines
public static void removePrintGridlines(org.apache.poi.ss.usermodel.Sheet sheet) Removes print gridlines from the sheet (gridlines will not print when document is printed).- Parameters:
sheet- The sheet to modify- Throws:
ortus.boxlang.runtime.types.exceptions.BoxRuntimeException- if sheet is null
-
getColumnWidth
public static double getColumnWidth(org.apache.poi.ss.usermodel.Sheet sheet, int column, boolean returnWidthInPixels) Gets the width of a column.- Parameters:
sheet- The sheet containing the columncolumn- The column number (0-based)returnWidthInPixels- If true, returns width in pixels; if false, returns width in points- Returns:
- Column width as a double value
- Throws:
ortus.boxlang.runtime.types.exceptions.BoxRuntimeException- if sheet is null or column is invalid
-
getLastRowNumber
public static int getLastRowNumber(org.apache.poi.ss.usermodel.Sheet sheet) Gets the last row number with data in the sheet.- Parameters:
sheet- The sheet to query- Returns:
- The last row number (0-based), or -1 if sheet is empty
- Throws:
ortus.boxlang.runtime.types.exceptions.BoxRuntimeException- if sheet is null
-
getPrintOrientation
Gets the print orientation of the sheet.- Parameters:
sheet- The sheet to query- Returns:
- "portrait" or "landscape"
- Throws:
ortus.boxlang.runtime.types.exceptions.BoxRuntimeException- if sheet is null
-
setColumnHidden
public static void setColumnHidden(org.apache.poi.ss.usermodel.Sheet sheet, int column, boolean hidden) Sets the hidden state of a column- Parameters:
sheet- The sheet containing the columncolumn- The column number (1-based)hidden- True to hide, false to show- Throws:
ortus.boxlang.runtime.types.exceptions.BoxRuntimeException- if sheet is null or column is invalid
-
isColumnHidden
public static boolean isColumnHidden(org.apache.poi.ss.usermodel.Sheet sheet, int column) Gets the hidden state of a column- Parameters:
sheet- The sheet containing the columncolumn- The column number (1-based)- Returns:
- true if column is hidden, false otherwise
- Throws:
ortus.boxlang.runtime.types.exceptions.BoxRuntimeException- if sheet is null or column is invalid
-
setRowHidden
public static void setRowHidden(org.apache.poi.ss.usermodel.Sheet sheet, int row, boolean hidden) Sets the hidden state of a row- Parameters:
sheet- The sheet containing the rowrow- The row number (1-based)hidden- True to hide, false to show- Throws:
ortus.boxlang.runtime.types.exceptions.BoxRuntimeException- if sheet is null or row is invalid
-
isRowHidden
public static boolean isRowHidden(org.apache.poi.ss.usermodel.Sheet sheet, int row) Gets the hidden state of a row- Parameters:
sheet- The sheet containing the rowrow- The row number (1-based)- Returns:
- true if row is hidden, false otherwise
- Throws:
ortus.boxlang.runtime.types.exceptions.BoxRuntimeException- if sheet is null or row is invalid
-
getActiveSheet
public static org.apache.poi.ss.usermodel.Sheet getActiveSheet(org.apache.poi.ss.usermodel.Workbook workbook) Gets the active sheet from the workbook- Parameters:
workbook- The workbook- Returns:
- The active sheet
-
setActiveSheet
Sets the active sheet by name- Parameters:
workbook- The workbooksheetName- The name of the sheet to make active- Throws:
ortus.boxlang.runtime.types.exceptions.BoxRuntimeException- If the sheet doesn't exist
-
setActiveSheet
public static void setActiveSheet(org.apache.poi.ss.usermodel.Workbook workbook, int sheetIndex) Sets the active sheet by index (0-based)- Parameters:
workbook- The workbooksheetIndex- The index of the sheet to make active (0-based)- Throws:
ortus.boxlang.runtime.types.exceptions.BoxRuntimeException- If the sheet index is invalid
-
renameSheet
public static void renameSheet(org.apache.poi.ss.usermodel.Workbook workbook, String oldName, String newName) Renames an existing sheet- Parameters:
workbook- The workbookoldName- The current name of the sheetnewName- The new name for the sheet- Throws:
ortus.boxlang.runtime.types.exceptions.BoxRuntimeException- If the sheet doesn't exist or new name is invalid
-
removeSheetByNumber
public static void removeSheetByNumber(org.apache.poi.ss.usermodel.Workbook workbook, int sheetNumber) Removes a sheet by index (1-based)- Parameters:
workbook- The workbooksheetNumber- The sheet index (1-based)- Throws:
ortus.boxlang.runtime.types.exceptions.BoxRuntimeException- If the sheet index is invalid
-
getOrCreateCell
public static org.apache.poi.ss.usermodel.Cell getOrCreateCell(org.apache.poi.ss.usermodel.Sheet sheet, int row, int col) Gets a cell at the specified row and column, creating it if it doesn't exist- Parameters:
sheet- The sheet containing the cellrow- The row number (1-based)col- The column number (1-based)- Returns:
- The cell object
-
getCell
public static org.apache.poi.ss.usermodel.Cell getCell(org.apache.poi.ss.usermodel.Sheet sheet, int row, int col) Gets a cell at the specified row and column, returning null if it doesn't exist- Parameters:
sheet- The sheet containing the cellrow- The row number (1-based)col- The column number (1-based)- Returns:
- The cell object or null if it doesn't exist
-
isSpreadsheetFile
Determines if a file is a valid spreadsheet file- Parameters:
filePath- The absolute path to the file- Returns:
- True if the file is a valid spreadsheet, false otherwise
-
isXmlFormat
public static boolean isXmlFormat(org.apache.poi.ss.usermodel.Workbook workbook) Determines if a workbook is in XML format (.xlsx)- Parameters:
workbook- The workbook to check- Returns:
- True if the workbook is in XML format, false if binary format
-
isBinaryFormat
public static boolean isBinaryFormat(org.apache.poi.ss.usermodel.Workbook workbook) Determines if a workbook is in binary format (.xls)- Parameters:
workbook- The workbook to check- Returns:
- True if the workbook is in binary format, false if XML format
-
getRowCount
public static int getRowCount(org.apache.poi.ss.usermodel.Workbook workbook) Gets the number of rows that contain data in the active sheet- Parameters:
workbook- The workbook- Returns:
- The number of rows with data
-
getRowCount
public static int getRowCount(org.apache.poi.ss.usermodel.Sheet sheet) Gets the number of rows that contain data in the specified sheet- Parameters:
sheet- The sheet- Returns:
- The number of rows with data
-
getColumnCount
public static int getColumnCount(org.apache.poi.ss.usermodel.Workbook workbook) Gets the number of columns that contain data in the active sheet- Parameters:
workbook- The workbook- Returns:
- The number of columns with data
-
getColumnCount
public static int getColumnCount(org.apache.poi.ss.usermodel.Sheet sheet) Gets the number of columns that contain data in the specified sheet- Parameters:
sheet- The sheet- Returns:
- The number of columns with data
-
autoSizeColumns
public static void autoSizeColumns(org.apache.poi.ss.usermodel.Workbook workbook) Auto-sizes all columns in the active sheet- Parameters:
workbook- The workbook
-
autoSizeColumns
public static void autoSizeColumns(org.apache.poi.ss.usermodel.Sheet sheet) Auto-sizes all columns in the specified sheet- Parameters:
sheet- The sheet
-
autoSizeColumn
public static void autoSizeColumn(org.apache.poi.ss.usermodel.Workbook workbook, int column) Auto-sizes a specific column in the active sheet- Parameters:
workbook- The workbookcolumn- The column number (1-based)
-
autoSizeColumn
public static void autoSizeColumn(org.apache.poi.ss.usermodel.Sheet sheet, int column) Auto-sizes a specific column in the specified sheet- Parameters:
sheet- The sheetcolumn- The column number (1-based)
-
deleteColumn
public static void deleteColumn(org.apache.poi.ss.usermodel.Sheet sheet, int column) Deletes a column by clearing all cells in that column Note: Apache POI doesn't support true column deletion, so we clear the column content- Parameters:
sheet- The sheetcolumn- The column number to delete (1-based)
-
deleteColumns
public static void deleteColumns(org.apache.poi.ss.usermodel.Sheet sheet, int startColumn, int endColumn) Deletes multiple columns by clearing all cells in those columns- Parameters:
sheet- The sheetstartColumn- The starting column number (1-based, inclusive)endColumn- The ending column number (1-based, inclusive)
-
formatCell
public static void formatCell(org.apache.poi.ss.usermodel.Cell cell, ortus.boxlang.runtime.types.IStruct format) Applies formatting to a specific cell based on a format structure- Parameters:
cell- The cell to formatformat- A structure containing formatting options
-
setHeader
public static void setHeader(org.apache.poi.ss.usermodel.Sheet sheet, ortus.boxlang.runtime.types.IStruct header) Sets the header text for the specified sheet. Expects a struct with optional keys: left, center, right- Parameters:
sheet- The sheet to updateheader- The header struct containing left/center/right text
-
setHeaderImage
public static void setHeaderImage(org.apache.poi.ss.usermodel.Sheet sheet, String alignment, String imagePath, org.apache.poi.ss.usermodel.Workbook workbook) Sets an image in the header of the specified sheet- Parameters:
sheet- The sheet to updatealignment- The alignment: "left", "center", or "right"imagePath- The absolute path to the image fileworkbook- The workbook instance
-
setCellComment
public static void setCellComment(org.apache.poi.ss.usermodel.Cell cell, Object commentObj, String author) Sets a comment on a cell with optional formatting- Parameters:
cell- The cell to add comment tocommentObj- The comment text (String) or struct with comment detailsauthor- The comment author (optional, used if commentObj is a String)
-
getCellComment
Gets a comment from a cell- Parameters:
cell- The cell to get comment from- Returns:
- The comment text, or null if no comment exists
-
getCellCommentAsStruct
public static ortus.boxlang.runtime.types.IStruct getCellCommentAsStruct(org.apache.poi.ss.usermodel.Cell cell) Get cell comment as a struct with Author, Column, Comment, Row keys- Parameters:
cell- The cell to get the comment from- Returns:
- A struct with {Author, Column, Comment, Row} or null if no comment exists
-
getAllCellComments
public static ortus.boxlang.runtime.types.Array getAllCellComments(org.apache.poi.ss.usermodel.Sheet sheet) Get all cell comments from a sheet as an array of structs- Parameters:
sheet- The sheet to get comments from- Returns:
- An array of structs with {Author, Column, Comment, Row} for each comment
-
setDocumentProperties
public static void setDocumentProperties(org.apache.poi.ss.usermodel.Workbook workbook, ortus.boxlang.runtime.types.IStruct properties) Sets document properties/metadata for a workbook- Parameters:
workbook- The workbook to set properties onproperties- A struct containing metadata properties (AUTHOR, TITLE, SUBJECT, COMMENTS, KEYWORDS, CATEGORY, MANAGER, COMPANY, LASTAUTHOR)
-
createSplitPane
public static void createSplitPane(org.apache.poi.ss.usermodel.Sheet sheet, int xSplitPos, int ySplitPos, int leftmostColumn, int topRow, int activePane) Creates a split pane in a sheet- Parameters:
sheet- The sheet to add split pane toxSplitPos- The horizontal (x-axis) position of the split in 1/20th of a pixel unitsySplitPos- The vertical (y-axis) position of the split in 1/20th of a pixel unitsleftmostColumn- The leftmost column visible in the right pane (1-based, will be converted to 0-based for POI)topRow- The top row visible in the bottom pane (1-based, will be converted to 0-based for POI)activePane- The active pane indicator (0=lower-left, 1=upper-right, 2=lower-right, 3=upper-left)
-
createSplitPane
public static void createSplitPane(org.apache.poi.ss.usermodel.Sheet sheet, int xSplitPos, int ySplitPos, int leftmostColumn, int topRow) Creates a split pane in a sheet with default active pane- Parameters:
sheet- The sheet to add split pane toxSplitPos- The horizontal position of the splitySplitPos- The vertical position of the splitleftmostColumn- The leftmost column visible in the right panetopRow- The top row visible in the bottom pane
-
formatRow
public static void formatRow(org.apache.poi.ss.usermodel.Sheet sheet, int row, ortus.boxlang.runtime.types.IStruct format) Formats all cells in a single row- Parameters:
sheet- The sheet containing the rowrow- The row number (1-based)format- A structure containing formatting options
-
formatRows
public static void formatRows(org.apache.poi.ss.usermodel.Sheet sheet, String rows, ortus.boxlang.runtime.types.IStruct format) Formats all cells in multiple rows specified by a comma-separated string or range Format: "1,3,5" or "1-5,7-10" or combinations like "1-3,5,7-10"- Parameters:
sheet- The sheet containing the rowsrows- A string specifying which rows to formatformat- A structure containing formatting options
-
formatColumn
public static void formatColumn(org.apache.poi.ss.usermodel.Sheet sheet, int column, ortus.boxlang.runtime.types.IStruct format) Formats all cells in a single column- Parameters:
sheet- The sheet containing the columncolumn- The column number (1-based)format- A structure containing formatting options
-
formatColumns
public static void formatColumns(org.apache.poi.ss.usermodel.Sheet sheet, String columns, ortus.boxlang.runtime.types.IStruct format) Formats all cells in multiple columns specified by a comma-separated string or range Format: "1,3,5" or "1-5,7-10" or combinations like "1-3,5,7-10"- Parameters:
sheet- The sheet containing the columnscolumns- A string specifying which columns to formatformat- A structure containing formatting options
-
shiftColumns
public static void shiftColumns(org.apache.poi.ss.usermodel.Sheet sheet, int start, int end, int n) Shifts columns left or right in a sheet Note: Apache POI doesn't provide a native column shift operation, so we implement it manually by copying cell data from the source columns to the destination columns.- Parameters:
sheet- The sheet containing the columnsstart- The starting column number (1-based)end- The ending column number (1-based)n- The number of columns to shift (positive for right, negative for left)
-
addImage
public static void addImage(org.apache.poi.ss.usermodel.Sheet sheet, String filepath, int row, int col, org.apache.poi.ss.usermodel.Workbook workbook) Adds an image to a sheet at a specified cell location- Parameters:
sheet- The sheet to add the image tofilepath- The absolute path to the image filerow- The starting row index (1-based, converted to 0-based)col- The starting column index (1-based, converted to 0-based)workbook- The workbook instance (needed to get image bytes)
-
addImage
public static void addImage(org.apache.poi.ss.usermodel.Sheet sheet, String filepath, int startRow, int startCol, int endRow, int endCol, org.apache.poi.ss.usermodel.Workbook workbook) Adds an image with custom anchor coordinates- Parameters:
sheet- The sheet to add the image tofilepath- The absolute path to the image filestartRow- The starting row (1-based)startCol- The starting column (1-based)endRow- The ending row (1-based)endCol- The ending column (1-based)workbook- The workbook instance
-
setCellHyperlink
public static void setCellHyperlink(org.apache.poi.ss.usermodel.Sheet sheet, int row, int column, String hyperlink, String label, org.apache.poi.ss.usermodel.Workbook workbook) Sets a hyperlink on a cell- Parameters:
sheet- The sheet containing the cellrow- The row number (1-based)column- The column number (1-based)hyperlink- The hyperlink URLlabel- The optional display label for the hyperlinkworkbook- The workbook instance
-
getCellHyperlink
public static ortus.boxlang.runtime.types.IStruct getCellHyperlink(org.apache.poi.ss.usermodel.Sheet sheet, int row, int column) Gets the hyperlink from a cell- Parameters:
sheet- The sheet containing the cellrow- The row number (1-based)column- The column number (1-based)- Returns:
- A struct containing hyperlink information, or null if no hyperlink exists
-
formatCellRange
public static void formatCellRange(org.apache.poi.ss.usermodel.Sheet sheet, ortus.boxlang.runtime.types.IStruct format, int startRow, int startCol, int endRow, int endCol, org.apache.poi.ss.usermodel.Workbook workbook) Formats a range of cells with a single format struct- Parameters:
sheet- The sheet containing the cellsformat- The format struct containing style propertiesstartRow- The starting row (1-based)startCol- The starting column (1-based)endRow- The ending row (1-based)endCol- The ending column (1-based)workbook- The workbook instance
-
setSheetPrintOrientation
public static void setSheetPrintOrientation(org.apache.poi.ss.usermodel.Sheet sheet, String orientation) Sets the sheet print orientation- Parameters:
sheet- The sheet to modifyorientation- The orientation ("portrait" or "landscape")
-
setRepeatingColumns
public static void setRepeatingColumns(org.apache.poi.ss.usermodel.Workbook workbook, org.apache.poi.ss.usermodel.Sheet sheet, int startCol, int endCol) Sets columns to repeat on every printed page- Parameters:
workbook- The workbook instancesheet- The sheet to modifystartCol- The starting column (1-based)endCol- The ending column (1-based)
-
setRepeatingRows
public static void setRepeatingRows(org.apache.poi.ss.usermodel.Workbook workbook, org.apache.poi.ss.usermodel.Sheet sheet, int startRow, int endRow) Sets rows to repeat on every printed page- Parameters:
workbook- The workbook instancesheet- The sheet to modifystartRow- The starting row (1-based)endRow- The ending row (1-based)
-
groupRows
public static void groupRows(org.apache.poi.ss.usermodel.Sheet sheet, int startRow, int endRow, Boolean isCollapsed) Groups rows together in the active sheet- Parameters:
sheet- The sheet to group rows instartRow- The starting row number (1-based)endRow- The ending row number (1-based)isCollapsed- Whether the group should be initially collapsed (optional)
-
ungroupRows
public static void ungroupRows(org.apache.poi.ss.usermodel.Sheet sheet, int startRow, int endRow) Ungroups rows in the active sheet- Parameters:
sheet- The sheet to ungroup rows instartRow- The starting row number (1-based)endRow- The ending row number (1-based)
-
groupColumns
public static void groupColumns(org.apache.poi.ss.usermodel.Sheet sheet, int startColumn, int endColumn, Boolean isCollapsed) Groups columns together in the active sheet- Parameters:
sheet- The sheet to group columns instartColumn- The starting column number (1-based)endColumn- The ending column number (1-based)isCollapsed- Whether the group should be initially collapsed (optional)
-
ungroupColumns
public static void ungroupColumns(org.apache.poi.ss.usermodel.Sheet sheet, int startColumn, int endColumn) Ungroups columns in the active sheet- Parameters:
sheet- The sheet to ungroup columns instartColumn- The starting column number (1-based)endColumn- The ending column number (1-based)
-
getAutoCalculate
public static boolean getAutoCalculate(org.apache.poi.ss.usermodel.Workbook workbook) Gets whether formulas automatically calculate- Parameters:
workbook- The workbook to check- Returns:
- True if auto-calculation is enabled (XSSF only, always true for HSSF)
-
setAutoCalculate
public static void setAutoCalculate(org.apache.poi.ss.usermodel.Workbook workbook, boolean autoCalculate) Sets whether formulas automatically calculate- Parameters:
workbook- The workbook to updateautoCalculate- Whether to enable auto-calculation
-
getRecalculateFormulasOnNextOpen
public static boolean getRecalculateFormulasOnNextOpen(org.apache.poi.ss.usermodel.Workbook workbook) Gets whether formulas will be recalculated when the file is opened- Parameters:
workbook- The workbook to check- Returns:
- True if force recalculation is enabled, false otherwise
-
setRecalculateFormulasOnNextOpen
public static void setRecalculateFormulasOnNextOpen(org.apache.poi.ss.usermodel.Workbook workbook, boolean recalculate) Sets whether formulas should be recalculated when the file is opened- Parameters:
workbook- The workbook to updaterecalculate- Whether to force recalculation on next open
-
hideSheet
Hides a sheet from view- Parameters:
workbook- The workbook containing the sheetsheetName- The name of the sheet to hide
-
unhideSheet
Unhides a sheet to make it visible- Parameters:
workbook- The workbook containing the sheetsheetName- The name of the sheet to unhide
-
moveSheet
public static void moveSheet(org.apache.poi.ss.usermodel.Workbook workbook, int fromIndex, int toIndex) Moves a sheet to a new position by sheet index- Parameters:
workbook- The workbook containing the sheetfromIndex- The current sheet index (0-based)toIndex- The target sheet index (0-based)
-
moveSheet
public static void moveSheet(org.apache.poi.ss.usermodel.Workbook workbook, String sheetName, int toIndex) Moves a sheet to a new position by sheet name- Parameters:
workbook- The workbook containing the sheetsheetName- The name of the sheet to movetoIndex- The target sheet index (0-based)
-
getCellAddress
public static String getCellAddress(org.apache.poi.ss.usermodel.Workbook workbook, int row, int column) Gets the cell address (e.g., "A1", "B5") for a given row and column- Parameters:
workbook- The workbook containing the cellrow- The row number (1-based)column- The column number (1-based)- Returns:
- The cell address string (e.g., "A1")
-
getInfo
public static ortus.boxlang.runtime.types.IStruct getInfo(org.apache.poi.ss.usermodel.Workbook workbook) Gets information about the spreadsheet as a struct- Parameters:
workbook- The workbook containing the sheet- Returns:
- A struct containing sheet properties
-
getSheetsVisibility
public static ortus.boxlang.runtime.types.IStruct getSheetsVisibility(org.apache.poi.ss.usermodel.Workbook workbook) Gets the visibility status of all sheets in the workbook- Parameters:
workbook- The workbook to get sheet visibility from- Returns:
- A struct with sheet names as keys and visibility status ("visible", "hidden", "veryHidden") as values
-
hasSheet
Checks if a sheet with the given name exists in the workbook- Parameters:
workbook- The workbook to checksheetName- The name of the sheet to look for- Returns:
- True if the sheet exists, false otherwise
-
getSheetNames
public static ortus.boxlang.runtime.types.Array getSheetNames(org.apache.poi.ss.usermodel.Workbook workbook) Gets the names of all sheets in the workbook- Parameters:
workbook- The workbook to get sheet names from- Returns:
- An array of sheet names
-
recalculateAllFormulas
public static void recalculateAllFormulas(org.apache.poi.ss.usermodel.Workbook workbook) Recalculates all formulas in the workbook- Parameters:
workbook- The workbook containing formulas to recalculate
-