h5 json 生成excel

引入库文件

创建下载按钮

实现转换

var data = [
["Joa Doe", "joa@doe.com"],
["Job Doe", "job@doe.com"],
["Joe Doe", "joe@doe.com"],
["Jon Doe", "jon@doe.com"],
["Joy Doe", "joy@doe.com"]
];
var workbook = XLSX.utils.book_new(),
worksheet = XLSX.utils.aoa_to_sheet(data);
workbook.SheetNames.push("First");
workbook.Sheets["First"] = worksheet;
res = XLSX.writeFile(workbook, "demo.xlsx");

下载文件

方式一: 库文件
window.saveAs(new Blob([wbout], { type: "application/octet-stream" }));

方式二: a 标签
onst link = document.createElement('a');
let blob = new Blob([res], {type: 'application/vnd.ms-excel'});
link.style.display = 'none';
link.href = URL.createObjectURL(blob);
link.download = "demo.xlsx";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);

posted @ 2023-02-21 11:47  vx_guanchaoguo0  阅读(32)  评论(0编辑  收藏  举报