Node.js Tutorial to Generate Excel File From JSON Data Using xlsx Module in Terminal
38 views
Jun 7, 2025
Buy the full source code of application here: https://codingshiksha.com/node/node-js-tutorial-to-generate-excel-file-from-json-data-using-xlsx-module-in-terminal/ Hi Join the official discord server to resolve doubts here: https://discord.gg/cRnjhk6nzW Visit my Online Free Media Tool Website https://freemediatools.com/ Buy Premium Scripts and Apps Here: https://procodestore.com/
View Video Transcript
0:00
uh hello guys uh welcome to this uh live
0:02
stream so in this tutorial I will show
0:04
you how to generate a excel file inside
0:08
NodeJS from JSON data so let me show you
0:13
the script that I written right here so
0:14
this is actually a very simple script
0:16
which will generate a Microsoft Excel
0:19
file from a set of JSON data json as you
0:23
all know it's a set of JavaScript
0:25
objects so we have this JSON object
0:28
right here an array of object each
0:30
object is having three properties name
0:33
age and city and now we just the prime
0:36
job of this script is to export this
0:38
JSON data into an Excel file which will
0:41
look something like this so you will see
0:43
it has it will actually convert this
0:46
JSON data into an Excel file uh let me
0:49
just delete this and re rerun the script
0:53
here
0:58
so now the job is if I go to the
1:02
terminal and just run the script here
1:05
you would see it will export all this
1:08
JSON data and then it will create a
1:10
excel file so you will see your Excel
1:12
file is generated and on the left hand
1:15
side you will see your Excel file is
1:17
generated you will see if I try to open
1:20
this this will look something like
1:23
this so all the
1:26
three data has been successfully
1:28
exported and for building this
1:30
application we are actually using a very
1:33
simple module inside NodeJS which is X
1:38
selects so just search for this module
1:40
and you just need to install this it's a
1:44
sheet JS it is commonly know known as
1:47
sheet JS library so the command is
1:50
simple
1:52
you first of all need to install
1:55
this so after installing it now I will
1:59
just write this code here so I've also
2:02
given the full source code in the
2:04
description of this video so first of
2:06
all you need to require all the packages
2:09
so for requiring this we
2:12
simply use the require keyword and then
2:15
require the module and then for building
2:18
this we also require the file system
2:20
module as well and after that we will be
2:23
declaring actually the JSON data that
2:26
needs to be converted so you just
2:28
declare a
2:29
variable which will be an array of
2:32
object so as I already told you each
2:34
object will
2:36
have three properties name and then we
2:40
also have age you can take any data of
2:43
your
2:44
choice this is one set of data one row
2:47
of data which contains three properties
2:49
here name age and city and
2:53
similarly we can put a comma and then we
2:55
can write another
2:57
object
2:59
similarly which we need to convert this
3:01
so we can simply so this is three sets
3:05
of data right here so now we just need
3:08
to convert this into Excel file so we
3:10
declared this after declaring it now we
3:13
just need to convert this JSON to a
3:16
worksheet so we declare a variable right
3:19
here and this
3:21
module you can see this XLS module right
3:25
here it contains a function to
3:30
actually first of all we need to create
3:32
a worksheet right
3:34
here it contains a util function and
3:37
then it contains this function which is
3:40
JSON JSON to sheet so we are using this
3:43
method to convert the JSON into a
3:46
worksheet so here we are providing the
3:48
JSON data after that we need to create a
3:51
new workbook for creating a new workbook
3:54
we simply create use
3:57
this which is book new so this will
4:01
create a new book Excel workbook and
4:05
then we just need to append the data for
4:08
appending the data we use another method
4:10
right here which is
4:13
book_append
4:15
sheet so this method will
4:18
actually put the data inside the
4:21
worksheet so we need to put uh we need
4:24
to put this data that we declared which
4:26
is the JSON
4:27
data and we simply pass the workbook
4:32
plus the worksheet that we are working
4:35
on and then the you can give the name of
4:38
the sheet so I will say sheet
4:41
one so after doing that now we just need
4:44
to download the Excel file so we can
4:47
simply put the file name of the output
4:50
file so we can say
4:52
output.xls and then to save this we can
4:55
simply call this function which is write
4:59
file so this will actually save the
5:02
excel
5:03
file the workbook at that location
5:06
output file
5:09
so after that we can simply console log
5:11
a simple statement that your data has
5:14
been
5:16
written so this is all the uh NodeJS
5:19
script which is required to export this
5:21
JSON data into an Excel file so if I try
5:23
to execute
5:25
this so now you will see if I go to the
5:28
terminal just write node index.js output
5:31
file created
5:33
if
5:34
I delete this once again execute this
5:38
you will see again it will create this
5:40
Excel file so this Excel file will be
5:43
having this data right here we have
5:45
three columns name age city so this is
5:49
actually exporting all these objects so
5:52
we can declare this JSON data right here
5:55
so we have these three objects which is
5:58
successfully exported to an Excel file
6:00
so in this easy way you can export JSON
6:04
into an Excel
6:06
file so thank you very much for watching
6:09
this video and I will be seeing you in
6:12
the next one