1. Products
  2.   Spreadsheet
  3.   Node.js
  4.   Node-XLSX-Writer
 
  

Open Source Node.js Library to Write Data to Excel Files

A Dedicated Open Source Node.js Excel XLSX Library That Provides a Simple API for Generating .xlsx (Excel) Files and Write Data to Excel Files in Streaming Fashion.

What is Node-XLSX-Writer?

In the world of Node.js development, generating reports and exporting data are common requirements. While powerful libraries like exceljs exist, sometimes you need a solution that is laser-focused on one task; writing XLSX files quickly and efficiently, without the overhead of reading or manipulating existing files. Enter Node-XLSX-Writer, a lightweight and straightforward library designed precisely for this purpose. The library stands out for its simplicity and focused functionality. Unlike comprehensive Excel libraries that handle reading, writing, and manipulation, this module concentrates solely on writing XLSX files, making it lightweight, easy to learn, fast implementation and reliable.

Node-XLSX-Writer is a lightweight and straightforward library designed specifically for generating XLSX (Excel) files in Node.js applications. For tasks like generating CSV-like exports with the added benefits of Excel's formatting and multi-sheet support (through its straightforward API), it is an excellent choice that will keep your code clean and your application's memory footprint small. Created by Ruben Vermeersch and reverse-engineered from sample XLSX files, this module provides developers with a simple yet effective way to export data into Excel format without the complexity of larger Excel manipulation libraries. This makes it an ideal choice for applications that need to generate large reports from databases or APIs, as its streaming nature ensures low memory usage even with massive datasets.

Previous Next

Getting Started with Node-XLSX-Write

Before installing Node-XLSX-Writer, you need to have libzip development libraries installed on your system. The library depends on these native bindings to create zip files (XLSX files are essentially zipped XML). Please use the following commands for a successful installation.

Install Node-XLSX-Write via npm

npm install --save xlsx-writer

You can download the compiled shared library from GitHub repository and install it.

Excel XLSX Spreadsheet Creation in Node.js

The open source Node-XLSX-Writer library has included features for creating and handling Excel XLSX files inside Node.js applications. The library does not require Microsoft Excel or COM interop. It constructs the XLSX file (which is essentially a ZIP file of XML parts) programmatically. This makes it suitable for server-side Node.js usage. The following code example demonstrates the simplest way to create an Excel file with some data.

How to Create Excel XLSX File inside Node.js Apps?

// Import the library
const XLSXWriter = require('node-xlsx-writer');

// 1. Create a new instance, specifying the output file path.
const xlsx = new XLSXWriter('./output/simple_report.xlsx');

// 2. Write the header row (optional, but recommended).
xlsx.writeRow(['Name', 'Email', 'Sign-Up Date']);

// 3. Write some data rows.
xlsx.writeRow(['Alice Smith', 'alice@example.com', '2023-10-25']);
xlsx.writeRow(['Bob Johnson', 'bob.j@domain.com', '2023-10-26']);
xlsx.writeRow(['Charlie Brown', 'charlie@brown.com', '2023-10-27']);

// 4. Finalize the file. This step is crucial to save the file properly.
xlsx.finalize();

console.log('Excel file created successfully!');

Streaming for Large Datasets

This is where Node-XLSX-Writer truly shines. Instead of loading all data into memory, you can stream rows one by one. This is perfect for processing database query results. The library uses functions to emit each record from the database. We immediately write it as a new row in the Excel file. This approach keeps memory usage constant, regardless of whether you're exporting 100 rows or 1 million rows.

How to Perform Streaming from a Database inside Node.js Apps?

// Import the library
const XLSXWriter = require('node-xlsx-writer');

// 1. Create a new instance, specifying the output file path.
const xlsx = new XLSXWriter('./output/simple_report.xlsx');

// 2. Write the header row (optional, but recommended).
xlsx.writeRow(['Name', 'Email', 'Sign-Up Date']);

// 3. Write some data rows.
xlsx.writeRow(['Alice Smith', 'alice@example.com', '2023-10-25']);
xlsx.writeRow(['Bob Johnson', 'bob.j@domain.com', '2023-10-26']);
xlsx.writeRow(['Charlie Brown', 'charlie@brown.com', '2023-10-27']);

// 4. Finalize the file. This step is crucial to save the file properly.
xlsx.finalize();

console.log('Excel file created successfully!');

Custom Styling (Font, Color, Borders)

While not as extensive as full-featured suites, the open source Node-XLSX-Writer library provides essential styling options to make your reports look professional. It supports various features for handling styles in spreadsheets, such as making the font bold or italic, set background color and border color, define the border style (thin', 'medium', 'thick') and so on. The following example shows how software developers can style header row inside Node.js apps.

How to Apply Style to Header Row via Node.js Library?

const XLSXWriter = require('node-xlsx-writer');
const xlsx = new XLSXWriter('./output/styled_report.xlsx');

// Define a style object for the header
const headerStyle = {
  bold: true,
  background: 'FF2A52F5', // Blue background (ARGB Hex format)
  color: 'FFFFFFFF',       // White text
  border: 'thin',          // Thin border around cells
  borderColor: 'FF000000'  // Black border
};

// Apply the style when writing the header row
xlsx.writeRow(['Product', 'Category', 'Price', 'Stock'], headerStyle);

// Data rows are written without a style (or with a different one)
xlsx.writeRow(['Laptop', 'Electronics', 999.99, 15]);
xlsx.writeRow(['Desk Chair', 'Furniture', 249.50, 8]);

xlsx.finalize();