1

import axios from 'axios'

/\*\*
 \* 封装导出Excel文件请求
 \* @param url
 \* @param data
 \* @param excelName  // excal名称
 \* @returns {Promise}
 \*/
export function exportExcel(url,options,excelName) {
  return new Promise((resolve, reject) => {
    axios.defaults.headers\['content-type'\] = 'application/json;charset=UTF-8'
    axios({
      method: 'post',
      url: url, // 请求地址
      data: options, // 参数
      responseType: 'blob' // 表明返回服务器返回的数据类型
    }).then(
      response => {
        resolve(response.data)
        let blob = new Blob(\[response.data\], {
          type: 'application/vnd.ms-excel'
        })
        // console.log(blob)
        let date = new Date()
        let newTime = date.getFullYear() + '-' +(date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-' +date.getDate()
        let fileName = excelName + newTime + '.xlsx'
        if (window.navigator.msSaveOrOpenBlob) {
          navigator.msSaveBlob(blob, fileName)
        } else {
          var link = document.createElement('a')
          link.href = window.URL.createObjectURL(blob)
          link.download = fileName
          link.click()
          //释放内存
          window.URL.revokeObjectURL(link.href)
        }
      },
      err => {
        reject(err)
      }
    )
  })
}

 1.dom引用

import { exportExcel } from '@/utils/fileDown.js'

2.方法调用

调用exportExcel(url,params,'文件名称')

lfaith
44 声望3 粉丝