Skip to content

Writing files

Use write to get the file content in memory. Use writeFile to save it in Node. writeFile takes the format from the file extension. Both serialize synchronously. writeFileSync is an alias of writeFile. writeFileAsync serializes synchronously and then saves the file asynchronously through Node’s file system; it does not move the engine work off the calling thread.

const XLSX = require("@agent-sheet/wasm");
const wb = XLSX.read("a,b\n1,2\n3,4", { type: "string" });
XLSX.writeFile(wb, "out.xlsx"); // format from the extension
XLSX.writeFile(wb, "out.csv");
console.log(XLSX.readFile("out.xlsx").Sheets.Sheet1["!ref"]);
console.log(JSON.stringify(XLSX.write(wb, { type: "string", bookType: "csv" })));
Output
A1:B3
"a,b\n1,2\n3,4"

In a browser, writeFile starts a download. See Browsers and CSP.

type write returns
"string" text (text formats only)
"base64" base64 text
"binary" a binary string
"buffer" a Node Buffer
"array" an ArrayBuffer
const XLSX = require("@agent-sheet/wasm");
const wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, XLSX.utils.aoa_to_sheet([["a", 1]]), "S");
console.log(typeof XLSX.write(wb, { type: "base64" }));
console.log(typeof XLSX.write(wb, { type: "binary" }));
console.log(XLSX.write(wb, { type: "array" }).constructor.name);
console.log(Buffer.isBuffer(XLSX.write(wb, { type: "buffer" })));
Output
string
string
ArrayBuffer
true

Set bookType. The default is "xlsx". See Supported formats for every value.

const XLSX = require("@agent-sheet/wasm");
const wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, XLSX.utils.aoa_to_sheet([["a", "b"], [1, 2]]), "First");
XLSX.utils.book_append_sheet(wb, XLSX.utils.aoa_to_sheet([["x"], [9]]), "Second");
console.log(XLSX.write(wb, { type: "string", bookType: "csv" }));
console.log(JSON.stringify(XLSX.write(wb, { type: "string", bookType: "csv", sheet: "Second" })));
console.log(XLSX.write(wb, { type: "string", bookType: "csv", FS: ";", RS: "|" }));
Output
a,b
1,2
"x\n9"
a;b|1;2

Single-sheet formats such as CSV write the first sheet. Use the sheet option (a name or an index) to write another one.

ZIP-based formats (XLSX, XLSM, XLSB, ODS) are stored without compression by default. Set compression: true to deflate the entries:

const XLSX = require("@agent-sheet/wasm");
const rows = Array.from({ length: 200 }, (_, i) => ["row", i, "same text again and again"]);
const wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, XLSX.utils.aoa_to_sheet(rows), "S");
const plain = XLSX.write(wb, { type: "buffer", bookType: "xlsx" });
const small = XLSX.write(wb, { type: "buffer", bookType: "xlsx", compression: true });
console.log(small.length < plain.length);
Output
true

wb.Props holds the standard properties and wb.Custprops holds custom ones. Both are written with the workbook.

const XLSX = require("@agent-sheet/wasm");
const wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, XLSX.utils.aoa_to_sheet([[1]]), "S");
wb.Props = { Title: "Report", Author: "Ann" };
wb.Custprops = { Project: "x" };
const back = XLSX.read(XLSX.write(wb, { type: "buffer", bookType: "xlsx" }));
console.log(back.Props.Title, back.Props.Author, back.Custprops);
Output
Report Ann { Project: 'x' }

See Writing options for the full list.

Option Effect
type output representation
bookType output format
cellStyles export styles and presentation data
bookSST write a shared string table
compression deflate ZIP entries
sheet sheet for single-sheet formats
FS, RS separators for text output
Props document properties to write instead of wb.Props