前端post请求实现导出excel表格

很多后台管理项目有导出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表格

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容