1,首先引入 XLSX 和 FileSaver 插件
yarn add xlsx file-saver -S
2,在需要使用的頁(yè)面 import
import FileSaver from 'file-saver'
import XLSX from 'xlsx'
3,出現(xiàn)的問(wèn)題,導(dǎo)出的數(shù)據(jù)會(huì)被自動(dòng)轉(zhuǎn)換等。如下圖 數(shù)據(jù)含有 - 特殊符號(hào)時(shí)數(shù)據(jù)變?yōu)槿掌诟袷?/p>

table格式
<Table id="tab"></Table>
//導(dǎo)出Excel
exportToExcel () {
let self = this;
let et = XLSX.utils.table_to_book(document.getElementById('tab')); //此處傳入table的DOM節(jié)點(diǎn)
let etout = XLSX.write(et, {
bookType: 'xlsx',
bookSST: true,
type: 'array'
});
try {
FileSaver.saveAs(new Blob([etout], {
type: 'application/octet-stream'
}), ' 統(tǒng)計(jì).xlsx'); //統(tǒng)計(jì).xlsx 為導(dǎo)出的文件名
self.$Message.info("導(dǎo)出Excel成功");
} catch (e) {
console.log(e, etout) ;
self.$Message.error("導(dǎo)出Excel失敗,請(qǐng)刷新后重試");
}
return etout;
},

錯(cuò)誤的格式
<Table id="tab"></Table>
//導(dǎo)出Excel
exportToExcel () {
let self = this;
let xlsxParam = { raw: true }; // 轉(zhuǎn)換excel時(shí)使用原始格式
let et = XLSX.utils.table_to_book(document.getElementById('tab'),xlsxParam); //此處傳入table的DOM節(jié)點(diǎn)
let etout = XLSX.write(et, {
bookType: 'xlsx',
bookSST: true,
type: 'array'
});
try {
FileSaver.saveAs(new Blob([etout], {
type: 'application/octet-stream;charset=utf-8'
}), ' 統(tǒng)計(jì).xlsx'); //統(tǒng)計(jì).xlsx 為導(dǎo)出的文件名
self.$Message.info("導(dǎo)出Excel成功");
} catch (e) {
console.log(e, etout) ;
self.$Message.error("導(dǎo)出Excel失敗,請(qǐng)刷新后重試");
}
return etout;
},

正確的格式