共计 622 个字符,预计需要花费 2 分钟才能阅读完成。
装置相干依赖
次要是两个依赖:(xlsx 和 file-saver)
npm install –save xlsx file-saver
插件应用
https://github.com/SheetJS/js…
https://github.com/eligrey/Fi…
放到组件头里引入
import FileSaver from ‘file-saver’
import XLSX from ‘xlsx’
调用
xlsxTable 是 table 的 DOM 节点
exportExcel () {
/* generate workbook object from table */
let wb = XLSX.utils.table_to_book(document.querySelector('#xlsxTable'));
/* get binary string as output */
let wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array'});
try {FileSaver.saveAs(new Blob([wbout], {type: 'application/octet-stream'}), '下载文件名.xlsx');
} catch (e)
{if (typeof console !== 'undefined')
console.log(e, wbout)
}
return wbout
},
参考文章:
https://www.jianshu.com/p/6ed…
正文完