很多后台管理项目有导出excel表格的需求,这里只描述前端的实现方法。。
1、首先写一个按钮,定义一个点击事件
<el-button
type="primary"
class="goodsindex-queryInfos-li"
size="small"
@click="send"
>导出excel
</el-button>
2、调用点击事件,请求接口,做导出功能
send() {
let data = this.tableData; //传入要导出的表格数据
_analysis.sendecxel(data).then(res => { //此处请求接口
// console.log(res)
const link = document.createElement("a");
const blob = new Blob([res.data], { type: "application/vnd.ms-excel" });
link.style.display = "none";
link.href = URL.createObjectURL(blob);
link.setAttribute("download", `${name}.xlsx`); //报表名称可自定义
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
});
}
3、如果是封装的接口,如下
sendecxel(data){
return new Promise((resolve, reject) => {
axios({
url: '接口路径',
method: "post",
data: data,
responseType: 'blob' //这一行一定要加!!!
}).then((res) => {
return resolve(res)
}).catch(error => {
console.log(error)
})
})
}
这是前端要有的操作,后台也要对接口做处理,方可实现导出excel表格