Skip to content

Getting Started

Install xlsx-format and start reading and writing Excel files in minutes.

Installation

# npm
npm install xlsx-format

# pnpm
pnpm add xlsx-format

# yarn
yarn add xlsx-format

# bun
bun add xlsx-format

Quick Start

import { readFile, writeFile } from "node:fs/promises";
import { read, write, sheetToJson, jsonToSheet, createWorkbook } from "xlsx-format";

// Read an Excel file into JSON
const workbook = await read(await readFile("report.xlsx"));
const rows = sheetToJson(workbook.Sheets[workbook.SheetNames[0]]);

// Write JSON back to Excel
const sheet = jsonToSheet([
  { Name: "Alice", Revenue: 48000 },
  { Name: "Bob", Revenue: 52000 },
]);
await writeFile("output.xlsx", await write(createWorkbook(sheet, "Q4 Sales")));

Reading Files

Node.js

import { readFile } from "node:fs/promises";
import { read } from "xlsx-format";

// XLSX from a file
const workbook = await read(await readFile("spreadsheet.xlsx"));

// CSV / TSV / HTML from a file (pass as string)
const workbook = await read(await readFile("data.csv", "utf-8"), { type: "string" });

// From a Uint8Array or ArrayBuffer
const workbook = await read(buffer);

Browser

import { read } from "xlsx-format";

// From a File input
const buffer = await file.arrayBuffer();
const workbook = await read(buffer);

// From a fetch response
const response = await fetch("/data/report.xlsx");
const workbook = await read(new Uint8Array(await response.arrayBuffer()));

Writing Files

Node.js

import { writeFile } from "node:fs/promises";
import { write } from "xlsx-format";

// XLSX to a file
await writeFile("output.xlsx", await write(workbook));

// CSV to a file
await writeFile("output.csv", await write(workbook, { bookType: "csv", type: "string" }));

// HTML to a file
await writeFile("output.html", await write(workbook, { bookType: "html", type: "string" }));

Browser

import { write } from "xlsx-format";

// Trigger a download
const data = await write(workbook, { type: "array" });
const blob = new Blob([data], {
  type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
});

const link = document.createElement("a");
link.href = URL.createObjectURL(blob);
link.download = "output.xlsx";
link.click();

Converting Data

xlsx-format converts between sheets and common data formats in both directions.

import { sheetToJson, jsonToSheet, arrayToSheet, sheetToCsv, sheetToHtml, csvToSheet, htmlToSheet } from "xlsx-format";

// Sheet -> JSON objects (first row = headers)
const rows = sheetToJson(sheet);
// [{ Name: "Alice", Age: 30 }, { Name: "Bob", Age: 25 }]

// Sheet -> array of arrays
const arrays = sheetToJson(sheet, { header: 1 });
// [["Name", "Age"], ["Alice", 30], ["Bob", 25]]

// Sheet -> CSV / HTML
const csv = sheetToCsv(sheet);
const html = sheetToHtml(sheet);

// JSON / arrays / CSV / HTML -> Sheet
const sheet1 = jsonToSheet([{ Name: "Alice", Age: 30 }]);
const sheet2 = arrayToSheet([["Name", "Age"], ["Alice", 30]]);
const sheet3 = csvToSheet("Name,Age\nAlice,30");
const sheet4 = htmlToSheet("<table><tr><td>Name</td></tr></table>");

Workbook Helpers

import { createWorkbook, appendSheet, setSheetVisibility } from "xlsx-format";

const wb = createWorkbook(firstSheet, "Sheet1");
appendSheet(wb, secondSheet, "Sheet2");
setSheetVisibility(wb, 1, "hidden");

Cell Utilities

import { setCellNumberFormat, setCellHyperlink, addCellComment, setArrayFormula } from "xlsx-format";

setCellNumberFormat(sheet, "B2", "#,##0.00");
setCellHyperlink(sheet, "A1", "https://example.com");
addCellComment(sheet, "C3", "Check this value", "Alice");
setArrayFormula(sheet, "D1:D10", "=A1:A10*B1:B10");

Cell Addresses

import { decodeCell, encodeCell, decodeRange, encodeRange } from "xlsx-format";

decodeCell("B3");       // { r: 2, c: 1 }
encodeCell({ r: 2, c: 1 }); // "B3"
decodeRange("A1:C5");   // { s: { r: 0, c: 0 }, e: { r: 4, c: 2 } }
encodeRange(range);      // "A1:C5"

Runtime Support

Node.js >= 22 -- Use read() with fs.readFile() and write() with fs.writeFile() from node:fs/promises for file I/O.

Browsers -- read() and write() work in any modern browser with Uint8Array or ArrayBuffer. No Node.js APIs needed.

Next Steps