Skip to content

Latest commit

 

History

History

javascript-excel

README.md

javascript-excel

Read and write .xlsx and .csv files in browsers, Web Workers and Node.js. TypeScript, no runtime dependencies, tree-shakable entry points.

2.0 is a from-scratch rewrite. It shares nothing with 1.x ([email protected]); see the 2.0.0 changelog for the breaking changes.

npm install javascript-excel

Try it in the browser: the playground writes, reads and previews workbooks with the published package, and shows the code for what you build.

entry for
javascript-excel the workbook model, the .xlsx writer, dataset builders, dates, addresses, errors
javascript-excel/read the .xlsx reader
javascript-excel/csv CSV parse and serialize
javascript-excel/dom HTML <table> to sheet, download()
javascript-excel/worker run big jobs in a Web Worker
javascript-excel/format show values as Excel does: number formats, dates, colours
javascript-excel/render the render model for a sheet viewer: geometry, styles, merges, navigation, copy
javascript-excel/import a user's .xlsx or .csv to { columns, data }: header detection, types, locales, column mapping

Each entry pulls in only what it needs: a writer-only app never ships the reader, the CSV code or the DOM code.

Write an .xlsx

import { toBlob, toUint8Array, type Workbook } from "javascript-excel";

const workbook: Workbook = {
  sheets: [
    {
      name: "Report",
      rows: [
        [{ value: "Item", style: { font: { bold: true }, fill: "DDEBF7" } }, "Qty", "Price"],
        ["Pens", 10, { value: 1.5, style: { numFmt: "#,##0.00" } }],
        ["Total", { formula: "SUM(B2:B2)", value: 10 }, new Date()],
      ],
      view: { freeze: { rows: 1 } },
      colProps: [{ width: "auto" }],
    },
  ],
};

const bytes: Uint8Array = await toUint8Array(workbook); // Node: writeFileSync("report.xlsx", bytes)
const blob = await toBlob(workbook); // browser: download it, upload it, ...

A workbook is plain data: sheets with rows[r][c], where a cell is a value (string, number, boolean, Date, { error }, { richText }) or a { value, formula, style, hyperlink } object.

From a dataset ({ columns, data })

import { fromDataset, toDataset, toUint8Array } from "javascript-excel";

const sheet = fromDataset(
  {
    columns: [
      { title: "Name", key: "name", width: "auto" },
      { title: "Sales", children: [{ title: "Q1", path: "sales.q1", numFmt: "#,##0.00", total: "sum" }] },
    ],
    data: [{ name: "Ada", sales: { q1: 1200.5 } }],
  },
  { freezeHeader: true, autoFilter: true, totals: { label: "Total" } },
);
const bytes = await toUint8Array({ sheets: [sheet] });

// ...and back from a sheet that was read: { columns, data } with one object per row.
const { columns, data } = toDataset(sheet);

Read an .xlsx

import { read } from "javascript-excel/read";

declare const file: Blob; // a File from <input type="file">, a Uint8Array or an ArrayBuffer

const { workbook, warnings } = await read(file);
for (const sheet of workbook.sheets) {
  console.log(sheet.name, sheet.dimension, sheet.rows[0]?.map((cell) => cell?.value));
}

// Opt in to the text stored for number and ISO-date cells, e.g. to keep "50.30" as written.
const exact = await read(file, { rawText: true });
const cell = exact.workbook.sheets[0]?.rows[0]?.[0];
console.log(cell?.type, cell?.value, cell?.rawText); // "number" 50.3 "50.30"

Damage the reader can recover from comes back as warnings. isDataLossWarning(w) tells the ones where data was lost (a cell skipped, rows cut, a part missing, comments or charts not read; partKind names the part, including ActiveX controls, embedded files, customUI, web add-ins, parts no relationship reaches and relationships to targets outside the file such as linked pictures, wherever they sit in the package) from informational ones; the warning cap (limits.maxWarnings, default 1,000) drops only informational warnings, so filtering warnings with it is enough to refuse a damaged file.

