Vue == 导出excel

☆樱花仙子☆ 提交于 2019-12-05 20:35:14
<el-form-item>
    <el-button @click="exportHandle()" type="success">导出</el-button>
</el-form-item>

<script>import axios from 'axios'
export default {
methods:{
    exportHandle() {
      axios({
          headers: {
            'Content-Type':'application/json',
            token: Cookies.get('tokensp')
          },
          responseType: 'blob', // 一定要写
          method: 'get',
          url:`${window.SITE_CONFIG['forumURL']}/productequipment/pmequipment/exportBackgroundEquipments`,
          params: {
            "json": {
                    token: Cookies.get("token"),
                    customerName:this.dataForm.customerName,
                    businessOfficeId:this.dataForm.officeId,
                    createTimeStart:'',
                    createTimeEnd:''
            }
          }
      })
        .then(res => {
          let blob = new Blob([res.data], {type: 'application/vnd.ms-excel'})
          let date = new Date();
          let month = date.getMonth() + 1;
          let day = date.getDate();
          let fileName = '设备管理信息' + date.getFullYear() + (month >= 10 ? month : '0' + month) + (day >= 10 ? day : '0' + day) // 下载的文件名
          if ('msSaveOrOpenBlob' in navigator){
            // 'IE'
            window.navigator.msSaveOrOpenBlob(blob, fileName + '.xls')
          } else {
            const link = document.createElement('a')
            link.style.display = 'none'
            link.href = URL.createObjectURL(blob)
            link.download = res.headers['content-disposition'] // 下载后文件名
            link.download = fileName
            document.body.appendChild(link)
            link.click()
            document.body.removeChild(link)
          }
        })
        .catch(err => {
          // console.log(err)
        })
      }
}
}

</script>

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!