vue 前端解析excel文件

   //   保存excel数据
   async saveExcelData(){
    
    
   // 没有选择文件,无法解析
        if(this.fileList.length<1) {
    
    
          this.$message.error("你还没有选择文件!!")
          return false
        }

// 选择的文件
      let file = this.uploadFileForm.file;
      // console.log(file)
      if(!file){
    
    
        console.log("文件打开失败")
        return ;
      }else{
    
    
       let data = await this.readFile(file);
       let workbook = XLSX.read(data,{
    
     type: "binary"});//解析二进制格式数据
       // console.log('二进制数据的解析:')
       // console.log(workbook)
       let worksheet = workbook.Sheets[workbook.SheetNames[0]];//获取第一个Sheet
       let result = XLSX.utils.sheet_to_json(worksheet);//json数据格式
       // console.log('最终解析的 json 格式数据:')
       // console.log(result)
       this.excelData.data = result
        if(this.excelData.data.length > 0) {
    
    
          let temHeader = this.excelData.data[0];
          for(const k in temHeader) {
    
    
            // console.log("key名称是:"+k+",key的值是:"+ temHeader[k])
            this.excelData.header.push(
            {
    
    "label":  k, "prop": k }
            )
          }
          // console.log(this.excelData)
        }
      }
    },

猜你喜欢

转载自blog.csdn.net/weixin_42382758/article/details/126558052
今日推荐