Working with Excel in JavaScript (Node.js): Overview of Libraries and Code Examples
Despite PythonLib being dedicated to the Python language, working with Excel spreadsheets is often required in the Node.js environment as well. JavaScript (especially on the server side) is actively used for report automation, data import from CRM systems, and price list generation. In this article, we will review three main libraries for working with Excel in Node.js, their capabilities, and provide practical code examples.
Why Node.js for Excel?
Node.js is an ideal choice for microservices and scripts that process large volumes of data. Excel libraries allow you to:
- Read existing
.xlsxand.xlsfiles. - Create new spreadsheets from scratch.
- Format cells (colors, fonts, borders).
- Work with formulas, styles, and merged cells.
- Export data to CSV.
Top 3 Libraries for Excel in Node.js
1. ExcelJS — The Most Feature-Rich
ExcelJS is a powerful library with full support for the xlsx format. It allows working with streams, which is critical for large files. Supports styles, conditional formatting, charts, and sheet protection.
Installation:
npm install exceljsExample 1: Reading an Existing File
const ExcelJS = require('exceljs');
async function readExcel(filePath) { const workbook = new ExcelJS.Workbook(); await workbook.xlsx.readFile(filePath); const worksheet = workbook.getWorksheet(1); // First sheet worksheet.eachRow((row, rowNumber) => { console.log(`Row ${rowNumber}: ${row.values.join(', ')}`); });}
readExcel('data.xlsx');Example 2: Creating a File with Formatting
const ExcelJS = require('exceljs');
async function createStyledExcel() { const workbook = new ExcelJS.Workbook(); const sheet = workbook.addWorksheet('Report'); // Header with bold font and fill const headerRow = sheet.getRow(1); headerRow.values = ['Name', 'Age', 'City']; headerRow.font = { bold: true, size: 12 }; headerRow.fill = { type: 'pattern', pattern: 'solid', fgColor: { argb: 'FFFF00' } // Yellow background }; // Data sheet.addRow(['Anna', 28, 'Moscow']); sheet.addRow(['Ivan', 35, 'Saint Petersburg']); // Column widths sheet.getColumn(1).width = 15; sheet.getColumn(2).width = 10; await workbook.xlsx.writeFile('output.xlsx'); console.log('File created!');}
createStyledExcel();2. xlsx (SheetJS) — Lightweight and Fast
The xlsx library (also known as SheetJS) is a workhorse for simple operations. It does not support advanced formatting but handles reading/writing data well. Supports xlsx, xls, csv, ods formats.
Installation:
npm install xlsxExample 3: Reading and Converting to JSON
const XLSX = require('xlsx');
function readToJSON(filePath) { const workbook = XLSX.readFile(filePath); const sheetName = workbook.SheetNames[0]; const sheet = workbook.Sheets[sheetName]; // Convert sheet to an array of objects const data = XLSX.utils.sheet_to_json(sheet); console.log(data);}
readToJSON('data.xlsx');Example 4: Writing an Array of Data
const XLSX = require('xlsx');
function writeFromArray() { const data = [ { Name: 'Olga', Age: 30, City: 'Kazan' }, { Name: 'Pyotr', Age: 42, City: 'Yekaterinburg' } ]; const worksheet = XLSX.utils.json_to_sheet(data); const workbook = XLSX.utils.book_new(); XLSX.utils