前端导出excel表格

在request.js里面

// 通用下载方法

export function download2(url, params, filename) {

  downloadLoadingInstance = Loading.service({ text: "正在下载数据,请稍候", spinner: "el-icon-loading", background: "rgba(0, 0, 0, 0.7)", })

  return service.post(url, params, {

    transformRequest: [(params) => {

      return JSON.stringify(params)

      }],

    // headers: { 'Content-Type': 'application/json' },

    responseType: 'blob'

  }).then(async (data) => {

    const isLogin = await blobValidate(data);

    if (isLogin) {

      const blob = new Blob([data])

      saveAs(blob, filename)

    } else {

      const resText = await data.text();

      const rspObj = JSON.parse(resText);

      const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode['default']

      Message.error(errMsg);

    }

    downloadLoadingInstance.close();

  }).catch((r) => {

    console.error(r);

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

    downloadLoadingInstance.close();

  })

}

在main.js里面全局挂载

import { download2 } from '@/utils/request'

Vue.prototype.download2 = download2

//点击按钮的方法
 dwdyguw(){//导出离线设备

    this.download2(

        "http://xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",   //接口

        {

          ...this.queryParams

        },

        `post_${new Date().getTime()}.xlsx`                          //post请求   后缀名是.xlsx

      );

    },

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

推荐阅读更多精彩内容