Streams
XLSX.stream turns a worksheet into a Node Readable stream. Use it to pipe a big sheet to a file or a
network response without building the whole output string first.
| Function | Stream output |
|---|---|
stream.to_csv(ws, opts) |
CSV text; options as for utils.sheet_to_csv |
stream.to_html(ws, opts) |
an HTML document |
stream.to_json(ws, opts) |
object mode: one row object per chunk; options as for utils.sheet_to_json |
stream.set_readable(Readable) |
give the library a Readable constructor (Node does this for you) |
const XLSX = require("@agent-sheet/wasm");const fs = require("fs");const { pipeline } = require("node:stream/promises");
async function main() { const ws = XLSX.utils.aoa_to_sheet([["a", "b"], [1, 2], [3, 4]]); await pipeline(XLSX.stream.to_csv(ws), fs.createWriteStream("out.csv")); console.log(JSON.stringify(fs.readFileSync("out.csv", "utf8")));
const rows = []; for await (const row of XLSX.stream.to_json(ws)) rows.push(row); console.log(rows);}main().catch((error) => { console.error(error); process.exitCode = 1; });"a,b\n1,2\n3,4"[ { a: 1, b: 2 }, { a: 3, b: 4 } ]The streams read the worksheet object as they run. The library does not read a file in pieces. To work with a huge file,
read it with readFile first (see Large files and performance), then stream the output.
In the browser
Section titled “In the browser”fs and stream are not available in browsers. The stream functions need a Readable class. In a
bundler, either do not use them or call XLSX.stream.set_readable(Readable) with a class from a
stream package that you bundle.
Empty sheets
Section titled “Empty sheets”stream.to_html without !ref infers a range from existing cells; an empty worksheet produces a table with a blank A1 cell. The original raises an unhandled stream error that can terminate the process. See Known differences.