借鑒:https://www.jianshu.com/p/1971fc5b97ca
https://blog.csdn.net/qq_40614207/article/details/94003793
貼出代碼
// 定義導出Excel表格事件
exportExcel() {
// 解決生成重復數據-因為使用l fixed屬性
var fix = document.querySelector('.el-table__fixed')
var wb
// 判斷要導出的節點中是否有fixed的表格,如果有,轉換excel時先將該dom移除,然后append回去
if (fix) {
/* 從表生成工作簿對象 */
wb = XLSX.utils.table_to_book(document.querySelector('#out-table').removeChild(fix))
document.querySelector('#out-table').appendChild(fix)
} else {
wb = XLSX.utils.table_to_book(document.querySelector('#out-table'))
}
/* 獲取二進制字符串作為輸出 */
var wbout = XLSX.write(wb, {
bookType: 'xlsx',
bookSST: true,
type: 'array'
})
try {
FileSaver.saveAs(
// Blob 對象表示一個不可變、原始數據的類文件對象。
// Blob 表示的不一定是JavaScript原生格式的數據。
// File 接口基於Blob,繼承了 blob 的功能並將其擴展使其支持用戶系統上的文件。
// 返回一個新創建的 Blob 對象,其內容由參數中給定的數組串聯組成。
new Blob([wbout], { type: 'application/octet-stream' }),
// 設置導出文件名稱
'sheetjs.xlsx'
)
} catch (e) {
if (typeof console !== 'undefined') console.log(e, wbout)
}
return wbout
},
