One data grid for JavaScript, React, Vue & Angular. Sorting, filtering, virtualization, pinning, and CSV export included. Early access $199 $349 → [Get it now]

How to generate Excel files in Node.js

Generating Excel files is crucial for Node.js applications that provide business reporting, data export, and analytics features. As the creator of CoreUI, I’ve built Excel export functionality in countless enterprise dashboards. The most effective solution is to use the xlsx library (SheetJS) to create Excel files with proper formatting and multiple sheets. This approach provides full control over spreadsheet structure and works efficiently for files of any size.

Use the xlsx library to generate Excel files in Node.js.

const express = require('express')
const XLSX = require('xlsx')
const path = require('path')

const app = express()

app.get('/export/excel', (req, res) => {
  const data = [
    { id: 1, name: 'John Doe', email: '[email protected]', salary: 50000 },
    { id: 2, name: 'Jane Smith', email: '[email protected]', salary: 60000 },
    { id: 3, name: 'Bob Johnson', email: '[email protected]', salary: 55000 }
  ]

  const summary = [
    { metric: 'Total Employees', value: data.length },
    { metric: 'Average Salary', value: data.reduce((sum, emp) => sum + emp.salary, 0) / data.length }
  ]

  // Create workbook
  const workbook = XLSX.utils.book_new()

  // Create employee sheet
  const employeeSheet = XLSX.utils.json_to_sheet(data)
  XLSX.utils.book_append_sheet(workbook, employeeSheet, 'Employees')

  // Create summary sheet
  const summarySheet = XLSX.utils.json_to_sheet(summary)
  XLSX.utils.book_append_sheet(workbook, summarySheet, 'Summary')

  // Generate buffer
  const buffer = XLSX.write(workbook, { type: 'buffer', bookType: 'xlsx' })

  // Set headers and send file
  res.setHeader('Content-Disposition', 'attachment; filename=employees.xlsx')
  res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
  res.send(buffer)
})

First, install xlsx with npm install xlsx. The book_new() creates a new workbook, and json_to_sheet() converts data arrays to Excel worksheets. Multiple sheets can be added with book_append_sheet(). The write() function generates the Excel file as a buffer that can be sent directly to the client. Column headers are automatically created from object keys.

Best Practice Note

This is the same Excel generation we use in CoreUI backend systems for business reporting. For advanced formatting like cell colors, fonts, or formulas, use the xlsx library’s cell formatting options. For very large datasets, consider streaming with the xlsx-stream library to avoid memory issues.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.
How to set focus on an input field after rendering in React
How to set focus on an input field after rendering in React

Passing props to child components in React function components
Passing props to child components in React function components

How to loop inside React JSX
How to loop inside React JSX

How to validate an email address in JavaScript
How to validate an email address in JavaScript

Answers by CoreUI Core Team