• xlsx模板下载


    有点时候需要通过前端下载xlsx文档,具体代码实现如下:

    // html代码
    
          下载模板
    
    //js 代码
    // 定义接口
    export function genCclOptionXlsxApi(params) {
      return axios({
        url: api.genCclOptionXlsxUrl,
        method: 'get',
        responseType: 'blob',  // 注意前端需要添加这个
        params
      })
    }
    // 引入接口
    import { genCclOptionXlsxApi } from '@/api/vcp'
    // 下载xlsx模板方法
    downloadTemplateFuc() {
     console.log('vcp--下载模板')
     let paramsResponse = {
       nodeVersionNo:
         this.getTimeDetails?.nodeVersion ||
         this.form.cclVersion.match(regex)[1]
     }
     genCclOptionXlsxApi(paramsResponse)
       .then((res) => {
         const fileName =
           'CCP Option-V' +
           `${
             this.getTimeDetails?.nodeVersion ||
             this.form.cclVersion.match(regex)[1]
           }` +
           '_' +
           this.dateFormat('yyyyMMdd') +
           '.xlsx'
         const url = window.URL.createObjectURL(new Blob([res]))
         const link = document.createElement('a')
         link.style.display = 'none'
         link.href = url
         link.setAttribute('download', fileName)
         document.body.appendChild(link)
         link.click()
         URL.revokeObjectURL(link.href)
         document.body.removeChild(link)
         message.success('请求下载模板成功,请保存')
       })
       .catch((error) => {
         console.log('error:', error)
         message.error(error.error.data.errorMsg || '请求失败')
       })
    },
    dateFormat(fmt) {
       var o = {
         'M+': new Date().getMonth() + 1, // 月份
         'd+': new Date().getDate(), // 日
         'h+': new Date().getHours(), // 小时
         'm+': new Date().getMinutes(), // 分
         's+': new Date().getSeconds(), // 秒
         'q+': Math.floor((new Date().getMonth() + 3) / 3), // 季度
         S: new Date().getMilliseconds()
         // 毫秒
       }
       if (/(y+)/.test(fmt))
         fmt = fmt.replace(
           RegExp.$1,
           (new Date().getFullYear() + '').substr(4 - RegExp.$1.length)
         )
       for (var k in o)
         if (new RegExp('(' + k + ')').test(fmt))
           fmt = fmt.replace(
             RegExp.$1,
             RegExp.$1.length == 1
               ? o[k]
               : ('00' + o[k]).substr(('' + o[k]).length)
           )
       return fmt
    },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
  • 相关阅读:
    MYSQL快速从另外一张表中更新数据
    CYarp:力压frp的C#高性能http内网反代中间件
    A. Short Sort
    类型多样的石膏PBR多通道贴图素材,速来收藏!
    python基础-3(文件操作)
    香港服务器适合的网站,香港服务器优势是什么?
    libevent源码跨平台编译(windows/macos/linux)
    精彩回顾|关系网络赋能银行数字化转型的应用与实践
    02 C++ 变量和基本类型
    MyBatis-Plus(二)- 进阶使用
  • 原文地址:https://blog.csdn.net/qq_37148353/article/details/133856878