ExcelWriter
The class that builds a workbook: sheets, cells, styles, merged cells, filters, frozen panes, images and saving.
ExcelWriter Class#
Constructor#
excel = new ExcelWriter()Creates a new Excel workbook.
Document Properties#
| Method | Description |
|---|---|
setAuthor(author) | Set document author |
setTitle(title) | Set document title |
setCompany(company) | Set company name |
Example:
excel.setAuthor("John Doe")
excel.setTitle("Sales Report 2025")
excel.setCompany("ABC Corporation")Sheet Management#
| Method | Description |
|---|---|
addSheet(name) | Add a new worksheet |
selectSheet(index) | Select sheet by index (1-based) |
selectSheetByName(name) | Select sheet by name |
getSheetCount() | Get number of sheets |
Example:
excel.addSheet("Sales")
excel.addSheet("Expenses")
excel.selectSheet(1) # Select first sheet
excel.selectSheetByName("Expenses") # Select by name
? excel.getSheetCount() # Output: 2Cell Operations#
| Method | Description |
|---|---|
setCell(row, col, value) | Set cell value (auto-detects type) |
setCellWithStyle(row, col, value, styleId) | Set cell with specific style |
setFormula(row, col, formula) | Set cell formula (without =) |
setNumber(row, col, value) | Set numeric value |
setDate(row, col, year, month, day) | Set date value |
Example:
excel.setCell(1, 1, "Text") # String
excel.setCell(1, 2, 42) # Number
excel.setCell(1, 3, 3.14159) # Decimal
excel.setFormula(1, 4, "A1&B1") # Formula (without =)
excel.setFormula(2, 1, "SUM(A1:C1)") # Sum formula
excel.setDate(3, 1, 2025, 12, 25) # DateBulk Data Operations#
| Method | Description |
|---|---|
exportList(data, startRow, startCol, hasHeader) | Export 2D list to cells |
setRow(row, values, startCol) | Set entire row |
setColumn(col, values, startRow) | Set entire column |
Example:
# Export a 2D list with header formatting
data = [
["Name", "Score"],
["Alice", 95],
["Bob", 87]
]
excel.exportList(data, 1, 1, true) # true = first row is header
# Set a row of values
excel.setRow(5, ["Total", 182], 1)
# Set a column of values
excel.setColumn(3, ["A", "B", "C"], 1)Styling#
Creating Styles#
styleId = excel.createStyle(options)Available Options:
| Option | Type | Description |
|---|---|---|
:bold | Boolean | Bold text |
:italic | Boolean | Italic text |
:fontName | String | Font name (e.g., "Arial") |
:fontSize | Number | Font size in points |
:fontColor | String | Font color (name or hex) |
:bgColor | String | Background color (name or hex) |
:align | String | Horizontal alignment: "left", "center", "right" |
:valign | String | Vertical alignment: "top", "center", "bottom" |
:wrap | Boolean | Wrap text in cell |
:border | String | Border style (see below) |
:borderColor | String | Border color (name or hex) |
:numberFormat | Number | Number format ID (see constants) |
Example:
# Create a custom style
myStyle = excel.createStyle([
:bold = true,
:italic = true,
:fontName = "Arial",
:fontSize = 14,
:fontColor = "FF0000",
:bgColor = "FFFF00",
:align = "center",
:valign = "center",
:wrap = true,
:border = "thin",
:borderColor = "000000"
])
excel.setCellWithStyle(1, 1, "Styled Cell", myStyle)Pre-defined Style Methods#
| Method | Description |
|---|---|
createHeaderStyle() | Blue background, white bold text, centered |
createCurrencyStyle() | Currency number format |
createPercentageStyle() | Percentage number format |
createDateStyle() | Date number format |
Example:
headerStyle = excel.createHeaderStyle()
currencyStyle = excel.createCurrencyStyle()
excel.setCellWithStyle(1, 1, "Price", headerStyle)
excel.setCellWithStyle(2, 1, 99.99, currencyStyle)Column and Row Formatting#
| Method | Description |
|---|---|
setColumnWidth(col, width) | Set column width (in characters) |
setRowHeight(row, height) | Set row height (in points) |
autoFitColumn(col) | Auto-fit column width (approximate) |
Example:
excel.setColumnWidth(1, 20) # Column A = 20 characters wide
excel.setColumnWidth(2, 15) # Column B = 15 characters wide
excel.setRowHeight(1, 30) # Row 1 = 30 points tallMerged Cells#
excel.mergeCells(row1, col1, row2, col2)Merges cells from (row1, col1) to (row2, col2).
Example:
excel.setCell(1, 1, "Title")
excel.mergeCells(1, 1, 1, 4) # Merge A1:D1
excel.setCell(3, 1, "Subtitle")
excel.mergeCells(3, 1, 5, 1) # Merge A3:A5 (vertical)Auto Filter#
excel.setAutoFilter(row1, col1, row2, col2)Adds filter dropdowns to the specified range.
Example:
# Add headers and data rows...
# Enable filter on the data range
excel.setAutoFilter(1, 1, 10, 3) # A1:C10Freeze Panes#
| Method | Description |
|---|---|
freezePanes(row, col) | Freeze above row and left of column |
freezeTopRow() | Freeze first row only |
freezeFirstColumn() | Freeze first column only |
Example:
excel.freezeTopRow() # Freeze row 1
excel.freezeFirstColumn() # Freeze column A
excel.freezePanes(2, 2) # Freeze row 1 AND column AImages#
excel.addImage(imagePath, row, col, width, height)Parameters:
imagePath- Path to image file (PNG, JPG, BMP, GIF)row,col- Cell position for top-left cornerwidth- Width in cells (default: 5)height- Height in cells (default: 5)
Example:
excel.addImage("logo.png", 1, 1, 4, 3) # A1, 4 cells wide, 3 cells tall
excel.addImage("chart.jpg", 10, 1, 8, 6) # A10, 8x6 cellsSupported Formats: PNG, JPG, JPEG, BMP, GIF
Saving#
result = excel.save(filename)Returns true if successful, false otherwise.