使用Sheetjs将JSON对象数组导出到Excel

时间:2018-08-24 10:51:59

标签: javascript excel vue.js sheetjs

我有JSONObjects数组,像这样

  [
    {"name":"John", "city": "Seattle"},
    {"name":"Mike", "city": "Los Angeles"},
    {"name":"Zach", "city": "New York"}
]

当我使用下面给出的代码时,它不起作用

 _export: function(records,opts) {
                var XLSX = xlsx;
                var fileName = opts.split(".")[2]
                var workSheet = XLSX.utils.json_to_sheet(records);
                console.log("THis is Worksheet",workSheet);
                var wb = XLSX.utils.book_new();
                console.log("THis is workbook",wb)
                XLSX.utils.book_append_sheet(wb, workSheet, fileName);

                var bin = XLSX.write(wb, {bookType:'xlsx',type: "binary"});
                return new Blob([this._binStr2ArrBuff(bin)], { type: "" });
            },
        },

我使用的是sheetjs 0.9.11版本,只是想检查一下将它们导出到excel的任何方法,我从最近两天就被困在这里。 任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

我认为您必须使用XLSX.writeFile(wb, 'book.xlsx')来导出Excel文件。

替换此代码:

var bin = XLSX.write(wb, {bookType:'xlsx',type: "binary"});
return new Blob([this._binStr2ArrBuff(bin)], { type: "" });

这里是参考:https://lovemewithoutall.github.io/it/json-to-excel/