JavaScript to XLSX: Export Data to Excel with Practical JS Code

Learn how to convert JavaScript data to XLSX files using SheetJS and ExcelJS with server-side and browser examples, best practices, and debugging tips for robust Excel exports.

JavaScripting
JavaScripting Team
·5 min read
Quick AnswerDefinition

Exporting data from JavaScript to XLSX is achieved with libraries like SheetJS (xlsx) or ExcelJS rather than translating code. This guide demonstrates how to generate Excel workbooks from JavaScript data, both on the server (Node.js) and in the browser, with practical code samples, data structuring tips, and best practices for robust exports.

Practical scenarios: when you need to export JavaScript data to XLSX

In modern web apps, users often request downloadable reports, inventories, or dashboards. You might hold user data as arrays of objects, JSON, or tabular structures in memory, and need to produce a native Excel file for offline analysis. The phrase javascript to xlsx refers to the workflow of taking data from your JavaScript runtime and writing it into a .xlsx workbook that can be opened by Excel or Google Sheets. According to JavaScripting, this export workflow is a common requirement for dashboards, admin panels, and reporting features. In this section we establish the problem space and show a minimal working example using SheetJS to turn a simple two-dimensional array into a workbook. The goal is to demonstrate a repeatable pattern you can adapt to any data shape, while keeping the API surface approachable for beginners and robust for production.

JavaScript
// Basic SheetJS export: 2D array to .xlsx const data = [ ["Name", "Email", "Role"], ["Alice", "[email protected]", "Engineer"], ["Bob", "[email protected]", "Designer"] ]; const ws = XLSX.utils.aoa_to_sheet(data); const wb = XLSX.utils.book_new(); XLSX.utils.book_append_sheet(wb, ws, "People"); XLSX.writeFile(wb, "people.xlsx");
  • This pattern creates a worksheet from a simple nested array, assigns a sheet name, and writes to disk. You can also start from a list of objects and convert to a sheet via json_to_sheet.
  • For clarity, the example assumes a Node.js environment with the SheetJS xlsx package installed. In the browser, you would generate a blob and trigger a download instead of writing a file.

line-by-lineBreakdown: null

Common variations: Start from objects, template rows, or apply headers separately. If you need date formatting or numeric precision, configure cell formats after you create the sheet.

Steps

Estimated time: 30-60 minutes

  1. 1

    Install dependencies

    Create a project folder, initialize npm, and install xlsx or exceljs. This sets up the toolchain for exporting data to XLSX.

    Tip: Pin versions to avoid breaking changes.
  2. 2

    Prepare your data

    Structure data as arrays of arrays or objects. SheetJS and ExcelJS work with both formats.

    Tip: Validate your data types before export.
  3. 3

    Create a workbook and worksheet

    Use library APIs to create a workbook, add a worksheet, and define columns.

    Tip: Set headers and keys for consistent exports.
  4. 4

    Write data to file or blob

    Write the workbook to disk (Node) or trigger a download in the browser.

    Tip: Choose appropriate output type (file vs blob).
  5. 5

    Handle errors and edge cases

    Add try/catch blocks and validate data ranges; handle large datasets with streaming if needed.

    Tip: Test with edge-case data.
  6. 6

    Validate and test export

    Open the generated XLSX to confirm structure, formatting, and data integrity.

    Tip: Automate tests for repeated exports.
Pro Tip: Prefer Node.js for large data exports to avoid browser memory constraints.
Warning: XLSX has a maximum of 1,048,576 rows per sheet; plan pagination for bigger datasets.
Note: Keep data types consistent (strings, numbers, dates) to ensure correct formatting in Excel.
Pro Tip: Leverage workbook properties and styles to improve readability.

Prerequisites

Required

Optional

  • A modern browser for browser-based export
    Optional
  • Optional: TypeScript setup for typed exports
    Optional

Commands

ActionCommand
Install SheetJS (xlsx) for Node.js exportsUse in Node.js scripts or bundlersnpm install xlsx
Install ExcelJS for richer formattingSupports streaming and advanced formattingnpm install exceljs
Run export scriptYour script builds and writes the workbooknode export.js

Questions & Answers

What is the difference between SheetJS and ExcelJS?

SheetJS focuses on parsing and writing a wide range of spreadsheet formats with a consistent API; ExcelJS offers richer formatting options and a streaming writer suitable for large files. Choose based on your needs.

SheetJS is great for broad format support, while ExcelJS shines with advanced formatting and streaming.

Can I export directly from the browser without a server?

Yes. Libraries like SheetJS can run in the browser, generate a workbook in memory, and trigger a download via a blob.

Yes, you can export XLSX in the browser and download a file.

What are common errors when exporting to XLSX?

Common issues include memory limits for large datasets, incorrect data types, and missing headers. Use streaming and validation to mitigate.

Common export errors are memory limits and type mismatches; validate data.

Do XLSX exports support formulas and formatting?

Yes, both libraries support basic formulas and cell formatting, though capabilities vary. Check docs for advanced features.

You can add formulas and formatting, depending on the library.

Is there a browser compatibility concern?

Most modern browsers support Blob downloads; ensure polyfills if supporting older browsers.

Modern browsers support XLSX export via Blob downloads.

What to Remember

  • Export data from JavaScript using SheetJS or ExcelJS
  • Choose Node.js for large exports, browser for small ones
  • Define clear headers and data types
  • Prefer streaming for big datasets to reduce memory usage
  • Test exported files to ensure data integrity

Related Articles