Stream API
XLSX.stream creates Node Readable streams from an already loaded worksheet.
It reduces the need to hold a complete output string, but does not stream input
parsing or avoid storing the worksheet. The namespace is absent from the mini
distribution.
Signatures
Section titled “Signatures”| Function | Parameters | Returns |
|---|---|---|
to_csv(worksheet, options?) |
Worksheet and CSV conversion options | Text-output Readable |
to_html(worksheet, options?) |
Worksheet and HTML conversion options | Text-output Readable |
to_json(worksheet, options?) |
Worksheet and JSON conversion options | Object-mode Readable, one row per chunk |
set_readable(Readable) |
A compatible Readable constructor |
No result to consume |
Node registers its Readable constructor automatically. Use set_readable
only when supplying that constructor yourself.
CSV options match utils.sheet_to_csv:
| Option | Meaning |
|---|---|
FS |
Field separator, default comma |
RS |
Record separator, default newline |
strip |
Remove trailing separators |
blankrows |
Keep blank rows, default true |
skipHidden |
Skip hidden rows and columns |
forceQuotes |
Quote every field |
rawNumbers |
Use raw numeric values |
dateNF |
Date format override |
CSV streams begin with a Unicode byte-order mark (\uFEFF). The ordinary
sheet_to_csv string does not include this mark.
Consume a stream with a pipe, pipeline, or async iteration. Catch stream errors
through the consumption method, not just a try around construction.
const XLSX = require("@agent-sheet/wasm");const assert = require("node:assert/strict");(async () => { const sheet = XLSX.utils.aoa_to_sheet([["Item", "Qty"], ["Pens", 3]]); let output = ""; for await (const chunk of XLSX.stream.to_csv(sheet)) output += chunk.toString(); assert.equal(output, "\uFEFF" + XLSX.utils.sheet_to_csv(sheet));})().catch((error) => { throw error; });Options are id, editable, header, footer, and gridcolor, matching
utils.sheet_to_html. Without !ref, a range is inferred 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.
const XLSX = require("@agent-sheet/wasm");const assert = require("node:assert/strict");(async () => { const sheet = XLSX.utils.aoa_to_sheet([["Hello"]]); let html = ""; for await (const chunk of XLSX.stream.to_html(sheet)) html += chunk.toString(); assert.ok(html.includes("Hello")); assert.ok(html.includes("<table"));})().catch((error) => { throw error; });JSON rows
Section titled “JSON rows”to_json returns row objects, not a serialized JSON array. Use
header: 1 for array rows, header: "A" for column-letter keys, or an explicit
header array for chosen keys. Other options include range, defval, raw,
rawNumbers, blankrows, dateNF, skipHidden, and UTC.
const XLSX = require("@agent-sheet/wasm");const assert = require("node:assert/strict");(async () => { const sheet = XLSX.utils.aoa_to_sheet([["Name", "Score"], ["Ari", 8], ["Bo", 6]]); const rows = []; for await (const row of XLSX.stream.to_json(sheet)) rows.push(row); assert.deepEqual(rows, [{ Name: "Ari", Score: 8 }, { Name: "Bo", Score: 6 }]);})().catch((error) => { throw error; });Do not pipe an object-mode JSON stream directly to a byte-oriented file stream. Transform each row into text first, or consume the objects in your application.
See Streams for file output and Converting sheets for conversion behavior.