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

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

READ  Redis Caching in Node.js

 

 

views/index.ejs

 

 

<!DOCTYPE html>
<html>
  <head>
    <title><%=title%></title>
    <link
      rel="stylesheet"
      href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
    />
  </head>
  <body>
      <div class="container">
          <h1 class="text-center">
              Raw JSON to Excel File
          </h1>
          <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>
            <div class="form-group">
                <button class="btn btn-danger btn-block">
                    Download Excel File
                </button>
            </div>
        </form>
      </div>
</body>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</html>

 

 

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.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended:false}))

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

const PORT = 4000

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

app.post('/jsontoexcel',(req,res) => {

    var jsondata = req.body.json

    var exceloutput = Date.now() + "output.xlsx"

    if(isJson(jsondata)){
        var xls = json2xls(JSON.parse(jsondata));

        fs.writeFileSync(exceloutput, xls, 'binary');

        res.download(exceloutput,(err) => {
            if(err){
                fs.unlinkSync(exceloutput)
                res.send("Unable to download the excel file")
            }
            fs.unlinkSync(exceloutput)
        })
    }
    else{
        res.send("JSON Data is not valid")
    }

})

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

 

 

 

Screenshot

 

 

 

Leave a Reply