To accept or refuse an upload without reading it into the model, inspect(file, { limits, signal }) checks it part by part without building cells: it refuses every file read refuses under the same limits, with the same error, and returns the format, the worksheets with their cell counts, and the part kinds read reports (partKinds: VBA, external links, OLE objects, …). It also inflates the parts read never opens, searches the bytes of every one of them for <!DOCTYPE (ASCII-compatible, UTF-16 and UTF-32 forms, whatever the part's name; a declaration inside an XML comment or in an embedded file is refused too), and refuses XML parts whose byte order mark, first bytes or complete declaration show an encoding other than UTF-8 or UTF-16, or whose declaration is malformed or not at the very start. Set limits.maxItems (for example 100,000): it bounds the styles and defined names inspect still holds, and the shared-formula masters and reference records it keeps. See docs/api.md §5.2.1.

CSV

import { parseCsv, readCsv, toCsv, toCsvBlob } from "javascript-excel/csv";

const text = toCsv([["name", "amount"], ["Ada", 1250.5], ["=1+1", -5]]); // "=1+1" is written as "'=1+1"
const blob = toCsvBlob([["name"], ["Ada"]]); // UTF-8 with a BOM, so Excel opens it correctly

const { rows, delimiter } = parseCsv("name;amount\nAda;1250,50\n"); // detects ";"
const amounts = parseCsv("Betrag\n1,5\n2,5\n", { delimiterCandidates: [";", ",", "\t"] }); // one column: ";" preferred, "|" never tried
declare const upload: Blob;
const typed = await readCsv(upload, { inferTypes: true }); // numbers, TRUE/FALSE and ISO dates typed

HTML tables and downloads

import { toBlob } from "javascript-excel";
import { download, fromTable } from "javascript-excel/dom";

const table = document.querySelector("table")!;
const sheet = fromTable(table, { styles: "computed", freezeHeader: true, autoWidth: true });
download(await toBlob({ sheets: [sheet] }), "report.xlsx");

Number formats

import { formatCell, formatValue } from "javascript-excel/format";
import { read } from "javascript-excel/read";

formatValue(1234.5, "#,##0.00"); // { text: "1,234.50", align: "right" }
formatValue(-12.5, "0.0;[Red]-0.0").color; // "FFFF0000"
formatValue(45306, "dddd, mmmm d, yyyy", { locale: "de-DE" }).text; // "Montag, Januar 15, 2024"

declare const file: Blob;
const { workbook } = await read(file, { dates: "serial" }); // serials: formatting does not depend on the time zone
const shown = formatCell(workbook.sheets[0]?.rows[0]?.[0], { locale: navigator.language, date1904: workbook.date1904 });

Importing a user's file

importDataset finds the sheet and header row, flattens header groups, infers column types (with an explicit number and date locale), maps columns to your fields, and reports problems per row. Formulas are never evaluated, and the read limits stay on.

import { importDataset } from "javascript-excel/import";

declare const file: File; // from <input type="file"> or a drop
const { columns, data, issues, meta } = await importDataset(file, {
  locale: { decimal: ",", group: "." }, // 1.234,56
  schema: { fields: { email: { aliases: ["E-mail"], required: true }, joined: { type: "date" } } },
  maxRows: 100_000,
});
console.log(meta.header, columns.map((c) => `${c.key}: ${c.type}`), data.length, issues);

A field typed 'day' (in types or the schema) holds a calendar day: a date-time such as 31.10.2026 08:15 becomes 00:00 of 2026-10-31 in dateZone, and toCsv writes it as 2026-10-31. 'date' keeps the time of day.

In a Web Worker

// excel.worker.ts (the app writes these two lines):
//   import { exposeWorker } from "javascript-excel/worker";
//   exposeWorker();
import type { Workbook } from "javascript-excel";
import { createWorkerClient } from "javascript-excel/worker";

declare const workbook: Workbook;
const excel = createWorkerClient(new Worker(new URL("./excel.worker.ts", import.meta.url), { type: "module" }));
const controller = new AbortController();
const bytes = await excel.write(workbook, { signal: controller.signal, onProgress: (p) => console.log(p.phase, p.ratio) });

What ships

  • Writer: strings, numbers, booleans, dates (1900 and 1904 systems), errors, rich text, formulas with cached values and array formulas, and styled empty cells.
    • Styles: the full style model (fonts, pattern and gradient fills, all border styles with a colour per side, alignment, protection, built-in and custom number formats), deduplicated.
    • Sheet features: column widths (characters, pixels or "auto"), row heights, hidden rows and columns, outline levels, merges, autofilter, freeze panes, right-to-left sheets, zoom, tab colours, hyperlinks (external URLs limited to http, https and mailto by default), defined names and print setup.
    • Workbook: document properties.
    • Validation: input that can't be written faithfully throws a typed ValidationError.
  • Reader: .xlsx, .xlsm, .xltx and .xltm, including ISO 29500 Strict files.
    • Reads values, shared, inline and rich strings, and formula text with cached values (shared formulas expanded).
    • Styles come back as shared frozen objects. Merges, column widths and row heights, views and panes, print setup, hyperlinks, defined names and document properties are read too.
    • Untrusted files: bounded by limits on size, zip entries, compression ratio, XML depth and token length, sheets, cells, sheet width (maxColumns; set it low, for example 256, for untrusted files), shared strings and the length of one cell's text; a shared formula is expanded only from a master within Excel's 8,192-character formula limit. A DOCTYPE is rejected, nothing is ever fetched or executed, and file content never becomes an object key.
    • Rejected formats: .xls, .xlsb, .ods and encrypted files throw UNSUPPORTED_FORMAT.
  • CSV: RFC 4180 parsing with delimiter detection, BOM and CRLF/LF/CR handling, lenient or strict modes, and optional type inference. readCsv decodes bytes in any TextDecoder encoding, or with encoding: 'auto' as UTF-8 and then Windows-1252, and for uploads can refuse binary input (rejectBinary), cap the input size (maxInputBytes) and check a file without keeping its rows (keepRows: false). Serializing gives you quoting, a BOM for Excel, ISO dates and a formula-injection guard, which is on by default.
  • Datasets: fromDataset and fromDatasets support key, path and function accessors, grouped headers, number formats, widths and a totals row. toDataset reads a sheet back into { columns, data }.
  • DOM: fromTable handles colspan/rowspan merges, data-* type hints and computed styles. download() needs no dependency, and the module is safe to import during SSR.
  • Worker: progress, warnings and AbortSignal work across the boundary, and results are transferred, not copied.
  • Import: importDataset turns a user's .xlsx or .csv into { columns, data }. It detects title and header rows, including multi-row headers. It infers column types all-or-nothing, parses numbers and dates in an explicit locale (1.234,56, 15/01/2024, and with twoDigitYearPivot two-digit years such as 31.10.26) and maps columns to your fields by name or alias. Problems are reported per row with their cell. Preview reads (preview) and maxRows stop reading early, and it also runs in the worker.
  • Not in 2.0: .xls/.xlsb/.ods, charts, images, comments, pivot tables, conditional formatting and formula evaluation. These are planned for later releases, not ruled out.

Runtime support

  • Node.js 22 and later.
  • Browsers with CompressionStream('deflate-raw'): Chrome and Edge 103+, Firefox 113+, Safari 16.4+.
  • TypeScript 5.0–7, with moduleResolution node16, nodenext or bundler. Types ship with the package.

Performance is measured in CI at 10k, 100k and 1M cells (for example, writing 1M numbers takes 1.3 s and reading them back 1.25 s on a GitHub Actions runner). The full table is in the repository README.

Documentation

Source and issues: https://github.com/securedeveloper/javascript-excel

License

MIT