vue多种文件格式导出方法

/**
        * 导出Excel文件
        * 参数说明:
        * option: 字段解析
        * data: <Object> blob数据
        * link: <String> 下载链接
        * time: <Boolean> 文件名加时间,默认true,
        * title: <String> 文件名
        * bool: <Boolean> 是否自定义文件名
        * suffix: <String> 设置后缀名
    */
Vue.prototype.importExcelFunc = (option) => {
    
    
        console.log(option)
        let url;
        if(option.data){
    
    
            url = window.URL.createObjectURL(option.data);
        }
        if(option.link){
    
    
            url = option.link
        }
        let title = "";
        if(option.title){
    
    
            title += option.title
        }
        if(!option.time){
    
    
            title += Vue.prototype.dataTransform(new Date(), "yyyyMMddHHmmss")
        }
        let link = document.createElement("a");
        if(option.bool){
    
    
            link.setAttribute("download", `${
      
      title}.${
      
      option.suffix || 'xlsx'}`)
        }
        link.href = url;
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link)
        if(option.fn){
    
    
            option.fn()
        }
    }
    **使用方式**
     <button class="btn icon redius iconfont icon-add" @click="exportList">导出列表</button>
     exportList(){
    
    
            this.searchList = JSON.parse(JSON.stringify(this.searchData))
            this.searchData = this.searchList
            this.searchData.taskId = this.$route.query.taskId
            this.post("/analyse/detail/exportPageList",this.searchData,(data)=>{
    
    
                this.importExcelFunc({
    
    
                    data:data,
                    title:'文件摸板',
                    suffix:'csv',
                    bool:true
                })
            },"",{
    
    bool:true})
        }

猜你喜欢

转载自blog.csdn.net/weixin_43959276/article/details/109495665