简体   繁体   中英

Convert Json into Xlsx File

I am trying to covert json data into Xlsx file and save it in a folder. I have been trying to use icg-json-to-xlsx module but till now I have been unable to use it. My code looks like this:

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

Getting 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 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);
}
)};

You can try Alasql JavaScript SQL library. It includes a module to work with JSON and XLSX files (with support of js-xlsx.js library).

Install these two libraries into your project.

npm install alasql
npm install xlsx

Then call alasql function:

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]);

See more examples in this demo file .

There are a lot of modules that can do it. But if you want to control the formatting of xlsx file, then I suggest you use this below code. Rows contain data in the form of JSON array.

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
    }]
);

Try this

  outputFile = jsonXlsx.writeFile(filename, result);

jsonXlsx is object, which contains methods like writeFile, writeBuffer, so you can't call jsonXlsx as function... or you need add reference to 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) {
    //
  });

In your case, query returns MongooseDocuments, but jsonXlsx needs plain JavaScript objects, so that's why you should use lean()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM