• vue+element ui读取excel文件


    <el-upload
      ref="upload"
      :http-request="handleImport"
      :show-file-list="false"
      accept=".xlsx,.xls"
      action="#"
      class="upload-demo"
      drag
      multiple
    >
      <i class="el-icon-upload">i>
      <div class="el-upload__text">将文件拖到此处,或<em>点击上传em>div>
      <div slot="tip" class="el-upload__tip">只能上传xlsx、xls文件,且不超过500kbdiv>
    el-upload>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    handleImport(event) {
      const file = event.file
      if (file) {
        this.readExcel(file)
        this.dialogImport = false
      }
    },
    readExcel(files) {
      const fileReader = new FileReader()
      fileReader.onload = (files) => {
        try {
          const data = files.target.result
          const workbook = xlsx.read(data, {
            type: 'binary'
          })
          const wsname = workbook.SheetNames[0] // 取第一张表
          const ws = xlsx.utils.sheet_to_json(workbook.Sheets[wsname]) // 生成json表格内容
          // console.log(ws, 'ws是表格里的数据,且是json格式')
          this.$message.success('导入成功')
          for (let i of ws) {
            const { 姓名, 手机号, 欠薪金额, 身份证号, 欠薪开始时间, 欠薪结束时间, 开户银行, 银行卡号, 备注 } = i
    
            this.queryParam.list.push({
              name: 姓名,
              cellPhone: 手机号,
              times: [this.fileDate(欠薪开始时间), this.fileDate(欠薪结束时间)],
              bankNo: 银行卡号,
              idcardNumber: 身份证号,
              money: 欠薪金额,
              bank: 开户银行,
              remark: 备注
            })
          }
        } catch (e) {
          console.log(e)
          this.$message.warning('请重试')
          return false
        }
      }
      fileReader.readAsBinaryString(files)
    },
    fileDate(date) {
      if (date) {
        let year, month, day, HH, mm, ss
        const time = new Date((date - 1) * 24 * 3600000 + 1 - 8 * 3600000)
    
        time.setFullYear(time.getFullYear() - 70)
        let timeDate
        year = time.getFullYear() // 年
        month = time.getMonth() < 9 ? '0' + (time.getMonth() + 1) : time.getMonth() + 1 // 月
        day = time.getDate() <= 9 ? '0' + time.getDate() : time.getDate() // 日
        HH = time.getHours() // 时
        mm = time.getMinutes() // 分
        ss = time.getSeconds() // 秒
        return (timeDate = year + '-' + month + '-' + day)
      } else return ''
    },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

    :http-request=“handleImport” 个人习惯使用这个方法,当然也可以使用别的方法,比如 :on-change=“handleImport” 只要是能获取到文件都可以。

    fileDate方法是格式化时间的,excel读取的时间格式转成自己想要的格式。我这边格式是YYYY-MM-dd,当然也有其他格式的,自行百度。

  • 相关阅读:
    Java实现图书管理系统
    数据结构与算法基础(王卓)(5)
    0数据结构-结构体struct与typedef
    LuatOS-SOC接口文档(air780E)--disp - disp库已合并到u8g2库,API等价
    程序执行的四个阶段
    ROS代码中的消息日志级别
    Python 3.6.10 中的 requests 库 TLS 1.2 强制使用问题及解决方案
    社交软件用户画像分析,社交网络数据可视化
    HTML动态爱心
    【小沐学NLP】AI辅助编程工具汇总
  • 原文地址:https://blog.csdn.net/qq_43628847/article/details/133851833