<button @click="fun>下载</button>
首先将数据转换为excel表格:
//变成excel表格
const getExcel = (tableRef, excelTitle) => {
const wb = utils.table_to_book(tableRef, { raw: true });
const wbout = write(wb, { bookType: "xlsx", bookSST: true, type: "array" });
return new File([wbout], excelTitle + ".xlsx");
};
点击事件:
const fun(){
const tableRef = document.getElementById("ContentTable"); //获取到列表数据(我用的是id获取的)
const url = window.URL.createObjectURL(getExcel(tableRef, `excelTitle`)); //上面声明的函数
const link = document.createElement("a");
let fname = "薪资管理.xls"; //表格名称
link.href = url;
link.setAttribute("download", fname);
document.body.appendChild(link);
link.click();
}
主要代码:
const url = window.URL.createObjectURL(getExcel(tableRef, `excelTitle`)); //上面声明的函数
const link = document.createElement("a");
let fname = "薪资管理.xls"; //表格名称
link.href = url;
link.setAttribute("download", fname);
document.body.appendChild(link);
link.click();