VOOZH about

URL: https://dev.to/vanloctech1/read-and-write-excel-files-in-bun-with-bun-excel-2a9h

⇱ Read and Write Excel Files in Bun with bun-excel - DEV Community


If you are building a Bun app and need to export Excel or CSV files, bun-excel provides a Bun-native API for common spreadsheet workflows.

It supports .xlsx, CSV, styles, formulas, hyperlinks, streaming exports, and Bun file targets such as local files, Bun.file(), and S3File.

Install

bun add bun-excel

Write an Excel file

import { writeExcel, type Workbook } from "bun-excel";

const workbook: Workbook = {
 worksheets: [
 {
 name: "Report",
 columns: [{ width: 20 }, { width: 12 }],
 rows: [
 {
 cells: [
 { value: "Name", style: { font: { bold: true } } },
 { value: "Score", style: { font: { bold: true } } },
 ],
 },
 { cells: [{ value: "Alice" }, { value: 95 }] },
 { cells: [{ value: "Bob" }, { value: 87 }] },
 ],
 },
 ],
};

await writeExcel("report.xlsx", workbook);

Read it back

import { readExcel } from "bun-excel";

const workbook = await readExcel("report.xlsx");

for (const sheet of workbook.worksheets) {
 for (const row of sheet.rows) {
 console.log(row.cells.map((cell) => cell.value).join(" | "));
 }
}

Stream large exports

For large reports, you can write rows incrementally:

import { createExcelStream } from "bun-excel";

const stream = createExcelStream("large-report.xlsx", {
 sheetName: "Data",
 columns: [{ width: 10 }, { width: 30 }, { width: 15 }],
});

stream.writeRow(["ID", "Product", "Price"]);

for (let i = 0; i < 100_000; i++) {
 stream.writeRow([i + 1, `Product ${i + 1}`, Math.random() * 1000]);
}

await stream.end();

CSV support

import { writeCSV, readCSV } from "bun-excel";

await writeCSV("users.csv", [
 ["Name", "Age"],
 ["Alice", 28],
 ["Bob", 31],
]);

const rows = await readCSV("users.csv");

When to use it

Use bun-excel when you need spreadsheet exports in a Bun backend, especially for reports, admin tools, dashboards, or data pipelines.

Package: https://www.npmjs.com/package/bun-excel

GitHub: https://github.com/vanloctech/bun-excel