vue下载excel
作者:互联网
方法一:后台传入
http({ url:'/statistical/exportPerson', method:'post', responseType:'blob', params:{ name:this.name, idCardNumber:this.idCardNumber, } }).then(res => { const fileName = '名单.xls' let blob = new Blob([res.data], { type: "application/octet-stream", }); if ('download' in document.createElement('a')) { const link = document.createElement('a') link.download = fileName link.style.display = 'none' link.href = URL.createObjectURL(blob) document.body.appendChild(link) link.click() // 执行下载 URL.revokeObjectURL(link.href) // 释放url document.body.removeChild(link) // 释放标签 } else { // 其他浏览器 navigator.msSaveBlob(blob, fileName) } }) 方法二:前台做导出 // 引入导出Excel表格依赖 import FileSaver from "file-saver"; import XLSX from "xlsx"; exportExcel () { var xlsxParam = { raw: true } // 导出的内容只做解析,不进行格式转换,保证时间格式或者较长数字不会出现科学计数法。(这个很关键) var wb = XLSX.utils.table_to_book(document.querySelector('#exportTab'), xlsxParam) var 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 },标签:wbout,vue,excel,fileName,link,blob,var,document,下载 来源: https://www.cnblogs.com/brillant/p/16326504.html