Sheetjs: saveAs is not defined.

Created on 31 Jan 2018  路  1Comment  路  Source: SheetJS/sheetjs

I wrote as in demo,but result in saveAs is not defined.

var data = [
{ name: "Barack Obama", pres: 44 },
{ name: "Donald Trump", pres: 45 }
];

/* generate a worksheet */
var ws = XLSX.utils.json_to_sheet(data);

/* add to workbook */
var wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "Presidents");

/* write workbook (use type 'array' for ArrayBuffer) */
var wbout = XLSX.write(wb, {bookType:'xlsx', type:'array'});

/* generate a download */
saveAs(new Blob([wbout],{type:"application/octet-stream"}), "sheetjs.xlsx");

Most helpful comment

saveAs is currently provided by the FileSaver shim: https://unpkg.com/[email protected]/FileSaver.min.js . You can also manually generate a download using URL.createObjectURL and clicking a link. See https://jsfiddle.net/ve9g38kv/

/* create object URL */
var blob = new Blob([wbout],{type:"application/octet-stream"});
var url = URL.createObjectURL(blob);

/* create new link and add to document */
var link = document.createElement("a");
link.download = "sheetjs.xlsx";
link.href = url;
document.body.appendChild(link);

/* click the link */
link.click();

/* cleanup */
document.body.removeChild(link);
URL.revokeObjectURL(url);

>All comments

saveAs is currently provided by the FileSaver shim: https://unpkg.com/[email protected]/FileSaver.min.js . You can also manually generate a download using URL.createObjectURL and clicking a link. See https://jsfiddle.net/ve9g38kv/

/* create object URL */
var blob = new Blob([wbout],{type:"application/octet-stream"});
var url = URL.createObjectURL(blob);

/* create new link and add to document */
var link = document.createElement("a");
link.download = "sheetjs.xlsx";
link.href = url;
document.body.appendChild(link);

/* click the link */
link.click();

/* cleanup */
document.body.removeChild(link);
URL.revokeObjectURL(url);
Was this page helpful?
0 / 5 - 0 ratings

Related issues

seanmcilvenna picture seanmcilvenna  路  3Comments

jamespan0 picture jamespan0  路  3Comments

magtuan picture magtuan  路  3Comments

gustavosimil picture gustavosimil  路  3Comments

m-ketan picture m-ketan  路  3Comments