I'm trying to encode a JSON object to XLSX and download it using the following code:
this.data = {
foo: "xyz"
}
let json2xls = require('json2xls');
var data = json2xls(this.data);
let blob = new Blob([data], { type: "binary" });
let a = angular.element("a");
a.attr("href", this.$window.URL.createObjectURL(blob));
a.attr("download", "myfile.xlsx");
a[0].click();
Although the file is being created and downloaded, Excel cannot open it.
I have verified that a different method works by sending this.data
to the server, saving it with fs.writeFile()
, and then downloading the file.
var data = json2xls(this.data);
Is there a way to successfully convert from JSON to XLS in a browser environment?