Writing data to an Excel file using Node.js

A guide on how to write data to an Excel file using Node.js, employing the ExcelJS library for efficient and effective Excel file manipulation.

In this article, we will explore how to write data into an Excel file using Node.js with the help of the ExcelJS library. This method provides a simple and efficient way to create and store data in a tabular format within Node.js applications.

// Import the ExcelJS library
const ExcelJS = require('exceljs');

// Create a new workbook
const workbook = new ExcelJS.Workbook();
const worksheet = workbook.addWorksheet('Information');

// Add column headers
worksheet.columns = [
  { header: 'Name', key: 'name', width: 20 },
  { header: 'Age', key: 'age', width: 10 },
  { header: 'City', key: 'city', width: 20 },
];

// Add data to the worksheet
worksheet.addRow({ name: 'Nguyen Van A', age: 23, city: 'Hanoi' });
worksheet.addRow({ name: 'Tran Thi B', age: 25, city: 'Ho Chi Minh' });
worksheet.addRow({ name: 'Le Van C', age: 21, city: 'Da Nang' });

// Save the workbook to an Excel file
workbook.xlsx.writeFile('output.xlsx')
  .then(() => {
    console.log('Data has been successfully written to the Excel file.');
  })
  .catch((error) => {
    console.error('An error occurred while writing to the Excel file:', error);
  });

Detailed Explanation

  1. const ExcelJS = require('exceljs');: Imports the ExcelJS library to use Excel-related functionalities.
  2. const workbook = new ExcelJS.Workbook();: Creates a new workbook.
  3. const worksheet = workbook.addWorksheet('Information');: Adds a new worksheet named "Information."
  4. worksheet.columns = [...]: Sets up columns with corresponding headers and widths.
  5. worksheet.addRow(...): Adds rows of data to the worksheet.
  6. workbook.xlsx.writeFile('output.xlsx'): Writes the data to an Excel file named "output.xlsx."
  7. console.log(...): Outputs a success or error message.

System Requirements

  • Node.js v12 or later
  • Library: exceljs

How to install the libraries needed to run the Node.js code above

Use the following command to install:

npm install exceljs

Tips

  • Make sure to install the ExcelJS library before running the code.
  • For more complex Excel operations, refer to the ExcelJS documentation to leverage advanced features.


Related

Guide to speeding up Node.js applications with ThreadPool

A comprehensive guide on using ThreadPool to speed up Node.js applications, enhancing performance and multitasking capabilities. The article covers how to configure and use ThreadPool in Node.js.
Using Selenium in Node.js to send JavaScript code to a website on Chrome

A guide on how to use Selenium in Node.js to automate sending JavaScript code to a web page in the Chrome browser. This article will walk you through the installation and execution steps.
How to open the Notepad application using Node.js

A guide on how to open the Notepad application on Windows using Node.js with the `child_process` module. This simple method demonstrates how to invoke system applications from Node.js.
JSON Web Token (JWT) Authentication in Node.js

This article provides a guide on how to use JSON Web Tokens (JWT) for user authentication in a Node.js application. JWT is a secure and popular way to protect APIs by transmitting user authentication information between the server and the client.
How to UPDATE data in a MySQL database using Node.js

A guide on how to use Prepared Statements in Node.js to update data in a MySQL database table safely and effectively.
How to automate website login using Selenium with Chrome in Node.js

A guide on how to use Selenium in Node.js to automate the login process for a website. The article will show how to set up the environment and write Node.js code to control Chrome.
How to SELECT data from a MySQL database using Node.js

A guide on how to use Prepared Statements in Node.js to query data from a MySQL database with multiple parameters safely and effectively.
How to Post data to API Using Node.js

This article guides you on how to send JSON data to an API using the axios library in Node.js, making it easy to perform POST requests to a web service.
How to INSERT data into a MySQL database using Node.js

A guide on how to use Prepared Statements in Node.js to insert data into a table in a MySQL database safely and effectively with multiple parameters.
Create a Simple Chat Application Using Socket.IO in Node.js

A detailed guide on how to create a simple chat application using Socket.IO in Node.js, allowing users to send and receive messages in real-time.

main.add_cart_success