4

I have an array of JSON objects, like this:

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

when I use the code below, it's not working:

     _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: "" });
                },
            },

I'm using SheetJS version 0.9.11, just wanted to check if there is any way to export it into Excel format? I'm stuck here for the last 2 days. Any help would be highly appreciated.

Max
  • 1,051
  • 1
  • 12
  • 20
Mavericks
  • 253
  • 1
  • 6
  • 20

1 Answers1

3

I think you have to use XLSX.writeFile(wb, 'book.xlsx') for export Excel file.

Replace this code:

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

Here is the reference:https://lovemewithoutall.github.io/it/json-to-excel/

NullPointer
  • 6,137
  • 3
  • 22
  • 38
  • i changed it but i'm getting an error XLSX.utils.json_to_sheet is not a function, i'm using the version sheetjs 0.9.11 – Mavericks Aug 24 '18 at 11:57
  • i finally solved it by upgrading it to 0.10.1 version, the methods json_to_sheet was not present there, – Mavericks Aug 27 '18 at 06:32