Node.js Excel Export

1.4.4 · maintenance · verified Sun Apr 19

node-excel-export is a Node.js module designed for generating `.xlsx` files from JavaScript datasets. It takes an array of objects as input along with a JSON-based report specification, allowing for flexible configuration of column headers, data mapping, and cell styling. The current stable version is 1.4.4. While its release cadence appears to be slow, suggesting a mature and largely feature-complete library, it remains functional for its stated purpose. Key differentiators include its ability to apply rich cell styling, dynamically reformat data using renderer functions, and define complex header rows and cell merges directly from a JavaScript configuration. It abstracts away the complexities of the underlying `xlsx` (js-xlsx) library for common export scenarios, providing a simpler API for generating Excel reports on the server side.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define styles, create heading rows, specify column structures, and generate an Excel (.xlsx) file from a dataset, saving it to disk.

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

// You can define styles as json object
const styles = {
  headerDark: {
    fill: {
      fgColor: {
        rgb: 'FF000000'
      }
    },
    font: {
      color: {
        rgb: 'FFFFFFFF'
      },
      sz: 14,
      bold: true,
      underline: true
    }
  },
  cellPink: {
    fill: {
      fgColor: {
        rgb: 'FFFFCCFF'
      }
    }
  },
  cellGreen: {
    fill: {
      fgColor: {
        rgb: 'FF00FF00'
      }
    }
  }
};

//Array of objects representing heading rows (very top)
const heading = [
  [{value: 'a1', style: styles.headerDark}, {value: 'b1', style: styles.headerDark}, {value: 'c1', style: styles.headerDark}],
  ['a2', 'b2', 'c2'] // <-- It can be only values
];

//Here you specify the export structure
const specification = {
  customer_name: { 
    displayName: 'Customer', 
    headerStyle: styles.headerDark, 
    cellStyle: function(value, row) { 
      return (row.status_id == 1) ? styles.cellGreen : {fill: {fgColor: {rgb: 'FFFF0000'}}}; 
    },
    width: 120 
  },
  status_id: {
    displayName: 'Status',
    headerStyle: styles.headerDark,
    cellFormat: function(value, row) { 
      return (value == 1) ? 'Active' : 'Inactive';
    },
    width: '10' 
  },
  note: {
    displayName: 'Description',
    headerStyle: styles.headerDark,
    cellStyle: styles.cellPink, 
    width: 220 
  }
}

// The data set should have the following shape (Array of Objects)
const dataset = [
  {customer_name: 'IBM', status_id: 1, note: 'some note', misc: 'not shown'},
  {customer_name: 'HP', status_id: 0, note: 'some note'},
  {customer_name: 'MS', status_id: 0, note: 'some note', misc: 'not shown'}
]

// Define an array of merges.
const merges = [
  { start: { row: 1, column: 1 }, end: { row: 1, column: 10 } },
  { start: { row: 2, column: 1 }, end: { row: 2, column: 5 } },
  { start: { row: 2, column: 6 }, end: { row: 2, column: 10 } }
]

// Create the excel report. This function will return a Buffer.
const report = excel.buildExport(
  [
    {
      name: 'Report',
      heading: heading,
      merges: merges,
      specification: specification,
      data: dataset
    }
  ]
);

// Save the buffer to a file
fs.writeFileSync('report.xlsx', report);
console.log('report.xlsx created successfully.');

view raw JSON →