前端读取导入数据

<Upload action=""  accept=".xls, .xlsx" :before-upload="handleBeforeUpload" style="float:right;right: 5px;height:32px;">
        <Button type="primary" :loading="uploadLoading"  style="float:right;margin-right: 5px" @click="handleUploadFile">导入</Button>
      </Upload>
handleUploadFile () {
      this.initUpload()
    },
    initUpload () {
        this.file = null
      },
    handleBeforeUpload (file) {
        const fileExt = file.name.split('.').pop().toLocaleLowerCase()
        if (fileExt === 'xlsx' || fileExt === 'xls') {
          this.readFile(file)
          this.file = file
        } else {
          this.$Notice.warning({
            title: '文件类型错误',
            desc: '文件:' + file.name + '不是EXCEL文件,请选择后缀为.xlsx或者.xls的EXCEL文件。'
          })
        }
        return false
      },
      // 读取文件
      readFile (file) {
        const reader = new FileReader()
        reader.readAsArrayBuffer(file)
        reader.onloadstart = e => {
          this.uploadLoading = true
        }
      
        reader.onerror = e => {
          this.$Message.error('文件读取出错')
        }
        reader.onload = e => {
          this.$Message.info('文件读取成功')
          const data = e.target.result
          const { header, results } = excel.read(data, 'array')
          let tableTitle = header.map(item => { return { title: item, key: item } })
          let tableData = results
          this.uploadLoading = false
          //保存数据
          this.saveImporeData(tableData);
        }
      },
    saveImporeData(tableData){
      importData(tableData).then(res=>{
        if(res.status===200){
          this.search();
        }
      });
    },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值