excel导出所有数据

119 阅读1分钟

excel导出表格所有数据

注意:我需要导出的数据是后端直接提供文档流格式,并非JSON,从浏览器里查看大概是这种:

微信截图_20220625155755.png responseType: 'blob'加在接口api里。。。。。。。

  
    exportCli() {
      this.$confirm('此操作将导出所有数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        //{ ...this.exportpage, condition: this.condition }导出所有的携带查询条件参数和分页
        const res =await monitorDownloadExportApi.api({ ...this.exportpage, condition: this.condition })
        console.log(res, '文件流')
    
        const blob = new Blob([res], {
          type: 'application/vnd.ms-excel;charset=UTF-8'
           // type: 'application/zip'  type类型下载zip
        })
        let link = document.createElement('a')
        link.href = URL.createObjectURL(blob)
        link.setAttribute(
          'download',
          `下载日志数据.xls`
        )
        link.click()
        link = null
        //或者页可以用这种方法
        // let url = window.URL.createObjectURL(new Blob([res], { type: '.xlsx' }))
        // const a = document.createElement('a')
        // a.style.display = 'none'
        // a.href = url
        // a.setAttribute('download', `下载日志数据.xlsx`)
        // document.body.appendChild(a)
        // a.click()
        // url = window.URL.revokeObjectURL(url)
        // document.body.removeChild(a)
        // this.exportLoading = false
        //
        this.$notify({
          title: '成功',
          message: '导出成功',
          type: 'success'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消导出'
        })
      })
    },

不需要掉接口导出text文件

    downLoadLogFile() {
      const domObj = document.createElement('a')
      domObj.setAttribute('href', 'data:text/json;charset=utf-8,' + encodeURIComponent(this.logData.xxlJobInfo.lastExecLog)) // 注:如存储数组 or JSON需将其转换为JSON字符串
      domObj.setAttribute('download', `${this.logData.taskEntity.name}日志详情.txt`)
      if (document.createEvent) {
        const event = document.createEvent('MouseEvents')
        event.initEvent('click', true, true)
        domObj.dispatchEvent(event)
      } else {
        domObj.click()
      }
    },