XLSX
@office-open/xlsx
Generate, parse, and patch .xlsx spreadsheets with a declarative TypeScript API
Installation
pnpm add @office-open/xlsx
npm install @office-open/xlsx
yarn add @office-open/xlsx
bun add @office-open/xlsx
Quick Start
{
"worksheets": [
{
"name": "Sheet1",
"rows": [
{ "cells": [{ "value": "Name" }, { "value": "Score" }] },
{ "cells": [{ "value": "Alice" }, { "value": 95 }] },
{ "cells": [{ "value": "Bob" }, { "value": 88 }] }
]
}
]
}
import { generateWorkbook } from "@office-open/xlsx";
const buffer = await generateWorkbook({
worksheets: [
{
name: "Sheet1",
rows: [
{ cells: [{ value: "Name" }, { value: "Score" }] },
{ cells: [{ value: "Alice" }, { value: 95 }] },
{ cells: [{ value: "Bob" }, { value: 88 }] },
],
},
],
});
Main Components
| Component | Description |
|---|---|
worksheets array | Root workbook options with worksheets and properties |
worksheet | Individual sheet with rows, styles, and features |
rows, cells, columns | Rows, cells, and columns with value, style, height, and width |
styles | Index-based style system (fonts, fills, borders) |
sharedStrings | Shared string table for string deduplication |
table | Data tables with filtering, sorting, and totals |
pivotTable | Data pivot tables with aggregation and filtering |
drawing | Images and charts anchored to cells |
comments | Cell comments with author tracking and rich text |
hyperlink | External and internal hyperlinks |
mergeCells | Merge cell ranges across rows and columns |
dataValidation | List, number, date, and custom validation rules |
conditionalFormatting | Cell value-based formatting rules |
autoFilter | Column header filter dropdowns |
freezePane | Freeze rows and columns for scrollable headers |
sheetProtection | Password-protect worksheets and editable ranges |
revisionLog | Track changes with revision logs |
pageSetup | Page size, margins, orientation, and print settings |
headerFooter | Print headers and footers |
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,
placeholders: {
name: "John Doe",
amount: 1500,
date: new Date("2024-12-31"),
},
});
See 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].rows — rows with cells
// opts.title, opts.creator — core properties
See for full documentation.
Export Formats
Every async function has a synchronous counterpart (e.g., generateWorkbookSync). See the page for the full list.
| Function | Returns | Use Case |
|---|---|---|
generateWorkbook(opts) | Buffer | Node.js file I/O |
generateWorkbook(opts, { type: "blob" }) | Blob | Browser downloads |
generateWorkbook(opts, { type: "base64" }) | string | Data URLs, API payloads |
generateWorkbook(opts, { type: "string" }) | string | Debugging, inspection |
generateWorkbookStream(opts) | ReadableStream<Uint8Array> | Streaming large files |