element的导入导出excel功能数据处理

1
npm install -S file-saver xlsx
npm install -D script-loader
2.在src目录下新建excel文件夹,然后引入Blob.js和Expor2Excel.js
百度云盘 可获取到Blob.js和Expor2Excel.js
链接: https://pan.baidu.com/s/1z0sieEiY5wf4tpY-845k5A 提取码: b38k 
//Expor2Excel.js中,路径要和自己实际文件路径一致
require('script-loader!file-saver');
require('./Blob.js'); //根据自己项目的实际路径
require('script-loader!xlsx/dist/xlsx.core.min');

 <el-upload class="upload-demo" 
 action="" 
 :on-change="handleChange" 
 :show-file-list="false" 
 accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel"
:auto-upload="false" >
     <el-button size="mini" icon="el-icon-plus" type="primary">批量导入</el-button>
 </el-upload>

 /** 导入按钮操作 **/
 handleChange(file, fileList) { //excel文件改变
     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.importExcel(this.fileTemp)
        } else {
          this.$message({
            type: 'warning',
            message: '附件格式错误,请删除后重新上传!'
          })
        }
      } else {
        this.$message({
          type: 'warning',
          message: '请上传附件!'
        })
      }
 },
 importExcel(obj) {
    let _this = this;
    // 通过DOM取文件数据
    this.file = obj

    var rABS = false; //是否将文件读取为二进制字符串
    var f = this.file;
    var reader = new FileReader();
    FileReader.prototype.readAsBinaryString = function(f) {
      var binary = "";
      var rABS = false; //是否将文件读取为二进制字符串
      var pt = this;
      var wb; //读取完成的数据
      var outdata;
      var reader = new FileReader();

      reader.onload = function(e) {
        var bytes = new Uint8Array(reader.result);
        var length = bytes.byteLength;
        for (var i = 0; i < length; i++) {
          binary += String.fromCharCode(bytes[i]);
        }
        var 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]]); //outdata就是你想要的东西

        this.da = [...outdata]
        //这里 处理导入的excel数据
        let arr = []
        this.da.map(v => {
          let name= v['姓名']
          arr.push(name=)
        })
        //以下下 这里去处理导入的excel数据怎么存储到数据库中
        //.....
        //.....
        return arr
      }
      reader.readAsArrayBuffer(f);
    }

    if (rABS) {
      reader.readAsArrayBuffer(f);
    } else {
      reader.readAsBinaryString(f);
    }
 },
上一篇:用FileReader的方式上传头像


下一篇:【DataX】Java中集成DataX开发