vue excel 导出功能, 亲测可用 简单明了

话不多说直接上代码, 亲测可用!!!

1. 安装 相关依赖   

npm install --save xlsx file-saver

2. 在 units 文件夹中 新建excel_export.js // 我是这样做的


excel_export.js 中 如下: 



import FileSaver from 'file-saver'
import XLSX from 'xlsx'


    exportExcel (tableName = 'sheet') {
         /* generate workbook object from table */
         var wb = XLSX.utils.table_to_book(document.querySelector('.table'))  // 传个id 或者.类名 
         /* get binary string as output */
         var wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' })
         try {
             FileSaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }),                                          tableName + '.xlsx')
         } catch (e) { if (typeof console !== 'undefined') console.log(e, wbout) }
         return wbout
     }



然后绑定到原型上

在.vue文件中使用

 欢迎评论!

扫描二维码关注公众号,回复: 5117952 查看本文章

猜你喜欢

转载自blog.csdn.net/qq_40808779/article/details/86647648