August 8, 2022

Blog @ Munaf Sheikh

Latest news from tech-feeds around the world.

Read Excel File in JSON Format Using Nodejs


This tutorial help to read excel file using nodejs. We’ll let you know to read the excel file in JSON format and insert it into MySQL data or NoSQL database using rest API.

The read-excel-file module help to read small to medium *.xlsx files in a browser or Node.js. It parses file data to an array of JSON objects using a schema.

I am also using chalk module for coloring in console window.

The Axios HTTP client is a Promise-based HTTP client for the react/nodejs application.

Checkout Other Nodejs tutorials:

Read Excel File and Insert Data Using Nodejs

We’ll use Read Excel File npm module to read the data from excel file in a json format.

Let’s create index.js file into the project folder.I have added below line at the top of the file:

As We know:

– The first line that begins with #! is usually called a “shebang.” This is normally only used on Linux or UNIX operating systems to inform the system what type of script is included in the rest of the text file.
chalk: This is used for terminal string styling.
read-excel-file: Help to read excel file.
axios: HTTP Client to save data into the db using API.

Define Schema

Let’s Define schema for excel sheet.

Read Data From Excel Sheet

Let’s read excel file using npm module.

We have used readXlsxFile instance and passed the excel source file path and schema, if everything is fine then we’ll get all excel data in a row as a JSON format.

Nodejs Method to Save Data into the Database

We have a JSON object, I am going to save the JSON object into the MySQL database.

We have used POST type method and passed employee row data as a payload.




Source link