I am using SheetJS library to convert Excel to JSON and vice versa. While the code to convert Excel to JSON works fine, there are issues with the code to convert JSON back to Excel. Please find the code below:
var wb = XLSX.utils.book_new();
wb.Props = {
Title: "SheetJS Tutorial",
Subject: "Test",
Author: "Red Stapler",
CreatedDate: new Date(2017, 12, 19)
};
wb.SheetNames.push("Test Sheet");
var ws_data = [['hello', 'world']];
var ws = XLSX.utils.aoa_to_sheet(ws_data);
wb.Sheets["Test Sheet"] = ws;
var wbout = XLSX.write(wb, { bookType: 'xlsx', type: 'binary' });
function s2ab(s) {
var buf = new ArrayBuffer(s.length);
var view = new Uint8Array(buf);
for (var i = 0; i < s.length; i++) view[i] = s.charCodeAt(i) & 0xFF;
return buf;
}
saveAs(new Blob([s2ab(wbout)], { type: "application/octet-stream" }), 'test.xlsx');
The above code keeps giving XLSX.utils.book_new() is not a function error. Has anybody come across similar issue ?
Thanks