Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
858 views
in Technique[技术] by (71.8m points)

node.js - 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);
}
)};
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...