Export
The generateWorkbook() function generates a .xlsx file from options and returns the result in the requested format. Use generateWorkbookSync() for synchronous generation and generateWorkbookStream() for streaming.
generateWorkbook
Returns a Buffer (default in Node.js). Best for file I/O.
import { generateWorkbook } from "@office-open/xlsx";
import { writeFileSync } from "node:fs";
// Async (non-blocking)
const buffer = await generateWorkbook({
/* options */
});
writeFileSync("output.xlsx", buffer);
Output Types
Control the output format with the second argument:
// Uint8Array (cross-platform)
const bytes = await generateWorkbook(opts, { type: "uint8array" });
// Blob (browser)
const blob = await generateWorkbook(opts, { type: "blob" });
// Base64 string (API payloads)
const base64 = await generateWorkbook(opts, { type: "base64" });
// ArrayBuffer
const ab = await generateWorkbook(opts, { type: "arraybuffer" });
| type | Returns | Use Case |
|---|---|---|
| "nodebuffer" | Buffer | Node.js file I/O |
| "uint8array" | Uint8Array | Cross-platform raw bytes |
| "blob" | Blob | Browser downloads |
| "base64" | string | Data URLs, API payloads |
| "arraybuffer" | ArrayBuffer | Memory handling |
Sync Variant
Use generateWorkbookSync() for synchronous generation — blocks the main thread but avoids async overhead:
import { generateWorkbookSync } from "@office-open/xlsx";
const buffer = generateWorkbookSync({
/* options */
});
Streaming
Use generateWorkbookStream() for large documents — returns a ReadableStream without buffering the entire file:
import { generateWorkbookStream } from "@office-open/xlsx";
import { createWriteStream } from "node:fs";
import { Readable } from "node:stream";
const stream = generateWorkbookStream({
/* options */
});
Readable.fromWeb(stream).pipe(createWriteStream("output.xlsx"));
Compression Options
All functions accept compression control. Default matches Microsoft Office: XML uses DEFLATE level 1 (SuperFast); media is split by type — already-compressed formats (PNG/JPEG/GIF) use STORE, the rest (EMF/WMF/BMP/TIFF/…) use DEFLATE level 6 / Normal.
// Default (MS Office)
await generateWorkbook(opts);
// Maximum XML compression
await generateWorkbook(opts, { compression: { xml: 9 } });
// No compression
await generateWorkbook(opts, { compression: { xml: 0 } });
| Option | Type | Default | Description |
|---|---|---|---|
| compression.xml | number | 1 | DEFLATE level 0–9 |
| compression.media | number | 6 | compressible media only; PNG/JPEG/GIF always STORE |
Browser Download Example
const blob = await generateWorkbook(opts, { type: "blob" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "output.xlsx";
a.click();
URL.revokeObjectURL(url);