一。导出代码。
exportExecl () {
let url = '/export'
axios({
method: 'get',
url: url,
responseType: 'blob'
}).then(response => {
this.download(response)
}).catch((error) => {
console.log(error)
})
},
download (response ) {
if (!response) {
return
}
// 如果是execl => type: application/vnd.ms-excel
let url = window.URL.createObjectURL(new Blob([response.data], {type: 'text/csv'}))
let link = document.createElement('a')
link.style.display = 'none'
link.href = url
// 如果是execl => execl.xlsx
link.setAttribute('download', 'excel.csv')
document.body.appendChild(link)
link.click()
},
二。说明
上面代码还需要引入 axios
上面实例是导出的cvs。如果出现中文乱码,则需要后端返回blob流时设置好对应的编码。
如果要导出execl 则需要修改new Blob的 类型 {type: 'application/vnd.ms-excel'}.同时修改导出的文件名