vue导入xlsx表格获取数据写入表格中

1.上传elcel表格得到表格对应的table数组

  原理:主要依赖于 xlsx 包  版本依赖 0.16.1

  下载方式npm install xlsx@0.16.1  0.18也可以 引入方式一定要用require("xlsx")的方式

npm install xlsx@0.16.1

  引入方式  var xlsx = require("xlsx");  

还用的了el-upload


<template>
  <div>
    <el-upload
      class="upload-demo"
      ref="upload"
      :action="doUpload"
      :data="{ bizType: 1, type: 1 }"
      :on-change="changeFile"
      :multiple="false"
      :limit="1"
      :file-list="fileList"
      :auto-upload="false"
      accept=".xls,.xlsx"
    >
      <el-button
        style="margin-left: 10px"
        slot="trigger"
        size="mini"
        icon="el-icon-folder-opened"
        type="primary"
        >选择文件</el-button
      >
    </el-upload>
  </div>
</template>

<script>
export default {
  name: "App",
  components: {},
  data() {
    return {
      doUpload: "/api/statis/budgetStandard/importIncomeInfoExcel",
      fileList: [],
    };
  },
  methods: {
    changeFile(file, fileList) {
      let dataList = [];
      if (fileList.length) {
        let fileContent = fileList[0].raw;
        const reader = new FileReader();
        reader.readAsArrayBuffer(fileContent);
        reader.onload = function () {
          const buffer = reader.result;
          const bytes = new Uint8Array(buffer);
          const length = bytes.byteLength;
          let binary = "";
          for (let i = 0; i < length; i++) {
            binary += String.fromCharCode(bytes[i]);
          }
          const XLSX = require("xlsx");
          const wb = XLSX.read(binary, {
            type: "binary",
          });
          // excel表格的数据有第一张表第二张表的 SheetNames[0]就是第一个表
          const outdata = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]);
          // 这里就是吧excel表格的数据转换成数组的形式
          console.log(outdata, "outdata");
        };
      }
    },
  },
};
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zWoods

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值