前端利用el-upload实现Excel导入

首先说明一下这里前端只是前端利用el-upload上传了Excel文件,excel的解析工作是由后端同事利用阿里的easyexcel来解析的,所以说这个方法只适用于后端解析excel的方式。下边是效果图:
在这里插入图片描述
可以点击上传Excel也可以拖拽,样式可根据个人需要修改。

下边是组件代码:

<template>
<div class="upload-excel">
    <el-upload
        drag
        class="upload-demo"
        name="excel"
        action="https://www.sunots.shop/easyExcel/import"
        :on-error="uploadFalse"
        :on-success="uploadSuccess"
        :before-upload="beforeAvatarUpload"
        :limit="1"
         accept=".xlsx,.xls"
        :show-file-list="false"
        :headers="headerObj"
        :file-list="fileList">
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
         <!-- <el-button icon="el-icon-edit" type="primary">批量上传</el-button> -->
         <div slot="tip" class="el-upload__tip">只能上传Excel文件且上传模板大小不能超过 10MB!</div>
       </el-upload>
</div>
</template>

这里讲一下上边属性配置
name是定义文件的名字,将导入的文件名传给后端,后端接口也要这个名字
action是上传的路径,这里我是写死的,可自行灵活配置
on-error是上传失败时的钩子,可以用来显示错误消息
on-success是上传成功事件,通过里边的respone参数判断上传成功或失败
before-upload是文件上传之前的钩子,用来对文件进行上传前的大小类型判断
limit是允许最大上传个数
accept是指定上传的文件类型
show-file-list定义是否显示已上传文件列表,类型是布尔值,我这里效果是不让显示上传文件列表
headers是设置上传的请求头部 token
drag 启用拖拽上传
file-list 定义上传的文件列表,我这里写多余了因为我也不展示

下边是引入的token和data中定义的数据

import { getToken } from '@/utils/auth'
data() {
return {
    headerObj: {
        token: getToken()
      },
    fileList: []
};

下边是js代码几个事件

// 上传成功事件
     uploadSuccess(response, file, fileList) {
      console.log(response)
      if (response.stat == 1) {
        this.$message({
          message: response.msg,
          type: 'success'
        });
        this.$router.back()  //这里我是单独一个页面的上传组件,成功后返回上一个页面
      } else {
        this.$message({
          message: response.msg,
          type: 'error'
        });
      }
    },
    // 上传失败时的钩子
    uploadFalse(response, file, fileList) {
      this.$message({
          message: 'excel文件上传失败!',
          type: 'error'
        });
    },
    // 上传前对文件的大小的判断
    beforeAvatarUpload(file) {
      const extension = file.name.split(".")[1] === "xls";
      const extension2 = file.name.split(".")[1] === "xlsx";
      const isLt10M = file.size / 1024 / 1024 < 10;
         if (!extension && !extension2) {
           this.$message({
            message: '上传模板只能是 xls、xlsx格式!',
            type: 'error'
           });
          }
          if (!isLt10M) {
           console.log("上传模板大小不能超过 10MB!");
           this.$message({
            message: '上传模板大小不能超过 10MB!',
            type: 'error'
          });
         }
        return extension || extension2 || isLt10M
    },
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
el-uploadElement UI 中的上传组件,可以用于上传各种类型的文件,包括 Excel 文件。以下是一个基本的 el-upload 导入 Excel 文件的代码示例: ```html <el-upload class="upload-excel" action="/upload" :before-upload="beforeUpload" :on-success="onSuccess" :on-error="onError" > <el-button size="small" type="primary">上传 Excel 文件</el-button> </el-upload> ``` ```javascript methods: { beforeUpload(file) { // 判断文件类型是否是 Excel 文件 const fileType = file.type const isExcel = fileType === 'application/vnd.ms-excel' || fileType === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' if (!isExcel) { this.$message.error('只能上传 Excel 文件') return false } // 判断文件大小是否超过限制 const fileSize = file.size / 1024 / 1024 < 50 if (!fileSize) { this.$message.error('Excel 文件大小不能超过 50MB') return false } return true }, onSuccess(response) { this.$message.success('Excel 文件上传成功') // 处理上传成功后的逻辑 }, onError(error) { this.$message.error('Excel 文件上传失败') // 处理上传失败后的逻辑 } } ``` 在上面的代码中,我们通过 `beforeUpload` 方法判断上传的文件是否是 Excel 文件,并且限制了文件大小不能超过 50MB。当上传成功或失败时,会分别触发 `onSuccess` 和 `onError` 方法,我们可以在这些方法中处理上传后的逻辑。需要注意的是,上传的 Excel 文件需要在后端进行解析和处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值