How to export json object into excel using javascr

2019-07-31 17:46发布

问题:

I wanted to export my json object into excel file. I have searched in Google and tried the following, but I am unable to get or export my object data into excel file on clicking of button, it is downloading but without any columns/data(i.e empty file is downloading).

I am not sure what is the problem, Please someone help me regarding this. Thanks in advance !

Created Plnkr.

html:

<div id="dvjson"></div>
      <br><button type="button" id='DLtoExcel'>
      Download CSV
      </button>

js:

$(document).ready(function(){


//if I give below json object, file is downloading with the columns/data and hence it is fine
//var testjsondata = [{"number":123}];

//if I give like below object, empty file is downloading, not having any data
//it is not working //it should also work

//var testjsondata = {"number": 123}//it is not working //it should also work

//and the following object format also should work

var testjsondata = {
    "test": {
        "name": "abc",
        "address": [{
            "number": "12345",
            "street": "xyz"
        }]

    },
    "mynumber": 12
};


var $btnDLtoExcel = $('#DLtoExcel');
    $btnDLtoExcel.on('click', function () {
        $("#dvjson").excelexportjs({
                    containerid: "dvjson"
                       , datatype: 'json'
                       , dataset: testjsondata
                       , columns: getColumns(testjsondata)     
                });

    });
console.log(testjsondata);
});

Any other solutions or libraries also welcome, my json object is plain type, not any array type.