XLSX
Quick Start
Create your first .xlsx spreadsheet in 5 minutes with @office-open/xlsx
Create a spreadsheet from scratch in three steps: define data, build a workbook, and export.
Step 1 — Install
pnpm add @office-open/xlsx
Step 2 — Create a Workbook
import { Workbook, Packer } from "@office-open/xlsx";
const wb = new Workbook({
worksheets: [
{
name: "Sales",
children: [
{
cells: [
{ value: "Product", style: { font: { bold: true } } },
{ value: "Q1", style: { font: { bold: true } } },
{ value: "Q2", style: { font: { bold: true } } },
],
},
{ cells: [{ value: "Widget" }, { value: 100 }, { value: 150 }] },
{ cells: [{ value: "Gadget" }, { value: 200 }, { value: 250 }] },
],
},
],
});
Or use the JSON API:
{
"worksheets": [
{
"name": "Sales",
"children": [
{ "cells": [{ "value": "Product" }, { "value": "Q1" }, { "value": "Q2" }] },
{ "cells": [{ "value": "Widget" }, { "value": 100 }, { "value": 150 }] },
{ "cells": [{ "value": "Gadget" }, { "value": 200 }, { "value": 250 }] }
]
}
]
}
{
worksheets: [
{
name: "Sales",
children: [
{ cells: [{ value: "Product" }, { value: "Q1" }, { value: "Q2" }] },
{ cells: [{ value: "Widget" }, { value: 100 }, { value: 150 }] },
{ cells: [{ value: "Gadget" }, { value: 200 }, { value: 250 }] },
],
},
],
}
Step 3 — Export
// Node.js — write to file
import { writeFileSync } from "node:fs";
const buffer = await Packer.toBuffer(wb);
writeFileSync("output.xlsx", buffer);
// Browser — download as file
const blob = await Packer.toBlob(wb);
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "output.xlsx";
a.click();
Multiple Worksheets
const wb = new Workbook({
worksheets: [
{
name: "Summary",
children: [{ cells: [{ value: "Total" }, { value: 500 }] }],
},
{
name: "Details",
children: [
{ cells: [{ value: "Item" }, { value: "Amount" }] },
{ cells: [{ value: "A" }, { value: 200 }] },
{ cells: [{ value: "B" }, { value: 300 }] },
],
},
],
});
Output Formats
The Packer class supports multiple output 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 |