xlsx-to-json is npm package its used for Convert xlsx file to JSON files using nodejs.??

 

Project Structure :

excel-to-json                       // Project directory
    - node_modules                    // Npm Package
    - server.js                     // our angular code
   -  package-lock.json.            // package-lock.json
   -  package.json.                 // package.json
   -  server.js.                    // server.js file

Create a new directory, we can run the following command:

mkdir <excel-to-json>

then move into the newly created directory:

cd <excel-to-json>

then  run this command : 

npm init

and fill this information like that :

package name: (excel-to-json)
version: (1.0.0)
description:
entry point: (index.js) server.js
test command:
git repository:
keywords:
author:
license: (ISC) 

After creating project install node package run command:

npm install --save express xlsx-to-json xls-to-json

 

create the server.js file and add this code :

var express = require('express')
var app = express()
var xlsxtojson = require("xlsx-to-json");
var xlstojson = require("xls-to-json");
app.use(function(req, res, next) { //allow cross origin requests
    res.setHeader("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", "POST, PUT, OPTIONS, DELETE, GET");
    res.header("Access-Control-Max-Age", "3600");
    res.header("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
    next();
});
// configuration
app.use(express.static(__dirname + '/public'));               
app.use('/public/uploads',express.static(__dirname + '/public/uploads'));      
app.get('/', function (req, res) {
  res.send('Hello World')
})
app.post('/api/xlstojson', function(req, res) {
    xlsxtojson({
        input: "./excel-to-json.xlsx",  // input xls
        output: "output.json", // output json
        lowerCaseHeaders:true
    }, function(err, result) {
        if(err) {
          res.json(err);
        } else {
          res.json(result);
        }
    });
});
app.listen(3000)

Result :

 

Github Link: https://github.com/Sudarshan101/excel-to-json-

See demo: https://www.youtube.com/watch?v=039yEMrZfEc

About the author
Code solution

info@codesolution.co.in

Discussion
  • 0 comments

Add comment To Login
Add comment