vue 组件导出excel

Vue配合element-ui实现excel导入导出

文章参考:vue后台项目模板,添加链接描述
项目运行:
npm install xlsx file-saver -S
npm install script-loader -S -D

终点在src目录下建一个名字为 vendor 的文件夹,里面放两个文件,文件 名不要改,会发生不可预知错误
在这里插入图片描述
文件下载地址:
添加链接描述

   //导出为excfel
    exportEx(){
      import('@/vendor/Export2Excel').then(excel => {
		//excel-list 非必填项
        const filterVal = ['numb','type', 'name', 'numer','price', 'date', 'recivedate','category','mode','address','people','descs']
        //表头必填项
        const tHeader = ['numb','type', 'name', 'numer','price', 'date', 'recivedate','category','mode','address','people','descs']
       //this.templist为获取到的选中的数组
        const list = this.templist
        const data = this.formatJson(filterVal, list)
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: this.filename,
          autoWidth: this.autoWidth,
          bookType: this.bookType
        })

      })

    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        if (j === 'timestamp') {
          return parseTime(v[j])
        } else {
          return v[j]
        }
      }))
    },

excel导入,vue文件在上面仓库

import UploadExcelComponent from "@/components/UploadExcel/index"
  <upload-excel-component :on-success="handleSuccess" :before-upload="beforeUpload"></upload-excel-component>
       </el-col>
        // 导入前的判断,可对导入文件做处理
    beforeUpload(file) {
      const isLt1M = file.size / 1024 / 1024 < 1

      if (isLt1M) {
        return true
      }

      this.$message({
        message: 'Please do not upload files larger than 1m in size.',
        type: 'warning'
      })
      return false
    },
    // 旧数据导入成功的回调
    handleSuccess({ results, header }) {
      console.log('返回值',results)
      //把数据进行排序
     let val = results.sort(this.compare('a'))
      console.log(val)
      localStorage.setItem('oldvale', JSON.stringify(val))
      this.getList()
    },
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值