导入excel并进行数据提取

/**
         * @description: 导入excel并进行数据提取
         * @param {type} 
         * @return: 
         */
        Vue.prototype.$importExcel = function (file, header) {
            let _this = this;
            return new Promise(function (resolve, reject) {
                const types = file.name.split('.')[1]
                const fileType = ['xlsx', 'xlc', 'xlm', 'xls', 'xlt', 'xlw', 'csv'].some(item => item === types)
                if (!fileType) {
                    _this.$message({
                        type: "warning",
                        message: "文件格式不正确,请重新选择!"
                    });
                    reject();
                }
                const reader = new FileReader();
                reader.onload = function (e) {
                    const data = e.target.result;
                    this.wb = XLSX.read(data, {
                        type: "binary"
                    });
                    const wsname = this.wb.SheetNames[0];
                    const ws = this.wb.Sheets[wsname];
                    /* Convert array of arrays */
                    const sheetJson = XLSX.utils.sheet_to_json(ws);
                    let tableData = []; //转换为真正的table所需要的数据
                    for (let item of sheetJson) {
                        let obj = {};
                        for (let key in item) {
                            for (let childItem of _this.header) {
                                if (key === childItem.label) {
                                    obj[childItem.prop] = item[key];
                                    break;
                                }
                            }
                        }
                        tableData.push(obj);
                    }
                    resolve(tableData);
                };
                reader.readAsBinaryString(file.raw);
            });
        }

  

 

<template>
  <div>
    <el-upload
      class="upload-demo"
      ref="upload"
      :auto-upload="false"
      :on-change="change"
    >
      <el-button
        style="margin-left: 10px;"
        size="small"
        type="success"
        @click="submitUpload"
      >上传到服务器</el-button>
     </el-upload>
  </div>
</template>
<script>

</script>
import XLSX from "xlsx";
export default {
 methods: {
    submitUpload() {
      this.$refs.upload.submit();
    },
    change(file) {
      this.$importExcel(file, this.header).then(tableData => {
        console.log(tableData);
      });
    },
 }
}

  

转载于:https://www.cnblogs.com/wangRong-smile/p/11137355.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值