Working with Excel in JavaScript (Node.js): Libraries and Tools

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with Excel in JavaScript (Node.js)



Modern web development often requires programmatic interaction with spreadsheets. Exporting reports, importing data from accounting systems, and processing large arrays of information all make working with Excel in Node.js a sought-after task. In this article, we will look at the three most popular libraries for working with the XLSX format: xlsx, exceljs, and excel4node. You will learn how to install each of them, create, edit, and read Excel files.



The xlsx Library (SheetJS)



xlsx is the most popular and lightweight library for working with spreadsheets in Node.js. It supports not only XLSX but also XLS, CSV, ODS, and other formats. The main advantage is the simplicity of the API and high performance.



Installation:

npm install xlsx


Example: Creating and writing an Excel file

const XLSX = require('xlsx');

// Create a worksheet with dataconst data = [ { Name: 'Anna', Age: 28, City: 'Moscow' }, { Name: 'Ivan', Age: 35, City: 'Saint Petersburg' }, { Name: 'Olga', Age: 22, City: 'Kazan' }];

const worksheet = XLSX.utils.json_to_sheet(data);const workbook = XLSX.utils.book_new();XLSX.utils.book_append_sheet(workbook, worksheet, 'Employees');

// Save the fileXLSX.writeFile(workbook, 'employees.xlsx');console.log('File employees.xlsx created!');


Example: Reading an existing file

const workbook = XLSX.readFile('employees.xlsx');const sheetName = workbook.SheetNames[0];const worksheet = workbook.Sheets[sheetName];const jsonData = XLSX.utils.sheet_to_json(worksheet);

console.log('Data from file:', jsonData);


Pros:

  • Minimal size (about 50 KB compressed)
  • Support for many formats (XLSX, XLS, CSV, ODS)
  • Simple API for JSON ↔ spreadsheet conversion


Cons:

  • Limited formatting capabilities (styles, fonts, colors)
  • Difficulties with merged cells and complex formulas


The exceljs Library



exceljs is an advanced library for working with XLSX files. It provides full control over formatting: you can manage fonts, colors, borders, merged cells, conditional formatting, and even add images.



Installation:

npm install exceljs


Example: Creating a formatted report

const ExcelJS = require('exceljs');

async function createReport() { const workbook = new ExcelJS.Workbook(); const worksheet = workbook.addWorksheet('Report');

// Define columns worksheet.columns = [ { header: 'Product', key: 'product', width: 20 }, { header: 'Quantity', key: 'quantity', width: 15 }, { header: 'Price', key: 'price', width: 15 } ];

// Add data worksheet.addRow({ product: 'Laptop', quantity: 10, price: 75000 }); worksheet.addRow({ product: 'Mouse', quantity: 50, price: 1500 });

// Header formatting const headerRow = worksheet.getRow(1); headerRow.font = { bold: true, color: { argb: 'FFFFFFFF' } }; headerRow.fill = { type: 'pattern', pattern: 'solid', fgColor: { argb: 'FF4472C4' } };

// Price cell formatting const priceColumn = worksheet.getColumn(3); priceColumn.numFmt = '#,##0.00';

// Save await workbook.xlsx.writeFile('report.xlsx'); console.log('Report created with formatting!');}

createReport();


Example: Reading and modifying a file

const workbook = new ExcelJS.Workbook();await workbook.xlsx.readFile('report.xlsx');const worksheet = w                    

Recommendations