el-import 上传.xls或.xlsx文件

5 篇文章 0 订阅
1 篇文章 0 订阅
<template>
  <el-upload
    ref="upload"
    class="upload"
    :headers="{ token: token }"
    :action="upLoadUrl"
    :before-upload="beforeUploadFile"
    :on-change="fileChange"
    :on-remove="handleRemove"
    :on-success="imgSuccess"
    :file-list="fileList"
  >
    <el-button size="small" type="primary">点击上传</el-button>
  </el-upload>
</template>

<script>
import Vue from 'vue'

export default {
  data() {
    return {
      // visible: false,
      upLoadUrl: '',
      // (process.env.NODE_ENV !== 'production' && process.env.OPEN_PROXY
      //   ? '/proxyApi/'
      //   : window.SITE_CONFIG.baseUrl) + '/v1/common/uploadExcelFile',
      token: Vue.cookie.get('token'),
      fileList: [], // excel文件列表
      formData: new FormData(),
      dataForm: {
        file: ''
      },
      dataRule: {
      }
    }
  },
  methods: {
    handleRemove(file, fileList) {
      for (const i in this.fileList) {
        if (this.fileList[i].uid === file.uid) {
          this.fileList.splice(i, 1)
        }
      }
      this.dataForm.file = ''
    },
    // 文件上传成功回调
    imgSuccess(res) {
      if (res.code === 0) {
        this.dataForm.file = res.data
      } else {
        this.dataForm.file = ''
      }
    },
    // 文件状态改变时的钩子
    fileChange(file, fileList) {
      this.fileList = fileList
      if (fileList.length > 1) {
        fileList.splice(0, 1)
      }
    },
    // 上传文件之前的钩子, 参数为上传的文件,若返回 false 或者停止上传
    beforeUploadFile(file) {
      var fileName = file.name.substring(file.name.lastIndexOf('.') + 1)
      const extension = fileName === 'xlsx'
      const extension2 = fileName === 'xls'
      if (!extension && !extension2) {
        this.$message.warning('导入文件只能是.xls或.xlsx格式的文件!')
        this.fileList.splice(this.fileList.length - 1, 1)
      }
      return extension || extension2
    },
    init() {
      // this.visible = true
      this.fileList.splice(0, this.fileList.length)
      this.dataForm.file = ''
    }
    // // 表单提交
    // dataFormSubmit() {
    //   this.$refs['dataForm'].validate((valid) => {
    //     if (valid) {
    //       console.log('文件上传格式正确')
    // this.$http({
    //   url: this.$http.adornUrl('/v1/sys/server/import'),
    //   method: 'post',
    //   data: this.$http.adornData({
    //     filePath: this.dataForm.file
    //   })
    // }).then(({ data }) => {
    //   if (data && data.code === 0) {
    //     this.$message.success(data.msg)
    //     this.visible = false
    //     this.$refs.upload.clearFiles()
    //     this.$emit('refreshDataList')
    //   } else {
    //     this.visible = false
    //     this.$refs.upload.clearFiles()
    //     this.$emit('refreshDataList')
    //   }
    // })
    //     }
    //   })
    // }
  }
}
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Element UI的el-upload组件上Excel文件并预览可以分为以下几个步骤: 1. 安装Element UI和xlsx依赖。在命令行中执行以下命令: ``` npm install element-ui xlsx --save ``` 2. 在Vue组件中引入Element UI和xlsx。例如: ```javascript import { ElUpload, ElButton } from 'element-ui'; import XLSX from 'xlsx'; ``` 3. 在Vue组件中使用el-upload组件创建文件并预览的功能。例如: ```html <template> <el-upload class="upload-demo" action="" :on-change="handleFileUpload" :before-upload="beforeUpload" :file-list="fileList" :auto-upload="false" :accept=".xlsx, .xls" :show-file-list="false"> <el-button size="small" type="primary">点击上</el-button> <div slot="tip" class="el-upload__tip">只能上Excel文件</div> </el-upload> <table> <thead> <tr> <th v-for="header in headers" :key="header">{{ header }}</th> </tr> </thead> <tbody> <tr v-for="(row, index) in data" :key="index"> <td v-for="(value, key) in row" :key="key">{{ value }}</td> </tr> </tbody> </table> </template> ``` 4. 在Vue组件中定义beforeUpload方法,用于限制只能上Excel文件。例如: ```javascript beforeUpload(file) { const fileType = file.name.split('.').pop(); if (fileType !== 'xlsx' && fileType !== 'xls') { this.$message.error('只能上Excel文件'); return false; } } ``` 5. 在Vue组件中定义handleFileUpload方法,用于处理上的Excel文件。例如: ```javascript handleFileUpload(file) { const reader = new FileReader(); reader.onload = (e) => { const data = e.target.result; const workbook = XLSX.read(data, { type: 'binary' }); const sheetName = workbook.SheetNames[0]; const sheet = workbook.Sheets[sheetName]; const jsonData = XLSX.utils.sheet_to_json(sheet, { header: 1 }); this.headers = jsonData[0]; this.data = jsonData.slice(1); }; reader.readAsBinaryString(file.raw); } ``` 6. 在Vue组件中定义fileList、headers和data变量,用于存储上的Excel文件信息和解析后的数据。例如: ```javascript data() { return { fileList: [], headers: [], data: [] }; } ``` 7. 根据需要自定义样式和其他功能。例如: ```css .upload-demo { margin-top: 20px; width: 100%; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值