vue 导出excel表格


import { ACCESS_TOKEN } from '@/store/mutation-types'


// 导出表格 // tableList:表格中的数据 // params:传入后端接口的参数 // api:后端接口的地址 // tableTitle:导出文件的名字 export const exportTable = (tableList, params, api, tableTitle) => { if (tableList.length > 0) { notification.success({ message: '成功', description: '正在导出列表,请稍等', duration: 1 }) axios({ method: 'post', url: api, headers: { 'Content-Type': 'application/json;charset=UTF-8', Authorization: 'Bearer ' + Vue.ls.get(ACCESS_TOKEN)//Vue.ls.get(ACCESS_TOKEN)获得token, }, responseType: 'arraybuffer',//改成这个值,导出的excel表格不会有数据损坏 data: params // data是添加到请求体(body)中的, 用于post请求。 }).then(res => { if (res.status === 200) { const link = document.createElement('a') const blob = new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }) link.style.display = 'none' link.href = window.URL.createObjectURL(blob)//创建连接 link.download = tableTitle + '.xlsx'//将下载的excel表格命名 document.body.appendChild(link) link.click() document.body.removeChild(link) notification.success({ message: '成功', description: '导出成功', duration: 3 }) } }).catch(error => { notification.error({ message: '失败', description: '导出失败', duration: 3 }) }) } else { notification.error({ message: '失败', description: '表格中没有数据无法导出', duration: 3 }) } }
原文地址:https://www.cnblogs.com/lvlvlv/p/11714436.html