Skip to content

ExcelWorkbook ​

An Excel workbook to read, write, style and save.

js
const wb = workbook.excelWorkbook(false);

wb.writeString(0, 0, "Region");
wb.writeString(0, 1, "Revenue");
wb.writeString(1, 0, "North");
wb.writeNumber(1, 1, 5000, "#,##0.00");

wb.save("exports/report.xlsx");

Constructors ​

workbook.excelWorkbook workbook.excelWorkbook(isStreamingWorkbook) ExcelWorkbook ​

Creates a new, empty workbook with one sheet, Sheet1.

ParameterTypeDescription
isStreamingWorkbookbooleantrue for a streaming workbook, for writing very large files. false for a regular workbook.
js
const wb = workbook.excelWorkbook(false);

workbook.excelWorkbook workbook.excelWorkbook(fileName, isStreamingWorkbook, password, rowAccessWindowSize) ExcelWorkbook ​

Opens a workbook from a file. Its first sheet is selected.

ParameterTypeDescription
fileNamestringThe path of the .xlsx file.
isStreamingWorkbookbooleantrue for a streaming workbook, for writing very large files. false for a regular workbook.
passwordstringOptional. The workbook's password. Not supported for streaming workbooks.
rowAccessWindowSizenumberOptional. For streaming workbooks, how many rows to keep in memory.
js
const wb = workbook.excelWorkbook("uploads/budget.xlsx", false);

workbook.fromBase64 workbook.fromBase64(base64String, isStreamingWorkbook, password) ExcelWorkbook ​

Opens a workbook from base64-encoded data. Its first sheet is selected.

ParameterTypeDescription
base64StringstringThe workbook, base64-encoded.
isStreamingWorkbookbooleantrue for a streaming workbook, for writing very large files. false for a regular workbook.
passwordstringOptional. The workbook's password. Not supported for streaming workbooks.
js
const wb = workbook.fromBase64(base64Data, false);

Overview ​

  • Rows and columns start at 0, so cell A1 is row 0, column 0.
  • The current sheet: reads and writes apply to the current sheet. Change it with selectSheet or createSheet.
  • Styling: the setCell methods style a cell that has already been written.
  • Closing: workbooks close automatically when the process finishes.

Streaming workbooks ​

A streaming workbook keeps only a window of rows in memory, so it can write very large files. Rows outside the window can't be read back, so use a regular workbook when you need to read cell values.

Reading formulas ​

The read methods work out a formula's result. If that fails, they throw an error, unless returnCachedOnError is true, which returns the result Excel last stored instead. The readCached methods only use the stored result, which is faster.

Border styles ​

ValueStyle
0None
1Thin
2Medium
3Dashed
4Dotted
5Thick
6Double
7Hairline
8Medium dashed
9Dash-dot
10Medium dash-dot
11Dash-dot-dot
12Medium dash-dot-dot
13Slanted dash-dot

Methods ​

createSheet createSheet(name) ​

Adds a sheet and makes it the current sheet.

ParameterTypeDescription
namestringThe sheet's name. It can't already be used in the workbook.

selectSheet selectSheet(name) ​

Makes a sheet the current sheet.

ParameterTypeDescription
namestringThe sheet's name.

changeSheetName changeSheetName(oldName, newName) ​

Renames a sheet. The current sheet doesn't change.

ParameterTypeDescription
oldNamestringThe sheet's current name.
newNamestringThe sheet's new name.

getSheetName getSheetName(index) string ​

Returns the name of the sheet at a position, starting at 0.

ParameterTypeDescription
indexnumberThe sheet's position.

getNumberOfSheets getNumberOfSheets() number ​

Returns how many sheets the workbook has.

writeString writeString(row, column, value) ​

Writes text to a cell.

ParameterTypeDescription
rownumberThe row.
columnnumberThe column.
valuestringThe text.

writeNumber writeNumber(row, column, value, numberFormat) ​

Writes a number to a cell.

ParameterTypeDescription
rownumberThe row.
columnnumberThe column.
valuenumberThe number.
numberFormatstringOptional. An Excel number format, such as "#,##0.00".

writeFormula writeFormula(row, column, formula) ​

Writes a formula to a cell.

ParameterTypeDescription
rownumberThe row.
columnnumberThe column.
formulastringThe formula, without the leading =, such as "SUM(B2:B10)".

deleteRow deleteRow(rowNumber) ​

Deletes a row. The rows below don't move up, so it leaves an empty row.

ParameterTypeDescription
rowNumbernumberThe row.

copyRange copyRange(srcRange, destRange, copyValues, copyFormulas, copyStyles) ​

Copies a range of cells to another place on the current sheet.

ParameterTypeDescription
srcRangestringThe range to copy, such as "A1:B2".
destRangestringWhere to copy it. Only its top-left cell is used.
copyValuesbooleantrue to copy the values.
copyFormulasbooleantrue to copy the formulas.
copyStylesbooleantrue to copy the styles.

copyRange copyRange(srcSheetName, srcRange, destSheetName, destRange, copyValues, copyFormulas, copyStyles) ​

Copies a range of cells from one sheet to another.

ParameterTypeDescription
srcSheetNamestringThe sheet to copy from.
srcRangestringThe range to copy, such as "A1:B2".
destSheetNamestringThe sheet to copy to.
destRangestringWhere to copy it.
copyValuesbooleantrue to copy the values.
copyFormulasbooleantrue to copy the formulas.
copyStylesbooleantrue to copy the styles.

evaluateAll evaluateAll() ​

Recalculates every formula in the workbook. Call it before saving so the stored results are up to date.

readString readString(row, column, returnCachedOnError) string | null ​

Reads a cell as text. Numbers and booleans are converted to text. Returns null when the cell is empty.

