根据excel表 读取对应的数据 并上传

225 阅读1分钟
<input type="file" ref="upload" accept=".xls,.xlsx" class="outputlist_upload">
mounted: function () {
    this.$refs.upload.addEventListener('change', e => {//绑定监听表格导入事件
        this.readExcel(e);
    })
},
methods : {
    readExcel(e) {//表格导入
        var that = this;
        const files = e.target.files;
        console.log(files);
        if (files.length <= 0) {//如果没有文件名
          return false;
        } else if (!/\.(xls|xlsx)$/.test(files[0].name.toLowerCase())) {
          this.$Message.error('上传格式不正确,请上传xls或者xlsx格式');
          return false;
        }
        const fileReader = new FileReader();
        fileReader.onload = (ev) => {
          try {
            const data = ev.target.result;
            const workbook = XLSX.read(data, {
              type: 'binary'
            });
            const wsname = workbook.SheetNames[0];//取第一张表
            const ws = XLSX.utils.sheet_to_json(workbook.Sheets[wsname]);//生成json表格内容
            console.log(ws);
            for (var i = 0; i < ws.length; i++) {
              if (!ws[i].type_more_name) { //如果没填写大类
                ws[i].type_more_id = ''
              } else {
                ws[i].type_more = this.more_types.find((x) => {
                  return x.name === ws[i].type_more_name
                })
                if (!ws[i].type_more) {//如果大类写错了,找不到会返回undefined
                  // ws[i].type_more_id = '999'
                  ws[i].type_more_id = ws[i].type_more_name
                } else {//填写正确
                  ws[i].type_more_id = ws[i].type_more.id
                }
              }
              if (!ws[i].project_type) { //如果没填写大类
                ws[i].project_type = ''
              } else {
                ws[i].project_type_dict = this.type_list_pro.find((x) => {
                  return x.type_name === ws[i].project_type
                })
                if (!ws[i].project_type_dict) {//如果大类写错了,找不到会返回undefined
                  // ws[i].type_more_id = '999'
                  ws[i].project_typeid = ws[i].project_type
                } else {//填写正确
                  ws[i].project_typeid = ws[i].project_type_dict.id
                }
              }
              //表格里面没填写的都会显示undefined
              if (!ws[i].property_id) {
                ws[i].property_id = ''
              }
              if (!ws[i].project) {
                ws[i].project = ''
              }
              if (!ws[i].project_typeid) {
                ws[i].project_typeid = ''
              }
              if (!ws[i].factory) {
                ws[i].factory = ''
              }
              if (!ws[i].getway) {
                ws[i].getway = ''
              }
              if (!ws[i].amount) {
                ws[i].amount = ''
              }
              if (!ws[i].end_day) {
                ws[i].end_day = ''
              }
              if (!ws[i].config) {
                ws[i].config = ''
              }
              if (!ws[i].user_id) {
                ws[i].user_id = ''
              }
              this.message_list.push(ws[i])
            }
            this.$refs.upload.value = '';
            this.total = this.message_list.length
          } catch (e) {

            return false;
          }
        };
        fileReader.readAsBinaryString(files[0]);
      }
}