共计 1130 个字符,预计需要花费 3 分钟才能阅读完成。
import axios from ‘axios’
/\*\*
\* 封装导出 Excal 文件请求
\* @param url
\* @param data
\* @param excelName // excal 名称
\* @returns {Promise}
\*/
export function exportExcel(url,options,excelName) {return new Promise((resolve, reject) => {axios.defaults.headers\['content-type'\] = 'application/json;charset=UTF-8'
axios({
method: 'post',
url: url, // 请求地址
data: options, // 参数
responseType: 'blob' // 表明返回服务器返回的数据类型
}).then(
response => {resolve(response.data)
let blob = new Blob(\[response.data\], {type: 'application/vnd.ms-excel'})
// console.log(blob)
let date = new Date()
let newTime = date.getFullYear() + '-' +(date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-' +date.getDate()
let fileName = excelName + newTime + '.xlsx'
if (window.navigator.msSaveOrOpenBlob) {navigator.msSaveBlob(blob, fileName)
} else {var link = document.createElement('a')
link.href = window.URL.createObjectURL(blob)
link.download = fileName
link.click()
// 释放内存
window.URL.revokeObjectURL(link.href)
}
},
err => {reject(err)
}
)
})
}
1.dom 引用
import {exportExcel} from '@/utils/fileDown.js'
2. 方法调用
调用 exportExcel(url,params,'文件名称')
正文完