Reading files
Use read for data you already have in memory. Use readFile to read a file in Node.
const XLSX = require("@agent-sheet/wasm");
// Make a small file to read.const src = XLSX.utils.book_new();XLSX.utils.book_append_sheet(src, XLSX.utils.aoa_to_sheet([["a", "b"], [1, 2], [3, 4]]), "First");XLSX.utils.book_append_sheet(src, XLSX.utils.aoa_to_sheet([["x"], [9]]), "Second");XLSX.writeFile(src, "book.xlsx");
const wb = XLSX.readFile("book.xlsx");console.log(wb.SheetNames);console.log(wb.Sheets.First["!ref"]);console.log(wb.Sheets.First.B2);[ 'First', 'Second' ]A1:B3{ t: 'n', v: 2, w: '2' }The format is detected from the content, not from the file name.
Input types
Section titled “Input types”Set type to tell read how to interpret the data you pass.
type |
Input |
|---|---|
"string" |
text, for formats such as CSV and HTML |
"base64" |
base64 text |
"binary" |
a binary string, one character per byte |
"buffer" |
a Node Buffer |
"array" |
a Uint8Array, an ArrayBuffer or an array of bytes |
"file" |
a file path (Node only) |
When you leave type out, read guesses from the value you pass.
const XLSX = require("@agent-sheet/wasm");const fs = require("fs");
const src = XLSX.utils.book_new();XLSX.utils.book_append_sheet(src, XLSX.utils.aoa_to_sheet([[1]]), "S");XLSX.writeFile(src, "book.xlsx");
const data = fs.readFileSync("book.xlsx");console.log(XLSX.read(data).SheetNames);console.log(XLSX.read(data.toString("base64"), { type: "base64" }).SheetNames);console.log(XLSX.read(new Uint8Array(data), { type: "array" }).SheetNames);console.log(XLSX.read(data.toString("binary"), { type: "binary" }).SheetNames);console.log(XLSX.read("a,b\n1,2", { type: "string" }).SheetNames);[ 'S' ][ 'S' ][ 'S' ][ 'S' ][ 'Sheet1' ]Read only part of a file
Section titled “Read only part of a file”These options save time on large files.
const XLSX = require("@agent-sheet/wasm");
const src = XLSX.utils.book_new();XLSX.utils.book_append_sheet(src, XLSX.utils.aoa_to_sheet([["a", "b"], [1, 2], [3, 4]]), "First");XLSX.utils.book_append_sheet(src, XLSX.utils.aoa_to_sheet([["x"], [9]]), "Second");XLSX.writeFile(src, "book.xlsx");
// Only the sheet names:const names = XLSX.readFile("book.xlsx", { bookSheets: true });console.log(names.SheetNames, Object.keys(names));
// Only one sheet, by name or by index:console.log(Object.keys(XLSX.readFile("book.xlsx", { sheets: "Second" }).Sheets));console.log(Object.keys(XLSX.readFile("book.xlsx", { sheets: 1 }).Sheets));
// Only the first two rows of each sheet:console.log(XLSX.readFile("book.xlsx", { sheetRows: 2 }).Sheets.First["!ref"]);[ 'First', 'Second' ] [ 'SheetNames' ][ 'Second' ][ 'Second' ]A1:B2Dense mode
Section titled “Dense mode”By default a worksheet is an object with one property per cell (A1, B2, …). With dense: true
the cells are in a row array named !data. Dense mode uses less memory for big sheets.
const XLSX = require("@agent-sheet/wasm");
const src = XLSX.utils.book_new();XLSX.utils.book_append_sheet(src, XLSX.utils.aoa_to_sheet([["a", "b"], [1, 2]]), "First");XLSX.writeFile(src, "book.xlsx");
const wb = XLSX.readFile("book.xlsx", { dense: true });const ws = wb.Sheets.First;console.log(ws["!data"][1][0]);console.log(ws.A1);{ t: 'n', v: 1, w: '1' }undefinedMain read options
Section titled “Main read options”See Parsing options for the full list. The ones you use most:
| Option | Effect |
|---|---|
type |
how to interpret the input |
cellDates |
produce date cells (t: "d") instead of serial numbers |
cellStyles |
load styles and sheet presentation data |
cellNF |
keep the number format in z |
cellFormula |
keep formulas in f (default true) |
sheetStubs |
create blank cells for cells that only carry formatting |
raw |
keep delimited-text values as strings |
codepage |
text encoding of legacy files, see Codepages |
WTF |
throw on malformed content instead of skipping it |
Plain text formats
Section titled “Plain text formats”CSV and other text formats are parsed with the same call. Numbers and dates are detected unless you set raw: true.
const XLSX = require("@agent-sheet/wasm");const wb = XLSX.read("id,name\n1,Ann\n2,Bob", { type: "string" });console.log(XLSX.utils.sheet_to_json(wb.Sheets.Sheet1));
const raw = XLSX.read("id\n007", { type: "string", raw: true });console.log(raw.Sheets.Sheet1.A2.v, typeof raw.Sheets.Sheet1.A2.v);[ { id: 1, name: 'Ann' }, { id: 2, name: 'Bob' } ]007 string