js上传excel

在这里插入图片描述
npm i xlsx

// HTML
<el-form-item>
          <el-upload
            class="upload-demo"
            action=""
            :on-change="handleChange"
            :show-file-list="false"
            :on-remove="handleRemove"
            accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel"
            :auto-upload="false"
          >
            <el-button
              v-permission="'PA.mallassist.orderManage.batchExcel'"
              type="primary"
              size="mini"
              >批量(导入)发货单</el-button
            >
          </el-upload>
          <el-button
            v-permission="'PA.mallassist.orderManage.batchDeliver'"
            type="primary"
            size="mini"
            @click="bulkDelivery"
            >批量发货</el-button
          >
        </el-form-item>
<script src="http://oss.sheetjs.com/js-xlsx/xlsx.full.min.js"></script>
import XLSX from "xlsx";
    importfxx(obj) {
      this.file = obj;
      const _this = this;
      const rABS = false; //是否将文件读取为二进制字符串
      const f = this.file;
      const reader = new FileReader();
      FileReader.prototype.readAsBinaryString = function(f) {
        let binary = "";
        const rABS = false; //是否将文件读取为二进制字符串
        // const pt = this;
        let wb; //读取完成的数据
        let outdata;
        const reader = new FileReader();
        reader.onload = function(e) {
          const bytes = new Uint8Array(reader.result);
          const length = bytes.byteLength;
          for (let i = 0; i < length; i++) {
            binary += String.fromCharCode(bytes[i]);
          }
          if (rABS) {
            wb = XLSX.read(btoa(_this.fixdata(binary)), {
              //手动转化
              type: "base64",
            });
          } else {
            wb = XLSX.read(binary, {
              type: "binary",
            });
          }
          outdata = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]); //outdata就是读取excel内容之后输出的东西
          _this.da = [...outdata];
          const arr = [];
          _this.da.map((v) => {
            const obj = {};
            obj.rownum = v["rownum"]; //行号
            obj.expressCompany = v["expressCompany"]; //快递公司
            obj.expressNum = v["expressNum"]; //快递单号
            obj.orderbatchcode = v["orderbatchcode"]; //订单号
            obj.productnumber = v["productnumber"]; //物料编号
            obj.pnum = v["pnum"]; //数量
            arr.push(obj);
          });
          console.log("arr", arr);
          _this.tableData2 = arr;
        };
        reader.readAsArrayBuffer(f);
      };
      if (rABS) {
        reader.readAsArrayBuffer(f);
      } else {
        reader.readAsBinaryString(f);
      }
    },
    fixdata(data) {
      //文件流转BinaryString
      let o = "";
      let l = 0;
      const w = 10240;
      for (; l < data.byteLength / w; ++l)
        o += String.fromCharCode.apply(
          null,
          new Uint8Array(data.slice(l * w, l * w + w))
        );
      o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)));
      return o;
    },
    handleChange(file) {
      this.fileTemp = file.raw;
      if (this.fileTemp) {
        if (
          this.fileTemp.type ==
            "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ||
          this.fileTemp.type == "application/vnd.ms-excel"
        ) {
          this.importfxx(this.fileTemp);
        } else {
          this.$message({
            type: "warning",
            message: "附件格式错误,请删除后重新上传!",
          });
        }
      } else {
        this.$message({
          type: "warning",
          message: "请上传附件!",
        });
      }
    },
    

复制粘贴就可以用了。
附上excel中内容
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值