Node.js Express Raw JSON to Excel File Converter Full Web App Using json2xls Library Deployed to Live Website 2020

You are currently viewing Node.js Express Raw JSON to Excel File Converter Full Web App Using json2xls Library Deployed to Live Website 2020



Welcome folks today in this tutorial we will be deploying a real web app where we will be making a real life web app in which we will be converting raw json to excel file converter. All the source code of the app is shown below.


Get Started



In order to get started we will be using a node dependency called as json2xls


npm init -y


npm i json2xls is-json express ejs


Now after installing this dependency inside your node.js project


Create a express project by creating a index.js file like this and copy paste the following code into it

var json2xls = require('json2xls');
var json = {
    foo: 'bar',
    qux: 'moo',
    poo: 123,
    stux: new Date()

var xls = json2xls(json);

fs.writeFileSync('data.xlsx', xls, 'binary'); 



Complete Example



Now we will build the complete example where we will take the input from the user in the form of json and then we will convert to excel file and download that excel file.



Now make a views folder and inside that folder create a index.ejs file and copy paste the following code to it






<!DOCTYPE html>
      <div class="container">
          <h1 class="text-center">
              Raw JSON to Excel File
          <form action="/jsontoexcel" method="post">
            <div class="form-group">
                <label for="json">JSON Data:</label>
                <textarea class="form-control" name="json" cols="30" rows="10" placeholder="Paste your json here"></textarea>
            <div class="form-group">
                <button class="btn btn-danger btn-block">
                    Download Excel File
  <script src=""></script>



Now we need to create index.js file and copy paste the following code which is shown below



var express = require('express')

var isJson = require('is-json')

var json2xls = require('json2xls')

var bodyParser = require('body-parser')

var fs = require('fs')

const app = express()


app.set('view engine','ejs')

const PORT = 4000

app.get('/',(req,res) => {
    res.render('index',{title:'Convert Raw JSON to Excel File Free Converter -'})
})'/jsontoexcel',(req,res) => {

    var jsondata = req.body.json

    var exceloutput = + "output.xlsx"

        var xls = json2xls(JSON.parse(jsondata));

        fs.writeFileSync(exceloutput, xls, 'binary');,(err) => {
                res.send("Unable to download the excel file")
        res.send("JSON Data is not valid")


app.listen(PORT,() => {
    console.log("Your app is listening on port 4000")








Leave a Reply