vue导出Excel表格

2021-11-30  本文已影响0人  安北分享

导出表格数据

        // 导出全部
        exportExcel(req).then(res => {
          const downloadName = '会议服务记录 ' + '_' + this.getDate()
          const blobData = new Blob([res], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
          const link = document.createElement('a')
          link.style.display = 'none'
          link.href = window.URL.createObjectURL(blobData)
          link.setAttribute('download', downloadName)
          document.body.appendChild(link)
          link.click()
          link.remove()
        }).catch(err => {
          console.log(err)
        })

也可以封装一下下载方法:

    // 文件流下载
    fileDownload (data, fileName) {
      const blob = new Blob([data])
      // 对象URL也称为bolbUrl,指的是引用保存在File或Bolb中数据的URL
      const blobUrl = window.URL.createObjectURL(blob) // 创建URl对象
      let a = document.createElement('a')
      a.href = blobUrl
      if (fileName) {
        a.download = fileName // 命名下载文件名字
      } else {
        a.setAttribute('download', '')
      }
      document.body.appendChild(a) // 兼容FireFox
      a.click()
      window.URL.revokeObjectURL(blobUrl)
      a.remove()
    },
    templateDownload () {
      let params = {
        responseType: 'blob'
      }
      templateDownload(params).then((res) => {
        this.fileDownload(res.data, '用户信息模板.xls')
      }).catch((e) => {
      })
    },
上一篇下一篇

猜你喜欢

热点阅读