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!