Your cart is currently empty!
How to Read and Write Excel Files in Node.js with the SheetJS (xlsx) Library
To read and write Excel files in Node.js, the most popular library is xlsx
(from the SheetJS project). It supports .xlsx
, .xls
, and .csv
formats and is easy to use.
✅ Step-by-Step Guide to Read & Write Excel Files in Node.js
📦 Step 1: Install the xlsx
Package
Run the following command:
npm install xlsx
📘 Example: Writing to an Excel File
const XLSX = require('xlsx');
// Sample data
const data = [
["Name", "Age", "City"],
["John", 30, "New York"],
["Alice", 25, "London"],
["Bob", 35, "Paris"]
];
// Create a new workbook and worksheet
const worksheet = XLSX.utils.aoa_to_sheet(data);
const workbook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(workbook, worksheet, "Sheet1");
// Write to file
XLSX.writeFile(workbook, "output.xlsx");
console.log("Excel file written successfully!");
📘 Example: Reading from an Excel File
const XLSX = require('xlsx');
// Read the Excel file
const workbook = XLSX.readFile('output.xlsx');
// Get the first sheet
const sheetName = workbook.SheetNames[0];
const worksheet = workbook.Sheets[sheetName];
// Convert to JSON
const jsonData = XLSX.utils.sheet_to_json(worksheet);
console.log("Excel file data:");
console.log(jsonData);
🔁 Input/Output Summary
Action | Method |
---|---|
Read file | XLSX.readFile(filename) |
Write file | XLSX.writeFile(workbook, filename) |
Create sheet | XLSX.utils.aoa_to_sheet(data) |
Convert to JSON | XLSX.utils.sheet_to_json(sheet) |
📝 Notes
- AOA (Array of Arrays): Best for simple table-like data.
- sheet_to_json() gives you an array of objects for easy processing.
Absolutely! Here’s an explanation of where and why you might need to read and write Excel files in Node.js, followed by real-world use cases.
📌 Where Is Excel File Handling Required in Node.js?
Working with Excel files in a Node.js backend or application is useful when your system needs to:
✅ 1. Export Reports or Data to Excel
When users want to download reports, sales data, invoices, or analytics in Excel format.
Example:
- A web dashboard that allows exporting user activity logs as
.xlsx
- An admin panel that exports inventory or orders
✅ 2. Read Uploaded Excel Files
When users upload Excel files containing data to be processed, imported, or validated.
Example:
- HR uploads employee records in Excel
- Accountants upload tax or ledger entries in
.xlsx
- Students upload answer sheets or marksheets
✅ 3. Data Migration
Reading old Excel files and importing them into a new system or database.
Example:
- Migrating legacy data from Excel to MongoDB or MySQL
- Uploading master data like product catalogs or customer lists
✅ 4. Automation and Scheduled Tasks
Scheduled scripts that read Excel templates, process them, and generate output.
Example:
- Nightly script that reads a
.xlsx
report and emails a summary - Cron job that reads monthly sales targets from Excel and stores them in the database
✅ 5. Online Formatted Excel Generation
When users fill out a form and get a custom Excel report/download with formatting.
Example:
- Loan EMI calculators generating
.xlsx
reports - Quotation generators for e-commerce or B2B services
💼 Real-World Use Cases
Use Case | Description |
---|---|
School Management System | Import student data, export mark sheets |
E-commerce Admin Panel | Export order lists or product catalogs |
Finance / Payroll App | Generate payslips, read salary structures |
Inventory Management | Upload or download stock records |
CRM Systems | Export contacts or leads |
🔧 Why Use Node.js for Excel?
- Fast, scalable backend
- Easily integrates with frontends (React, Angular, etc.)
- Works well with REST APIs and file uploads
- Supports real-time and batch processing
On sale products
-
Excel Course in Hindi: Basic to Advanced Level
Original price was: ₹2,299.00.₹2,249.00Current price is: ₹2,249.00. -
Excel Training Program: Fundamentals to Advanced Techniques : Classroom/ Live Classes
Original price was: ₹6,000.00.₹4,000.00Current price is: ₹4,000.00. -
Excel VBA/Macro Masterclass: Automate Excel, Boost Productivity : Classroom /Live Class Training
Original price was: ₹8,500.00.₹6,500.00Current price is: ₹6,500.00. -
Gmail Mastery Training in Hindi: Unlocking Advanced Email Management Technique
Original price was: ₹899.00.₹849.00Current price is: ₹849.00. -
Gmail Mastery: Advanced Training for Efficient Email Management
Original price was: ₹899.00.₹849.00Current price is: ₹849.00.