Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

convert excel file to json using node js

let express = require('express'),
    app = express(),
    bodyParser = require('body-parser'),
    multer = require('multer'),
    crypto = require('crypto'),
    xlsxtojson = require('xlsx-to-json'),
    xlstojson = require("xls-to-json");

let fileExtension = require('file-extension');

    app.use(bodyParser.json());  

    let storage = multer.diskStorage({ //multers disk storage settings
        destination: function (req, file, cb) {
            cb(null, './input/')
        },
        filename: function (req, file, cb) {
            crypto.pseudoRandomBytes(16, function (err, raw) {
                cb(null, raw.toString('hex') + Date.now() + '.' + fileExtension(file.mimetype));
                });
        }
    });

    let upload = multer({storage: storage}).single('file');

    /** Method to handle the form submit */
    app.post('/sendFile', function(req, res) {
        let excel2json;
        upload(req,res,function(err){
            if(err){
                 res.json({error_code:401,err_desc:err});
                 return;
            }
            if(!req.file){
                res.json({error_code:404,err_desc:"File not found!"});
                return;
            }

            if(req.file.originalname.split('.')[req.file.originalname.split('.').length-1] === 'xlsx'){
                excel2json = xlsxtojson;
            } else {
                excel2json = xlstojson;
            }

           //  code to convert excel data to json  format
            excel2json({
                input: req.file.path,  
                output: "output/"+Date.now()+".json", // output json 
                lowerCaseHeaders:true
            }, function(err, result) {
                if(err) {
                  res.json(err);
                } else {
                  res.json(result);
                }
            });

        })

    });
    // load index file to upload file on http://localhost:3000/
    app.get('/',function(req,res){
        res.sendFile(__dirname + "/index.html");
    });

    app.listen('3000', function(){
        console.log('Server running on port 3000');
    });
Comment

PREVIOUS NEXT
Code Example
Javascript :: disable js in chrome dev tools 
Javascript :: preview file before upload in react 
Javascript :: why does array index start from 0 
Javascript :: null is true or false javascript 
Javascript :: how to access value of itself object in javascript 
Javascript :: .unshift 
Javascript :: javascrip functions parameters 
Javascript :: jetty 
Javascript :: for in loops javascript 
Javascript :: alertify js examples 
Javascript :: append to array in js 
Javascript :: reference data types in javascript 
Javascript :: jquery edit href 
Javascript :: mongoose model schema 
Javascript :: double click react 
Javascript :: how to disable previous date in datepicker using angular 6 
Javascript :: how to declare objects inside arrays in javascript 
Javascript :: destructuring nested objects 
Javascript :: javascript string to ascii array 
Javascript :: how-to-close-current-tab-in-a-browser-window 
Javascript :: never give up 
Javascript :: json parse vs json stringify 
Javascript :: change h2 to h1 using javascript 
Javascript :: Regex Match Only Number Lines 
Javascript :: react native run android common error 
Python :: ignore filter warnings jupyter notebook 
Python :: suppres tensorflow warnings 
Python :: python - show all columns / rows of a Pandas Dataframe 
Python :: pygame get screen width and height 
Python :: dotenv python 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =