5

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.

1 Answer 1

7

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/

Sign up to request clarification or add additional context in comments.

2 Comments

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
i finally solved it by upgrading it to 0.10.1 version, the methods json_to_sheet was not present there,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.