Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON to Excel convertion in Nodejs

Tags:

node.js

excel

I'm trying to convert large amount of json data to excel and tried couple of modules Below are my findings, if anyone used better node module which handle more data please let me know so that i can explore

json2xls

JSON array with 100000 length took 402574ms once i exceeded to 200000 it failed with this error FATAL ERROR: CALL_AND_RETRY_2 Allocation failed - process out of memory

node-xls

JSON array with 100000 length took 444578ms

I tried this in windows 7 system with 8GB RAM, Intel Core i7, CPU @ 2.10Ghz - 2.70Ghz

like image 205
tomalex Avatar asked Aug 26 '15 21:08

tomalex


2 Answers

First push your data into a temporary array with required column and then convert it into xls, I have done it in following manner:

// use the below package to convert json to xls
var json2xls = require('json2xls');

json.forEach(function(instance, indexx,record){
        var tempArry = {
            'ColoumnName1' : record[indexx].columnNameVlaue,
            'ColoumnName2' : record[indexx].columnNameVlaue,
            'ColoumnName3' : record[indexx].columnNameVlaue,
            'ColoumnName4' : record[indexx].columnNameVlaue
        }
        jsonArray.push(tempArry);
    });
//this code is for sorting  xls with required value
    jsonArray.sort(function(a, b) {
        return parseFloat(b.ColoumnName4) - parseFloat(a.ColoumnName4);
    });
    var xls = json2xls(jsonArray);

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

Dont try to add all the data into the excel file, use the specific columns you want in the file to be saved.

like image 65
Maximus Avatar answered Nov 14 '22 22:11

Maximus


If its a nodejs project then do this,

const xlsx = require("xlsx")//npm install xlsx
const fs = require("fs")//npm install fs
var rawFile = fs.readFileSync("./datas.json")//dir of your json file as param
var raw = JSON.parse(rawFile)
var files  = []
for (each in raw){
    files.push(raw[each])
    }  
   var obj = files.map((e) =>{
        return e
       })

   var newWB = xlsx.book_new()

   var newWS = xlsx.utils.json_to_sheet(obj)

   xlsx.utils.book_append_sheet(newWB,newWS,"name")//workbook name as param

   xlsx.writeFile(newWB,"Sample-Sales-Data.xlsx")//file name as param
like image 24
Ratul Das Avatar answered Nov 14 '22 23:11

Ratul Das