import { parseTime } from '@/utils' /** * @description 导出excel * @param {*} ctx * @param {*} api */ export const exportExcel = async (ctx, api, data = {}, fileName = 'excel数据') => { try { ctx.isExportLoading = true const res = await api(data) if (!res) { return ctx.$message.error('没有导出数据') } fileName += ' - ' + parseTime(new Date().getTime(), '{y}-{m}-{d}') + '.xlsx' const blob = new Blob([res]) if ('download' in document.createElement('a')) { // 非IE下载 const elink = document.createElement('a') elink.download = fileName elink.style.display = 'none' elink.href = URL.createObjectURL(blob) document.body.appendChild(elink) elink.click() URL.revokeObjectURL(elink.href) // 释放URL 对象 document.body.removeChild(elink) } else { // IE10+下载 navigator.msSaveBlob(blob, fileName) } } catch (error) { } finally { ctx.isExportLoading = false } }