Vue-Excel(文档流Excel导出)

<!--
  // 注:此为导出Excel表格说明文件,不用引入,
  1、使用方式:
    引入:
      import commonJs from "@js/common";
    使用:
      commonJs.exportExcel(obj)
    必传字段:
      obj: {
        blob: res // 文档流
      }
    (注:api中需设置 responseType: 'blob',可查看下面示例)
    ————————————————————————————————————————以上几步即可简单使用
  2、参数说明
    obj: {
      blob: '', // 文档流(必)
      title: , // excel文件名称,默认 'excel表格+当前日期'(非)
    }
  3、示例
    全局文件中搜索 @js/common,可查看更多具体示例
    
    示例:

    Api文件::
      xxx: (opts = {}) => { // 产品库设置-列表导出
          return http.get(`xxxx`, {
              
              <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

              responseType: 'blob',(必)******** 亲,看这里就可以

              <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

              query: {
                  ...opts
              }
          })
      },

    vue文件::
      api.xxx({
        ...
      }).then((res) => {
      
      <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<示例

      commonJs.exportExcel({ ************************亲,瞅这里
        blob: res,
        title: '权益卡-使用数据'
      })

      >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

      }).catch(() => {})
-->
// 公共js
const commonJs = {
  // Excel表格导出,小熊
  exportExcel(obj) {
    if (!obj.blob) {
      return false
    }
    const content = obj.blob
    const blob = new Blob([content])
    const fileName = `${obj.title ? obj.title : 'excel表格'}${this.getdate()}.xls`
    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)
    }
    return true
  },
  // 当前时间
  getdate() {
    let date = new Date();
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let strDate = date.getDate();

    if (month >= 1 && month <= 9) {
      month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
      strDate = "0" + strDate;
    }
    let currentDate = year + "." + month + "." + strDate;
    return currentDate;
  }
}
export default commonJs

 

posted @ 2023-11-17 16:31  忙着可爱呀~  阅读(150)  评论(0)    收藏  举报