How to Read Contents from Excel File in Node Application

Last updated on: by Digamber

In this quick guide, we will teach you how to very efficiently read the excel file data in the node js application using the third-party package.

Excel file holds tons of data in row and column format; the great thing about excel is not limited to one thing; instead, its advantages are diversified:

It offers the following benefits:

  • Best way to store data.
  • Great for calculations.
  • Transform and clean data.
  • Makes data analysis facile.
  • Accessible to data visualizations with charts.
  • Gather or print reports effortlessly.

Enough about knowing the background of excel; our goal in this post is diffrent.

Excel file contains the data in row and column format. Therefore, we will show you how to install and configure the read-excel-file module in the node app. And how to parse the excel data row by row in node js environment.

Node Js Extract Row by Row Data from Excel File Example

  • Step 1: Build Folder
  • Step 1: Make Package JSON
  • Step 2: Formulate Server File
  • Step 3: Add Excel Module
  • Step 4: Read Excel or XLSX File
  • Step 5: Display Excel Data

Build Folder

Get on to the console screen, type the command that we have mentioned below.

As soon as you hit enter onto the keyboard a new blank folder will be created on your system.

mkdir node-vlog

Step inside the newly created directory.

cd node-vlog

Make Package JSON

Now, we have to create a file where our project related information will reside.

This information is simple scripts, commands or project related details.

Hence, you have to run the following command from the command prompt.

npm init

Formulate Server File

In this step, we need to formulate the app.js file.

Also, you require to add this script in the scripts section of package.json file.

...
...
  "scripts": {
    "start": "node app.js"
  },
...
...

Add Excel Module

In this step, we need to add the react excel module make sure to type and execute the given command.

npm install read-excel-file

Read Excel or XLSX File

Now, you have to open the app.js file and insert the following code line by line within the file.

const readExcel = require('read-excel-file/node')
readExcel('./sample.xlsx').then((data) => {
  for (i in data) {
    for (j in data[i]) {
      console.log(data[i][j])
    }
  }
})

In order to read the excel file, we need a sample file that has some data to be read. Consequently, create a sample.xlsx file and keep it at the root of our node app directory.

Display Excel Data

Head over to the console, on the command prompt you have to type the given command.

node app.js

After the script is executed, following output will appear on your console screen.

Price
Payment Type
Name
City
1200
Visa
Sophie
Newtown
1200
Mastercard
asuncion
Centennial
1200
Mastercard
Sandrine
Walnut Creek
3600
Amex
Brittany
Orlando
1200
Visa
Carmen
Arlington
1200
Amex
Corinne
Anthem
1200
Amex
Francoise
Danville
1200
Visa
Katherine
Marietta
1200
Mastercard
Laura
Fairfield

Conclusion

How to Read Contents from Excel File in Node Application

In this short guide, we started with setting up a basic node app; in node js set up, we created some files modules with the aim to extract data from an excel file in node.

Remember, you have to keep the simple xlsx file at the root of your node app, then only you can read and print the excel data on the terminal.

Digamber

I am Digamber, a full-stack developer and fitness aficionado. I created this site to bestow my coding experience with newbie programmers. I love to write on JavaScript, ECMAScript, React, Angular, Vue, Laravel.