XLSX
@office-open/xlsx
Generate .xlsx spreadsheets with a declarative TypeScript API
Installation
pnpm add @office-open/xlsx
Quick Start
{
"worksheets": [
{
"name": "Sheet1",
"children": [
{ "cells": [{ "value": "Name" }, { "value": "Score" }] },
{ "cells": [{ "value": "Alice" }, { "value": 95 }] },
{ "cells": [{ "value": "Bob" }, { "value": 88 }] }
]
}
]
}
import { Workbook, Packer } from "@office-open/xlsx";
const wb = new Workbook({
worksheets: [
{
name: "Sheet1",
children: [
{ cells: [{ value: "Name" }, { value: "Score" }] },
{ cells: [{ value: "Alice" }, { value: 95 }] },
{ cells: [{ value: "Bob" }, { value: 88 }] },
],
},
],
});
const buffer = await Packer.toBuffer(wb);
Main Components
| Component | Description |
|---|---|
Workbook | Root workbook container with worksheets and properties |
Worksheet | Individual sheet with cells, styles, and features |
Cell | Cell with value, style, and formatting |
Row | Row container with height and visibility |
Column | Column definition with width and range |
Styles | Index-based style system (fonts, fills, borders) |
SharedStrings | Shared string table for string deduplication |
Patching
Modify existing .xlsx templates by replacing {{placeholder}} tokens with new content:
import { patchWorkbook } from "@office-open/xlsx";
const result = await patchWorkbook({
outputType: "nodebuffer",
data: templateBuffer,
patches: {
name: { value: "John Doe" },
amount: { value: 1500 },
date: { value: "2024-12-31" },
},
});
See Patching for full documentation.
Parsing
Parse existing .xlsx files into WorkbookOptions for inspection or round-trip workflows:
import { parseWorkbook } from "@office-open/xlsx";
const opts = parseWorkbook(buffer);
// opts.worksheets — worksheet array
// opts.worksheets[0].children — rows with cells
// opts.title, opts.creator — core properties
See Parsing for full documentation.
Export Formats
Every async method has a synchronous counterpart (e.g., toBufferSync). See the Export page for the full list.
| Method | Returns | Use Case |
|---|---|---|
Packer.toBuffer(wb) | Buffer | Node.js file I/O |
Packer.toBlob(wb) | Blob | Browser downloads |
Packer.toBase64String(wb) | string | Data URLs, API payloads |
Packer.toString(wb) | string | Debugging, inspection |
Packer.toStream(wb) | ReadableStream<Uint8Array> | Streaming large files |