因为数据绑定的原因没有及时响应,所以原来的方法不能用了
var fix = document.querySelector('.el-table__fixed');
// 因为element-ui的表格的fixed属性导致多出一个table,会下载重复内容,这里删除掉
var wb;
if (fix) {
wb = XLSX.utils.table_to_book(document.querySelector('#multipleTable').removeChild(fix))
document.querySelector('#multipleTable').appendChild(fix)
} else {
wb = XLSX.utils.table_to_book(document.querySelector('#multipleTable'))
}
var wbout = XLSX.write(wb, {bookType: "xlsx", bookSST: true, type: "array" });
try {
FileSaver.saveAs(
new Blob([wbout], { type: "application/octet-stream" }),
`${this.$route.query.title}.xlsx`
);
} catch (e) {
if (typeof console !== "undefined") console.log(e, wbout);
}
return wbout;
能用手动拼接的方法,注意[tableHeader],这个必须是二位数组
const tableHeader = ["序号", "指标", "普查办",'单位','指标说明'];
const tableData = [tableHeader, ...this.tableData.map(item => [item.xh, item.zb, item.value,item.dw,item.zbsm])];
const worksheet = XLSX.utils.sheet_add_aoa(XLSX.utils.aoa_to_sheet(tableData), [tableHeader], { origin: "A1" });
const workbook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(workbook, worksheet, "Sheet1");
const buffer = XLSX.write(workbook, { bookType: "xlsx", type: "array" });
const blob = new Blob([buffer], { type: "application/octet-stream" });
FileSaver.saveAs(blob, `${this.$route.query.title}.xlsx`);