【问题标题】:Convert Json into Xlsx File将 Json 转换为 Xlsx 文件
【发布时间】:2015-02-01 22:38:06
【问题描述】:

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

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

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

但我收到了这个错误

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

从 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(err) {
        return callback(err);
    } else {
        if (!file) {
            return callback('file not found');
        }
    }

    callback(null, file);
}
)};

【问题讨论】:

    标签: json node.js excel file


    【解决方案1】:

    试试这个

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

    更新

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

    在您的情况下,查询返回 MongooseDocuments,但 jsonXlsx 需要纯 JavaScript 对象,因此您应该使用 lean()

    【讨论】:

      【解决方案2】:

      你可以试试Alasql JavaScript SQL 库。它包括一个处理 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]);
      

      demo file 中查看更多示例。

      【讨论】:

        【解决方案3】:

        有很多模块可以做到这一点。但是如果你想控制 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
            }]
        );
        

        【讨论】:

          猜你喜欢
          • 2023-01-19
          • 2015-07-04
          • 1970-01-01
          • 1970-01-01
          • 2018-07-22
          • 2018-04-24
          • 2021-01-27
          • 2013-05-19
          • 1970-01-01
          相关资源
          最近更新 更多