Export
The Packer class converts a Workbook into various output formats. Every async method (toBuffer, toBlob, etc.) has a synchronous counterpart (toBufferSync, toBlobSync, etc.).
Async methods use Web Workers for non-blocking ZIP compression; sync methods block the main thread but avoid Worker overhead.
toBuffer
Returns a Buffer. Best for Node.js file I/O.
import { Packer } from "@office-open/xlsx";
import { writeFileSync } from "node:fs";
// Async (non-blocking, uses Web Workers)
const buffer = await Packer.toBuffer(wb);
writeFileSync("output.xlsx", buffer);
// Sync (blocking, no Worker overhead)
const buffer2 = Packer.toBufferSync(wb);
writeFileSync("output-sync.xlsx", buffer2);
toBlob
Returns a Blob. Best for browser environments.
// Async
const blob = await Packer.toBlob(wb);
// Sync
const blob2 = Packer.toBlobSync(wb);
// Trigger download
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "output.xlsx";
a.click();
URL.revokeObjectURL(url);
toBase64String
Returns a Base64-encoded string. Useful for data URLs or API payloads.
// Async
const base64 = await Packer.toBase64String(wb);
// Sync
const base642 = Packer.toBase64StringSync(wb);
const dataUrl = `data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,${base64}`;
toBytes
Returns a Uint8Array. Useful when you need raw bytes without Node.js Buffer.
// Async
const bytes = await Packer.toBytes(wb);
// Sync
const bytes2 = Packer.toBytesSync(wb);
toArrayBuffer
Returns an ArrayBuffer. Useful for browser or cross-platform code.
// Async
const ab = await Packer.toArrayBuffer(wb);
// Sync
const ab2 = Packer.toArrayBufferSync(wb);
toString
Returns a string representation. Useful for debugging.
// Async
const str = await Packer.toString(wb);
// Sync
const str2 = Packer.toStringSync(wb);
toStream
Returns a ReadableStream<Uint8Array>. Useful for streaming large workbooks. Works in both Node.js and browsers. This method is always synchronous.
import { createWriteStream } from "node:fs";
import { Readable } from "node:stream";
const stream = Packer.toStream(wb);
Readable.fromWeb(stream).pipe(createWriteStream("output.xlsx"));
compile
Returns the raw Zippable file map without compressing. Useful when you need to customize the ZIP process.
const files = Packer.compile(wb);
// files is a Zippable — can be passed to custom zip functions
Summary
| Method | Async Return | Sync Return | Environment | Use Case |
|---|---|---|---|---|
toBuffer | Promise<Buffer> | Buffer | Node.js | File I/O |
toBlob | Promise<Blob> | Blob | Browser | Downloads |
toBytes | Promise<Uint8Array> | Uint8Array | Any | Raw bytes |
toBase64String | Promise<string> | string | Any | Data URLs, APIs |
toString | Promise<string> | string | Any | Debugging |
toArrayBuffer | Promise<ArrayBuffer> | ArrayBuffer | Any | Memory handling |
toStream | ReadableStream<Uint8Array> | — | Both | Large files |
compile | — | Zippable | Any | Custom ZIP |