vue exale 转为 json

file 是文件
   _toJson(fiel) {
      let self = this;
      let file = fiel;
      console.log(file);
      let reader = new FileReader();
      reader.onload = (event) => {
        console.log(event);
        let data = event.target.result;
        const workbook = XLSX.read(data, {
          type: "binary",
        });

        let resultArray = [];
        let tableArray = [];
     
        workbook.SheetNames.forEach(function (sheetName) {
          console.log(sheetName);
          console.log("workbook.Sheets[sheetName]", workbook.Sheets[sheetName]);
          // XLSX.utils.sheet_to_json(worksheet , {defval:""})
          const data = XLSX.utils.sheet_to_json(workbook.Sheets[sheetName], {
            header: 1,
            defval: "",
          });
          console.log("data", data);
          resultArray = [...data];
          resultArray.splice(0, 2);
          resultArray.forEach((item, index) => {
            item = item.slice(0, 4);
            console.log(item);
            item[0] = String(item[0]).substring(0, 20);

            let prefix = String(item[1]).substring(0, 2);
            if (prefix == "黑龙") {
              item[1] = String(item[1]).substring(0, 3);
            } else {
              item[1] = prefix;
            }
            if (item[1] === "内蒙") {
              item[1] = "内蒙古";
            }
            item[2] = String(item[2]).substring(0, 10);

            if (typeof item[3] == "number") {
              console.log(typeof item[3]);
              if (item[3] < 0) {
                item[3] = "";
              }else {
                item[3] = Number(String(item[3]).substring(0, 10));
              }
              
            } else {
              item[3] = "";
            }
            tableArray.push(
              {
              name: item[0],
              province: item[1],
              category: item[2],
              revenue: item[3],
              show_detail: 2,
              image: { name: "", url: "" },
            }
            )
      
          });

       
        });

           console.log(resultArray);
          console.log(tableArray);
          self.$emit("accept", tableArray);
      };

      reader.readAsBinaryString(file);
    },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值