使用vue导入excel文件并把文件中的数据在el-table表格中

先看一下效果吧

QQ录屏20220815152955

1、安装XLSX

 npm i XLSX -S

2、

<template>
  <div class="rtsp-container">
    <div style="display: flex">
      <el-button type="text" @click="downloadTemplate">下载模板</el-button>
      <div style="margin-left: 40px">
        <input
          class="file"
          accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
          type="file"
          ref="upload"
          @change="changeFile"
        />
        <el-button class="btn btn-right-margin" @click="browseFile"
          >选择文件</el-button
        >
        <el-input class="file-name" readonly :value="fileName" />
        <el-button class="btn btn-blue" type="primary" @click="importExcelData"
          >导入</el-button
        >
      </div>
    </div>
    <el-table
      :data="tableData"
      height="270"
      style="width: 100%; margin-bottom: 30px"
    >
      <el-table-column type="index" label="序号" width="80" align="center">
      </el-table-column>
      <el-table-column prop="type" label="设备类型"> </el-table-column>
      <el-table-column prop="name" label="设备名称"> </el-table-column>
      <el-table-column
        prop="rtsp"
        width="180"
        :show-overflow-tooltip="true"
        label="码流地址"
      >
      </el-table-column>
      <el-table-column
        prop="historyRtsp"
        width="180"
        :show-overflow-tooltip="true"
        label="历史视频流地址"
      >
      </el-table-column>
      <el-table-column label="操作" width="60">
        <template slot-scope="scope">
          <el-button type="text" @click="del(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
export default {
  name: "RTSP",
  data() {
    return {
      fileName: "",
      fileTemp: null,
      tableData: [],
    };
  },
  watch:{
    tableData:{
      handler(val){
        console.log(val);
      },
      immediate:true,
      deep:true
    }
  },
  methods: {
    downloadTemplate() {
      let a = document.createElement("a"); //创建一个<a></a>标签
      a.href = "/static/file/template.xlsx"; // 给a标签的href属性值加上地址,注意,这里是绝对路径,不用加 点.
      a.download = "批量导入RTSP通道模板.xlsx"; //设置下载文件文件名,这里加上.xlsx指定文件类型,pdf文件就指定.fpd即可
      a.style.display = "none"; // 障眼法藏起来a标签
      document.body.appendChild(a); // 将a标签追加到文档对象中
      a.click(); // 模拟点击了a标签,会触发a标签的href的读取,浏览器就会自动下载了
      a.remove(); // 一次性的,用完就删除a标签
    },
    changeFile() {
      this.fileName = this.$refs.upload.files[0].name;
      this.fileTemp = this.$refs.upload.files[0];
    },
    browseFile() {
      this.$refs.upload.click();
    },
    importExcelData() {
      if (this.fileTemp) {
        let _this = this;
        this.file = this.fileTemp;
        let rABS = false; //是否将文件读取为二进制字符串
        let f = this.file;
        let reader = new FileReader();
        FileReader.prototype.readAsBinaryString = function (f) {
          let binary = "";
          let rABS = false; //是否将文件读取为二进制字符串
          let wb;
          let outData;
          let reader = new FileReader();
          reader.onload = function (e) {
            let bytes = new Uint8Array(reader.result);
            let length = bytes.byteLength;
            for (let i = 0; i < length; i++) {
              binary += String.fromCharCode(bytes[i]);
            }
            let XLSX = require("xlsx");
            if (rABS) {
              wb = XLSX.read(btoa(fixdata(binary)), {
                type: "base64",
              });
            } else {
              wb = XLSX.read(binary, {
                type: "binary",
              });
            }
            outData = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]);
            let da = [...outData];
            da = da.map((item, index) => {
              let newItem = {
                id: index + 1,
                type: item["设备类型"],
                name: item["设备名称"],
                rtsp: item["码流地址"],
              };
              for (const key in item) {
                if (key.indexOf("获取历史视频流地址") !== -1) {
                  newItem.historyRtsp = item[key];
                }
              }
              return newItem;
            });
            _this.tableData = [...da];
          };
          reader.readAsArrayBuffer(f);
        };
        if (rABS) {
          reader.readAsArrayBuffer(f);
        } else {
          reader.readAsBinaryString(f);
        }
      }
    },
    del(row) {
      this.tableData = this.tableData.filter((item) => item.id !== row.id);
    },
  },
};
</script>
<style >
.rtsp-container {
  width: 100%;
}
.rtsp-container .file {
  display: none;
}

.rtsp-container .file-name {
  height: 34px;
  width: 350px;
  margin: 20px;
}
</style>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值