欧巴酱 2019-07-30 15:46:46 1469 收藏
版权
使用前都需导入 xlsx.core.js 等系列组件;
1.使用table_to_sheet():
var ws = XLSX.utils.table_to_sheet(document.getElementById("conTable"));
var wb = new XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "xx1");
XLSX.writeFile(wb, "sheet.xlsx");
2.使用json_to_sheet():
var data = JSON.parse(result.data);
var ws = XLSX.utils.json_to_sheet(data);
var wb = new XLSX.utils.book_new();
const wopts = { bookType: 'xlsx', bookSST: false, type: 'binary' };
downloadExl(data);
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;
}
function saveAs(obj, fileName) {//自定义下载文件方式
var tmpa = document.createElement("a");
tmpa.download = fileName || "下载";
tmpa.href = URL.createObjectURL(obj); //绑定a标签
tmpa.click(); //模拟点击实现下载
setTimeout(function () { //延时释放
URL.revokeObjectURL(obj); //用URL.revokeObjectURL()来释放这个object URL
}, 100);
}
function downloadExl(data) {
const wb = { SheetNames: ['Sheet1'], Sheets: {}, Props: {} };
wb.Sheets['Sheet1'] = XLSX.utils.json_to_sheet(data);//通过json_to_sheet转成单页(Sheet)数据
saveAs(new Blob([s2ab(XLSX.write(wb, wopts))], { type: "application/octet-stream" }), "这里是下载的文件名" + '.' + (wopts.bookType == "biff2" ? "xls" : wopts.bookType));
————————————————
版权声明:本文为CSDN博主「欧巴酱」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_36894527/article/details/97788979