Convert Json to Xlsx File

I am trying to hide json data in an Xlsx file and save it in a folder. I am trying to use the icg-json-to-xlsx module, but so far I have not been able to use it. My code is as follows:

jsonXlsx = require('icg-json-to-xlsx');

filename = path.join('./files', "output.xlsx");
outputFile = jsonXlsx(filename, result) //result contains json data
console.log(outputFile);

but i got this error

outputFile = jsonXlsx(filename, result)
             ^
TypeError: Property 'jsonXlsx' of object # is not a function

Receiving data from mongodb: in routes:

router.get('/', function(req, res, next) {
  fileController.getAll(function(err, result){
     if(err){
        res.send(500,err);
          }  
       // res.json(result);
        var data = result;

in the controller:

FileController.prototype.getAll = function(callback){

File.find( {}, {_id: false, id: true, name: true, status: true}, function(err, file){

    if(err) {
        return callback(err);
    } else {
        if (!file) {
            return callback('file not found');
        }
    }

    callback(null, file);
}
)};
+4
source share
3 answers

try it

  outputFile = jsonXlsx.writeFile(filename, result);

jsonXlsx is an object that contains methods like writeFile, writeBuffer, so you cannot call jsonXlsx as a function ... or you need to add a reference to a function like this

jsonXlsxWriteFile = require('icg-json-to-xlsx').writeFile;  
outputFile        = jsonXlsxWriteFile(filename, result)

Example

var jsonXlsx = require('icg-json-to-xlsx');
var path     = require('path');
var filename = path.join('./files', "output.xlsx");


var result = [ 
  { id: '1', name: 'test', status: '123' }, 
  { id: '2', name: 'david', status: '323'}, 
  { id: '3', name: 'ram', status: '2323' } 
];

var outputFile = jsonXlsx.writeFile(filename, JSON.stringify(result));

console.log(outputFile);

Update :

File
  .find({ })
  .select({
    _id: false, id: true, name: true, status: true
  })
  .lean()
  .exec(function(err, file) {
    //
  });

MongooseDocuments, jsonXlsx JavaScript, lean()

0

Alasql JavaScript JavaScript. JSON XLSX ( js-xlsx.js).

.

npm install alasql
npm install xlsx

alasql:

var alasql = require(alasql);
alasql('SELECT * INTO XLSX("mydata.xlsx",{headers:true}) \
            FROM JSON("mydata.json")');

var cities = [{City:'London',Population:2500000},{City:"Paris",Population:2000000}];
alasql("SELECT * INTO XLSX("mydata.xlsx",{headers:true}) FROM ?",[cities]);

.

+1

, . xlsx , . JSON.

var excel = require('node-excel-export');
var styles = {
    headerDark: {
        fill: {
            fgColor: {
                rgb: 'FF000000'
            }
        },
        font: {
            color: {
                rgb: 'FFFFFFFF'
            },
            sz: 14,
            bold: true,
            underline: true
        }
    },
    cellPink: {
        fill: {
            fgColor: {
                rgb: 'FFFFCCFF'
            }
        }
    },
    cellGreen: {
        fill: {
            fgColor: {
                rgb: 'FF00FF00'
            }
        }
    }
};

var specification = {
    "Col1": {
        "displayName": 'Col1Name',
        "headerStyle": styles.headerDark,
        "width": 250
    },
    "Col2": {
        "displayName": 'Col2Name',
        "headerStyle": styles.headerDark,
        "width": 215
    },
    "Col3": {
        displayName: 'Col3Name',
        headerStyle: styles.headerDark,
        width: 150
    }
}

var report = excel.buildExport(
    [{
        name: 'Report.xlsx',
        specification: specification,
        data: rows
    }]
);
0

All Articles