ParameterTypeDescription
rownumberThe row.
columnnumberThe column.
returnCachedOnErrorbooleanOptional. true to return the stored result if a formula fails.

readNumber readNumber(row, column, returnCachedOnError) number | null ​

Reads a cell as a number. Returns null when the cell is empty or holds text.

ParameterTypeDescription
rownumberThe row.
columnnumberThe column.
returnCachedOnErrorbooleanOptional. true to return the stored result if a formula fails.

readBoolean readBoolean(row, column, returnCachedOnError) boolean | null ​

Reads a cell as true or false. Returns null when the cell is empty or isn't a boolean.

ParameterTypeDescription
rownumberThe row.
columnnumberThe column.
returnCachedOnErrorbooleanOptional. true to return the stored result if a formula fails.

readDate readDate(row, column, format, returnCachedOnError) string ​

Reads a date cell as text.

ParameterTypeDescription
rownumberThe row.
columnnumberThe column.
formatstringOptional. The date format, such as "DD/MM/YYYY". Defaults to "YYYY-MM-DD".
returnCachedOnErrorbooleanOptional. true to return the stored result if a formula fails.

readCachedString readCachedString(row, column) string ​

Reads a cell as text, using the result Excel last stored.

ParameterTypeDescription
rownumberThe row.
columnnumberThe column.

readCachedNumber readCachedNumber(row, column) number ​

Reads a cell as a number, using the result Excel last stored.

ParameterTypeDescription
rownumberThe row.
columnnumberThe column.

toMatrix toMatrix(a1Range, cachedOnly) array[] ​

Reads a range of the current sheet as an array of rows, each an array of values. Numbers, booleans and text keep their types.

ParameterTypeDescription
a1RangestringThe range, such as "A1:D20". Use "" for every cell with a value.
cachedOnlybooleantrue to use the results Excel last stored, instead of working out formulas. Much faster on large sheets.

toMatrix toMatrix(cachedOnly) array[] ​

Reads every cell with a value on the current sheet. The same as toMatrix("", cachedOnly).

ParameterTypeDescription
cachedOnlybooleantrue to use the results Excel last stored, instead of working out formulas.

setCellFontColor setCellFontColor(rowNum, colNum, hexColor) ​

Sets a cell's text colour.

ParameterTypeDescription
rowNumnumberThe row.
colNumnumberThe column.
hexColorstringThe colour, such as "#FF0000".

setCellBackgroundColor setCellBackgroundColor(rowNum, colNum, hexColor) ​

Sets a cell's background colour.

ParameterTypeDescription
rowNumnumberThe row.
colNumnumberThe column.
hexColorstringThe colour, such as "#EEEEEE".

setCellAlignment setCellAlignment(rowNum, colNum, horizontal, vertical) ​

Sets how a cell's content is aligned.

ParameterTypeDescription
rowNumnumberThe row.
colNumnumberThe column.
horizontalstring"left", "center", "right" or "justify". Use "" to leave it as it is.
verticalstring"top", "center", "bottom" or "justify". Use "" to leave it as it is.

setCellBorder setCellBorder(rowNum, colNum, borderStyle) ​

Sets the border on all four sides of a cell.

ParameterTypeDescription
rowNumnumberThe row.
colNumnumberThe column.
borderStylenumberThe border style.

setCellBorderSide setCellBorderSide(rowNum, colNum, borderStyle, side) ​

Sets the border on one side of a cell.

ParameterTypeDescription
rowNumnumberThe row.
colNumnumberThe column.
borderStylenumberThe border style.
sidestring"top", "bottom", "left" or "right".

getCellAddress getCellAddress(row, column) string ​

Returns a cell's address, such as "A1" for row 0, column 0.

ParameterTypeDescription
rownumberThe row.
columnnumberThe column.

getColumnName getColumnName(column) string ​

Returns a column's letter, such as "A" for 0 and "AA" for 26.

ParameterTypeDescription
columnnumberThe column.

save save(fileName) ​

Saves the workbook to a file, replacing it if it exists.

ParameterTypeDescription
fileNamestringThe path to save to, such as "exports/report.xlsx".

toBase64 toBase64() string ​

Returns the workbook, base64-encoded, such as to attach to an email.

close close() optional ​

Closes the workbook. Workbooks close automatically when the process finishes, so only call this to free memory early.

Examples ​

Build a styled report ​

js
const wb = workbook.excelWorkbook(false);
wb.changeSheetName("Sheet1", "Sales");

const headers = ["Region", "Units", "Revenue"];
headers.forEach((header, column) => {
    wb.writeString(0, column, header);
    wb.setCellBackgroundColor(0, column, "#EEEEEE");
    wb.setCellBorderSide(0, column, 1, "bottom");
});

wb.writeString(1, 0, "North");
wb.writeNumber(1, 1, 120);
wb.writeNumber(1, 2, 5000, "#,##0.00");

wb.writeString(2, 0, "South");
wb.writeNumber(2, 1, 85);
wb.writeNumber(2, 2, 4200, "#,##0.00");

wb.writeString(3, 0, "Total");
wb.writeFormula(3, 2, "SUM(C2:C3)");

wb.evaluateAll();
wb.save("exports/sales.xlsx");

Read a sheet ​

js
const wb = workbook.excelWorkbook("uploads/budget.xlsx", false);
wb.selectSheet("Budget");

for (const row of wb.toMatrix("A2:C4", false)) {
    console.log(row.join(", "));
}
text
North, 2026, 5000
South, 2026, 4200
East, 2026, 3900

Email a workbook ​

js
const wb = workbook.excelWorkbook(false);
wb.writeString(0, 0, "Hello");

new EmailBuilder()
    .to("user@example.com")
    .subject("Report")
    .body("The report is attached.")
    .attachBase64("report.xlsx", wb.toBase64())
    .send();