Convert Json into Xlsx File

后端 未结 3 1221
自闭症患者
自闭症患者 2021-01-18 18:18

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

相关标签:
3条回答
  • 2021-01-18 18:37

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

    0 讨论(0)
  • 2021-01-18 18:39

    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
        }]
    );
    
    0 讨论(0)
  • 2021-01-18 18:59

    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.

    0 讨论(0)
提交回复
热议问题