Skip to content

API Reference

ABCrimson edited this page Mar 2, 2026 · 10 revisions

API Reference

Initialization

initWasm()

Initialize the WASM module. Must be called once before any other operation.

import { initWasm } from 'modern-xlsx';
await initWasm();

I/O Functions

readFile(path: string): Promise<Workbook>

Read an XLSX file from disk. Node.js / Bun / Deno only.

readBuffer(data: Uint8Array): Promise<Workbook>

Read an XLSX file from a buffer. Works in all environments.

writeBlob(wb: Workbook): Blob

Write a workbook to a Blob for browser downloads.

Workbook

Constructor

const wb = new Workbook();

Properties

Property Type Description
sheetNames string[] Names of all sheets
sheetCount number Number of sheets
dateSystem 'date1900' | 'date1904' Date epoch system
styles StylesData Styles collection
namedRanges DefinedNameData[] Named ranges
docProperties DocPropertiesData | null Document metadata
workbookViews WorkbookViewData[] View settings

Methods

Method Returns Description
addSheet(name) Worksheet Add a new sheet
getSheet(name) Worksheet | undefined Get sheet by name
getSheetByIndex(i) Worksheet | undefined Get sheet by index
removeSheet(nameOrIndex) boolean Remove a sheet
createStyle() StyleBuilder Create a style builder
addNamedRange(name, value, sheetId?) void Add named range
getNamedRange(name) DefinedNameData | undefined Get named range
removeNamedRange(name) boolean Remove named range
toBuffer() Promise<Uint8Array> Serialize to buffer
toFile(path) Promise<void> Write to file
toJSON() WorkbookData Get raw data

Worksheet

Properties

Property Type Description
name string Sheet name (read/write)
rows RowData[] All rows
columns ColumnInfo[] Column definitions
mergeCells string[] Merge ranges
autoFilter AutoFilterData | null Auto filter config
frozenPane FrozenPane | null Frozen pane config
hyperlinks HyperlinkData[] Hyperlinks
validations DataValidationData[] Validations
comments CommentData[] Cell comments
pageSetup PageSetupData | null Page setup
pageMargins PageMarginsData | null Page margins
sheetProtection SheetProtectionData | null Protection

Methods

Method Returns Description
cell(ref) Cell Get or create a cell
setColumnWidth(col, width) void Set column width
setRowHeight(row, height) void Set row height
setRowHidden(row, hidden) void Hide/show row
addMergeCell(range) void Add merge
removeMergeCell(range) boolean Remove merge
addHyperlink(ref, location, opts?) void Add link
removeHyperlink(ref) boolean Remove link
addValidation(ref, rule) void Add validation
removeValidation(ref) boolean Remove validation
addComment(ref, author, text) void Add comment
removeComment(ref) boolean Remove comment

Cell

Properties

Property Type Description
reference string Cell reference (e.g. "A1")
type CellType Value type
value string | number | boolean | null Cell value (read/write)
formula string | null Formula (read/write)
styleIndex number | null Style index (read/write)

StyleBuilder

Fluent builder for creating cell styles:

const idx = wb.createStyle()
  .font({ name: 'Arial', size: 12, bold: true, color: 'FF0000' })
  .fill({ pattern: 'solid', fgColor: 'FFFF00' })
  .border({ top: { style: 'thin', color: '000000' } })
  .alignment({ horizontal: 'center', wrapText: true })
  .protection({ locked: true })
  .numberFormat('#,##0.00')
  .build(wb.styles);

Utility Functions

Sheet Conversions

Function Description
aoaToSheet(data, opts?) Array-of-arrays to Worksheet
jsonToSheet(data, opts?) JSON array to Worksheet
sheetToJson(ws, opts?) Worksheet to JSON array
sheetToCsv(ws, opts?) Worksheet to CSV string
sheetToHtml(ws, opts?) Worksheet to HTML table
sheetAddAoa(ws, data, opts?) Append array data
sheetAddJson(ws, data, opts?) Append JSON data

Date Utilities

Function Description
dateToSerial(date) Date/Temporal to Excel serial
serialToDate(serial) Excel serial to Date
isDateFormatId(id) Check if built-in date format
isDateFormatCode(code) Check if date format string

Cell References

Function Description
encodeCellRef(row, col) (0, 0)"A1"
decodeCellRef(ref) "A1"{ row: 0, col: 0 }
encodeRange(start, end) Addresses to range string
decodeRange(range) Range string to addresses
columnToLetter(col) 0"A"
letterToColumn(letter) "A"0

Formatting

Function Description
formatCell(value, format) Format value with Excel format code
getBuiltinFormat(id) Get built-in format string by ID

Clone this wiki locally