How to Export an Array to Excel in JavaScript?

Estimated read time 1 min read

To export an array to Excel in JavaScript, you can use the SheetJS library which is an open-source JavaScript library for manipulating Excel files. Here’s an example of how to export an array to an Excel file using SheetJS:

// Import SheetJS library
const XLSX = require('xlsx');

// Define your array
const myArray = [
  ['Name', 'Age', 'City'],
  ['John', 25, 'New York'],
  ['Jane', 30, 'London'],
  ['Bob', 35, 'Paris']

// Create a new workbook
const workbook = XLSX.utils.book_new();

// Convert the array to a worksheet
const worksheet = XLSX.utils.aoa_to_sheet(myArray);

// Add the worksheet to the workbook
XLSX.utils.book_append_sheet(workbook, worksheet, 'My Sheet');

// Write the workbook to a file
XLSX.writeFile(workbook, 'myFile.xlsx');

In this example, we first import the SheetJS library, define our array, create a new workbook, convert the array to a worksheet, add the worksheet to the workbook, and finally write the workbook to a file using the writeFile method. This will create a new file called “myFile.xlsx” in the same directory as your script, containing the data from the array.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply