Skip to content

Getting Started

ABCrimson edited this page Mar 2, 2026 · 17 revisions

Getting Started

Installation

npm install modern-xlsx
# or
pnpm add modern-xlsx
# or
yarn add modern-xlsx

Requires a runtime with WASM support: Node.js 25+, Bun, Deno, or modern browsers.

Initialize WASM

Before any operation, initialize the WASM module once:

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

This loads and compiles the ~870 KB WASM binary. Call it once at application startup.

Create a Workbook

import { Workbook } from 'modern-xlsx';

const wb = new Workbook();
const ws = wb.addSheet('Sheet1');

// Set values
ws.cell('A1').value = 'Name';
ws.cell('B1').value = 'Age';
ws.cell('A2').value = 'Alice';
ws.cell('B2').value = 30;

Apply Styles

const headerStyle = wb.createStyle()
  .font({ bold: true, size: 12, color: '1F4E79' })
  .fill({ pattern: 'solid', fgColor: 'D6E4F0' })
  .alignment({ horizontal: 'center' })
  .build(wb.styles);

ws.cell('A1').styleIndex = headerStyle;
ws.cell('B1').styleIndex = headerStyle;

Write to File

// Node.js / Bun / Deno
await wb.toFile('output.xlsx');

// Any environment (returns Uint8Array)
const buffer = await wb.toBuffer();

// Browser (returns Blob)
import { writeBlob } from 'modern-xlsx';
const blob = writeBlob(wb);

Read a File

import { readFile, readBuffer } from 'modern-xlsx';

// From file path
const wb = await readFile('data.xlsx');

// From buffer
const wb = await readBuffer(uint8Array);

// Access data
const ws = wb.getSheet('Sheet1');
console.log(ws?.cell('A1').value);

Next Steps

Clone this wiki locally