Excel is a common office software used by many businesses and organizations for data analysis and processing. In web development, it is often necessary to export some data to Excel tables for users to download and view. In this article, we'll show you how to export data to an Excel document using Node.js.
1. Install the necessary modules
In Node.js, we can use third-party modules to export data into Excel documents. Among them, the two most commonly used modules are xlsx and exceljs. Before using these modules, we need to install them first:
$ npm install xlsx exceljs
2. Use the xlsx module to export Excel
xlsx is a powerful and easy-to-use JavaScript module for exporting data to Excel document. It supports multiple formats including XLSX, XLS, ODS, CSV, etc.
The following is a simple example that demonstrates how to export an array containing name, age and gender to an Excel table:
const fs = require('fs'); const XLSX = require('xlsx'); const data = [ ['Name', 'Age', 'Gender'], ['Amy', 25, 'Female'], ['Bob', 30, 'Male'], ['Cathy', 27, 'Female'] ]; const workbook = XLSX.utils.book_new(); const worksheet = XLSX.utils.aoa_to_sheet(data); XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet1'); const filename = 'data.xlsx'; XLSX.writeFile(workbook, filename); console.log(`File ${filename} is written successfully`);
In the above code, we first create an array containing the data Two-dimensional array. We then convert this array into a sheet object using the XLSX.utils.aoa_to_sheet() method. Next, we create a workbook object and add the sheet to the workbook using the XLSX.utils.book_append_sheet() method. Finally, we write the workbook to a file using the XLSX.writeFile() method.
After executing the above code, we will get an Excel file named data.xlsx, which contains the data we exported.
3. Use exceljs module to export Excel
exceljs is another popular Node.js module for exporting data into Excel documents. It has more features than xlsx, such as support for multiple worksheets, cell formatting, charts, etc.
The following is an example of using exceljs to export an Excel table:
const fs = require('fs'); const ExcelJS = require('exceljs'); const data = [ ['Name', 'Age', 'Gender'], ['Amy', 25, 'Female'], ['Bob', 30, 'Male'], ['Cathy', 27, 'Female'] ]; const workbook = new ExcelJS.Workbook(); const worksheet = workbook.addWorksheet('Sheet1'); data.forEach(row => worksheet.addRow(row)); const filename = 'data.xlsx'; workbook.xlsx.writeFile(filename) .then(() => console.log(`File ${filename} is written successfully`));
In the above code, we first create a two-dimensional array containing data. Then, we create an ExcelJS.Workbook object and add a worksheet using the addWorksheet() method. Next, we loop through the array and add each row of data to the worksheet using the addRow() method. Finally, we write the workbook to a file using the writeFile() method.
Different from xlsx, exceljs needs to use Promise to handle file writing operations. After executing the above code, we will get an Excel file named data.xlsx, which contains the data we exported.
4. Summary
In this article, we introduced how to use Node.js to export data into an Excel document. We can easily accomplish this task by using third-party modules like xlsx or exceljs. Whether you need to export data to XLSX or XLS format, or support advanced features like multiple worksheets or cell formatting, these modules can meet your needs.
The above is the detailed content of How to export nodejs to excel. For more information, please follow other related articles on the PHP Chinese website!

The article discusses HTML5 audio formats and cross-browser compatibility. It covers MP3, WAV, OGG, AAC, and WebM, and suggests using multiple sources and fallbacks for broader accessibility.

SVG and Canvas are HTML5 elements for web graphics. SVG, being vector-based, excels in scalability and interactivity, while Canvas, pixel-based, is better for performance-intensive applications like games.

HTML5 enables drag and drop with specific events and attributes, allowing customization but facing browser compatibility issues on older versions and mobile devices.

The article discusses the differences between HTML's <meter> and <progress> tags, used for displaying scalar values and task progress, respectively.

Here is the converted data into a tabular format using HTML5, including examples and strategies for responsive design, best practices for styling, and semantic HTML5 tags used within a table structure:<!DOCTYPE html> <html lang=&

The article discusses image maps in web design, their benefits like enhanced navigation and engagement, and tools for their creation.

The article discusses the differences between <datalist> and <select> tags, focusing on their functionality, user interaction, and suitability for different web development scenarios.

The article discusses the differences between HTML's <figure> and <img> tags, focusing on their purposes, usage, and semantic benefits. The main argument is that <figure> provides better structure and accessi


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools
