说明:在工作中遇到这个需求,正好在网上看到方法,自己记录下来供以后参考。
原博点这里
//引入两个依赖
import XLSX from 'xlsx'
import fileSave from 'file-saver'
//挂载到vue原型上,参数id为表格的id,参数tablename为打印出来的表格名字
Vue.prototype.exportExcel = function (id, tablename) {
let et = XLSX.utils.table_to_book(document.getElementById(id))
let etout = XLSX.write(et, {
bookType: 'xlsx',
bookSST: true,
type: 'array'
})
try {
fileSave.saveAs(new Blob([etout], {
type: 'application/octet-stream'
}), tablename + '.xlsx')
} catch (e) {
console.log(e, etout)
}
return etout
}