1. Products
  2.   Spreadsheet
  3.   Node.js
  4.   Node-Excel-Export Library
 
  

Open Source Node.js Library to Export Data to Excel XLSX

A Leading Open Source Node.js Excel XLSX Library that enables Software Developers to Export Data into Excel XLSX Format via free API in Node.js Environment.

The Node-Excel-Export Library is a powerful tool for software developers who want to export data from Node.js applications to Excel. Software developers looking to produce Excel spreadsheets programmatically will find this open source library, which is available on GitHub, to be an excellent choice due to its numerous capabilities. The library allows developers to set fonts, colors, borders, and other style elements to satisfy specific requirements or corporate branding regulations.

Node-Excel-Export is a basic yet versatile module that allows software developers to quickly generate Excel files from JSON data. This ease is critical for users who need to rapidly and effectively export data from their apps to a standard format. The library supports both XLSX and XLS formats, making it compatible with a variety of Excel versions. The library includes a number of critical capabilities, including mapping JSON data to XLSX, exporting data to Excel files, defining font styles, multi-sheet exports, Excel formula inclusion, cross-platform compatibility, and more.

The Node-Excel-Export package is simple to use and works seamlessly with Node.js apps. It is easily deployed with NPM, and its straightforward API enables software developers to start exporting data to Excel files with little preparation. As an open source project, the library benefits from the developer community's contributions and improvements. This collaborative approach ensures that the library is up to date with the most recent features and best practices. Its numerous capabilities, including customizable styles, multi-sheet support, and speed optimization, make it an essential library for data-driven applications.

Previous Next

Getting Started with Node-Excel-Export

The recommended way to install Node-Excel-Export is using npm, please use the following script for a smooth installation.

Install Node-Excel-Export via npm

npm install excel-export

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

Export JSON Data to Excel XLSX in Node.js

The Node-Excel-Export library is a powerful yet user-friendly tool that simplifies the process of generating Excel files in Node.js applications. It simplifies the process of mapping JSON data to an Excel spreadsheet. Software developers can define schemas to specify how JSON data should be translated into Excel cells. This feature ensures that the exported data retains the desired structure and format, making the resultant spreadsheets more readable and organized. The following example shows how to define a schema with styles and specify how JSON data should map to Excel columns and generates the Excel file content based on the schema and data provided.

How to Generates Excel File form JSON Data inside Node.js Apps?

const excel = require('node-excel-export');

// Define a schema for the export
const styles = {
    headerDark: {
        fill: {
            fgColor: {
                rgb: 'FF000000'
            }
        },
        font: {
            color: {
                rgb: 'FFFFFFFF'
            },
            sz: 14,
            bold: true,
            underline: true
        }
    },
    cellPink: {
        fill: {
            fgColor: {
                rgb: 'FFFFCCFF'
            }
        }
    }
};

const specification = {
    name: {
        displayName: 'Name',
        headerStyle: styles.headerDark,
        width: 120
    },
    age: {
        displayName: 'Age',
        headerStyle: styles.headerDark,
        width: 100
    },
    location: {
        displayName: 'Location',
        headerStyle: styles.headerDark,
        width: 150
    }
};

const dataset = [
    { name: 'John Doe', age: 30, location: 'New York' },
    { name: 'Jane Smith', age: 28, location: 'San Francisco' }
];

const report = excel.buildExport(
    [
        {
            name: 'Report',
            specification: specification,
            data: dataset
        }
    ]
);

// Save the Excel file
require('fs').writeFileSync('report.xlsx', report);

Customize Styles While Exporting Data to XLSX in Node.js

Presentation is crucial when sharing data, and Node-Excel-Export excels in this area by allowing extensive customization of cell styles. Users can define fonts, colors, borders, and other stylistic elements to match specific requirements or corporate branding guidelines. This flexibility ensures that the exported Excel files are not only functional but also visually appealing. The following example shows how to apply custom styles to both headers and cells, ensuring that the resulting Excel file is both functional and aesthetically pleasing.

How to Apply Custom Styles to Excel File Headers and Cells during Data Export to XLSX in Node.js?

const styles = {
    headerGreen: {
        fill: {
            fgColor: {
                rgb: 'FF00FF00'
            }
        },
        font: {
            color: {
                rgb: 'FF000000'
            },
            sz: 12,
            bold: true
        }
    },
    cellYellow: {
        fill: {
            fgColor: {
                rgb: 'FFFFFF00'
            }
        }
    }
};

const specification = {
    product: {
        displayName: 'Product',
        headerStyle: styles.headerGreen,
        cellStyle: styles.cellYellow,
        width: 120
    },
    price: {
        displayName: 'Price',
        headerStyle: styles.headerGreen,
        width: 100
    },
    stock: {
        displayName: 'Stock',
        headerStyle: styles.headerGreen,
        width: 150
    }
};

const dataset = [
    { product: 'Laptop', price: 1200, stock: 30 },
    { product: 'Phone', price: 800, stock: 50 }
];

const report = excel.buildExport(
    [
        {
            name: 'Inventory',
            specification: specification,
            data: dataset
        }
    ]
);

require('fs').writeFileSync('inventory.xlsx', report);

Define Multiple Sheets While Exporting Data in Node.js

The open source Node-Excel-Export library fully supports the creation of multiple sheets within a single Excel file while exporting data to Excel XLSX files inside Node.js applications. This feature is particularly useful for large datasets or when categorizing data into separate logical groups. Users can define multiple sheets within a single Excel file, each with its own schema and data. The following example shows how developers can create an Excel file with two sheets: one for user data and another for product data. Each sheet has its own schema and dataset, demonstrating the flexibility of the library.

How to Create Multiple Sheets in Excel File while Export Data in Node.js?

const sheet1Specification = {
    name: { displayName: 'Name', width: 120 },
    age: { displayName: 'Age', width: 100 }
};

const sheet2Specification = {
    product: { displayName: 'Product', width: 120 },
    price: { displayName: 'Price', width: 100 }
};

const sheet1Data = [
    { name: 'John Doe', age: 30 },
    { name: 'Jane Smith', age: 28 }
];

const sheet2Data = [
    { product: 'Laptop', price: 1200 },
    { product: 'Phone', price: 800 }
];

const report = excel.buildExport(
    [
        {
            name: 'Users',
            specification: sheet1Specification,
            data: sheet1Data
        },
        {
            name: 'Products',
            specification: sheet2Specification,
            data: sheet2Data
        }
    ]
);

require('fs').writeFileSync('multiple_sheets.xlsx', report);