vue3 excel 文件导出

//文件导出 在index.ts 中

export function downloadHandle(url: string,params?:object, filename?: string, method: string = 'GET'){

try {

downloadLoadingInstance = ElLoading.service({

text: "正在生成下载数据,请稍候",

background: "rgba(0, 0, 0, 0.7)",

});

return service.get(url, {

responseType: 'blob',

params:params?params:''

}).then(async (resp: any) => {

// 创建a标签

const down = document.createElement('a')

// 文件名没传,则使用时间戳

if (filename) {

down.download = filename

} else {

const downName = resp.headers'content-disposition'.split('=')1

down.download = decodeURI(downName)

}

// 隐藏a标签

down.style.display = 'none'

// 创建下载url

down.href = URL.createObjectURL(

new Blob(resp.data, {

type: resp.data.type

})

)

// 模拟点击下载

document.body.appendChild(down)

down.click()

// 释放URL

URL.revokeObjectURL(down.href)

// 下载完成移除

document.body.removeChild(down)

downloadLoadingInstance.close();

}).catch((r: any) => {

console.error(r);

ElMessage.error('下载文件出现错误,请联系管理员!');

downloadLoadingInstance.close();

});

} catch (err: any) {

ElMessage.error(err.message)

}

}

在页面中 引入

import {downloadHandle} from "../../api/index";

const downloadFun = () => {

downloadHandle("/bg/customer/accountQuantity/exportExcelTemplate");

};

相关推荐
计算机魔术师1 分钟前
持股2%却要掌舵50.1%:Anthropic上市前的控制权保卫战
前端
Blanche15002 分钟前
Webpack 构建流程解析
javascript·面试
粥里有勺糖3 分钟前
体验一下最近比较🔥的闪卡SKILL
前端·github
flash俊杰3 分钟前
WebGL 实时合成管线:纹理、Shader 滤镜链与离屏渲染调度
前端
前端snow3 分钟前
ai agent --- LangSmith 全链路观测
前端
Csvn4 分钟前
Hooks 原理:把"每次重跑的函数"变成"有记忆的组件"
前端
计算机魔术师4 分钟前
用AI拒批老人看病?美国这个试点项目的激励机制出了大问题
前端
风骏时光牛马4 分钟前
AI服务线上响应异常故障
前端
IT_陈寒4 分钟前
JavaScript的this指向问题又让我加了个班
前端·人工智能·后端
PC2005_cloud4 分钟前
Nginx 学习笔记:Server 块配置详解,域名路由与多站点部署实战
前端·后端