Working with Excel in JavaScript (Node.js): Library Overview and Practical Examples
Despite PythonLib being dedicated to the Python language, modern web development often requires processing Excel spreadsheets on the server side using Node.js. JavaScript (Node.js) is a powerful environment for building server-side applications, and working with spreadsheets is one of the in-demand tasks. In this article, we will review three main libraries for working with Excel in Node.js: node-xlsx (xlsx), exceljs, and excel4node, and provide examples of their usage.
Why Work with Excel in Node.js?
On the server side, it is often necessary to:
- Generate reports in XLSX format for users to download;
- Import data from uploaded Excel files into a database;
- Automatically process price lists, accounting tables, and other documents;
- Create complex reports with formatting, charts, and styles.
Node.js handles these tasks excellently, providing several specialized libraries.
The xlsx Library (SheetJS)
SheetJS (the xlsx package) is the most popular and mature library for working with Excel in JavaScript. It supports reading and writing many formats: XLSX, XLS, CSV, ODS, and others. The main advantage is simplicity and speed.
Installation
npm install xlsxExample: Creating and Saving an Excel File
const XLSX = require('xlsx');
// Create a new workbookconst workbook = XLSX.utils.book_new();
// Data for the tableconst data = [ ['Name', 'Age', 'City'], ['Anna', 28, 'Moscow'], ['Ivan', 35, 'Saint Petersburg'], ['Maria', 22, 'Kazan']];
// Convert array to a worksheetconst worksheet = XLSX.utils.aoa_to_sheet(data);
// Add the worksheet to the workbookXLSX.utils.book_append_sheet(workbook, worksheet, 'Users');
// Write the file to diskXLSX.writeFile(workbook, 'users.xlsx');console.log('File users.xlsx created!');Example: Reading an Existing File
const XLSX = require('xlsx');
const workbook = XLSX.readFile('users.xlsx');const sheetName = workbook.SheetNames[0]; // Get the first sheetconst worksheet = workbook.Sheets[sheetName];
// Convert the worksheet to an array of objectsconst jsonData = XLSX.utils.sheet_to_json(worksheet);console.log(jsonData);The exceljs Library
ExcelJS is a more modern and feature-rich library. It provides extensive formatting capabilities: cell styles, fonts, colors, cell merging, conditional formatting, working with images and charts. It supports streams for working with large files.
Installation
npm install exceljsExample: Creating a Formatted Report
const ExcelJS = require('exceljs');
async function createReport() { const workbook = new ExcelJS.Workbook(); const sheet = workbook.addWorksheet('Report');
// Define columns sheet.columns = [ { header: 'Product', key: 'product', width: 30 }, { header: 'Price', key: 'price', width: 15 }, { header: 'Quantity', key: 'quantity', width: 15 } ];
// Add data sheet.addRow({ product: 'Laptop', price: 75000, quantity: 10 }); sheet.addRow({ product: 'Mouse', price: 1500, quantity: 50 }); sheet.addRow({ product: 'Keyboard', price: 3500, quantity: 30 });
// Format the header const headerRow = sheet.getRow(1); headerRow.font = { bold: true, size: 12, color: { argb: 'FFFFFFFF' } }; headerRow.fill = { type: 'pattern', pattern: 'solid', fgColor: { argb: 'FF4472C4' } };
// Alignment headerRow.alignment = { vertical: 'middle', horizontal: 'ce