ExcelJS for JavaScript (Node.js): Complete Guide with Examples

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

ExcelJS for JavaScript (Node.js): The Complete Guide to Creating and Parsing Excel Files

1. What is ExcelJS and Why Use It?

ExcelJS is a powerful and flexible JavaScript library for working with spreadsheet formats like Excel (XLSX, XLSM, CSV, and more). It runs both in Node.js and in the browser (via module bundlers like Webpack or Browserify). ExcelJS lets developers programmatically create, read, modify, and parse Excel files without needing Microsoft Excel installed.

Why use ExcelJS? In modern web development and business process automation, Excel remains one of the most popular data exchange formats. Reports, database exports, financial documents, price lists, statistics — all of these often need to be exported to Excel. ExcelJS provides a complete toolkit for these tasks: from creating a simple data table to complex reports with merged cells, styles, formulas, charts, and conditional formatting. It can also parse (read) existing Excel files, making it easy to automate data import from spreadsheets.

Key advantages of ExcelJS over alternatives (e.g., xlsx by SheetJS):

  • Style support: colors, fonts, borders, alignment, fills.
  • Formula handling: read and write formulas.
  • Conditional formatting: change cell appearance based on values.
  • Charts: create graphs and charts.
  • Images: insert images into worksheets.
  • Streaming: work with large files without loading everything into memory.
  • Active maintenance: regularly updated with a large community.

2. Installation

Installing ExcelJS in a Node.js project is done via npm. Open your terminal in the project root and run:

npm install exceljs

Once installed, import the library into your code:

const ExcelJS = require('exceljs');

If you're using ES modules (modern import/export syntax), make sure your package.json has "type": "module" set, or use the .mjs file extension:

import ExcelJS from 'exceljs';

3. Quick Start — Minimal Working Example

Let's create a simple Excel file with one table and save it to disk. This example demonstrates the core steps: creating a workbook, adding a worksheet, writing data, and saving.

const ExcelJS = require('exceljs');

async function createSimpleExcel() {
    // 1. Create a new workbook
    const workbook = new ExcelJS.Workbook();
    
    // 2. Add a worksheet named "My Data"
    const worksheet = workbook.addWorksheet('My Data');
    
    // 3. Define columns (headers)
    worksheet.columns = [
        { header: 'ID', key: 'id', width: 10 },
        { header: 'Name', key: 'name', width: 30 },
        { header: 'Age', key: 'age', width: 10 },
        { header: 'City', key: 'city', width: 20 }
    ];
    
    // 4. Add data rows
    worksheet.addRow({ id: 1, name: 'Anna Ivanova', age: 28, city: 'Moscow' });
    worksheet.addRow({ id: 2, name: 'Peter Petrov', age: 35, city: 'Saint Petersburg' });
    worksheet.addRow({ id: 3, name: 'Elena Sidorova', age: 22, city: 'Novosibirsk' });
    
    // 5. Save the workbook to a file
    await workbook.xlsx.writeFile('output.xlsx');
    console.log('File output.xlsx created successfully!');                    

Recommendations