将Json转换为Xlsx文件 [英] Convert Json into Xlsx File

查看:379
本文介绍了将Json转换为Xlsx文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将json数据转换成Xlsx文件并将其保存在一个文件夹中。
我一直在尝试使用icg-json-to-xlsx模块,但到目前为止我一直无法使用它。
我的代码如下所示:

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

filename = path.join('./ files',output.xlsx);
outputFile = jsonXlsx(filename,result)// result包含json数据
console.log(outputFile);

但是我收到这个错误

  outputFile = jsonXlsx(filename,result)
^
TypeError:对象#的属性'jsonXlsx'不是函数
/ pre>

从mongodb获取数据:路由中的

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



  FileController.prototype.getAll = function(callback) {

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

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

回调(null,file);
}
)};


解决方案

尝试这个

  outputFile = jsonXlsx.writeFile(filename,result); 

jsonXlsx是对象,它包含像writeFile,writeBuffer这样的方法,所以你不能调用jsonXlsx作为函数...或者您需要添加对这样的函数的引用

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

示例

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

更新



文件
.find({})
.select({
_id:false,id:true,name:true,status:true
})
.lean()
.exec(function(err,file){
//
});在您的情况下,查询返回MongooseDocuments,但 jsonXlsx 需要纯Java对象,所以您应该使用 lean()


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

解决方案

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()

这篇关于将Json转换为Xlsx文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