Excel导入操作

news/2024/7/21 5:20:51 标签: excel
<template>
  <el-dialog
    width="500px"
    title="员工导入"
    :visible="showExcelDialog"
    @close="$emit('update:showExcelDialog', false)"
  >
    <el-row type="flex" justify="center">
      <div class="upload-excel">
        <input
          ref="excel-upload-input"
          class="excel-upload-input"
          type="file"
          accept=".xlsx, .xls"
          @change="uploadChange"
        />
        <div class="drop">
          <i class="el-icon-upload" />
          <el-button type="text" @click="getTemplate">下载导入模板</el-button>
          <span
            >将文件拖到此处或
            <el-button type="text" @click="handleUpload">点击上传</el-button>
          </span>
        </div>
      </div>
    </el-row>
    <el-row type="flex" justify="end">
      <!-- update:props属性名,值 直接修改 .sync修饰符的属性值 -->
      <el-button
        size="mini"
        type="primary"
        @click="$emit('update:showExcelDialog', false)"
        >取消</el-button
      >
    </el-row>
  </el-dialog>
</template>

点击上传-弹出文件选择框

 <el-button type="text" @click="handleUpload">点击上传</el-button>
methods:{
  // 上传
  handleUpload() {
    this.$refs["excel-upload-input"].click();// this.$refs.属性名 和 this.$refs[属性名] 等价
  },
}

在这里插入图片描述
监听文件改变-上传excel-关闭弹层

 <input
     ref="excel-upload-input"
      class="excel-upload-input"
      type="file"
      accept=".xlsx, .xls"
      @change="uploadChange"
  >
  async uploadChange(event) {
      console.log(event.target.files)
      // 调用上传接口
      // uploadExcel() // 参数  form-data 需要文件file
      const files = event.target.files // input的文件列表
      if (files.length > 0) {
        // 大于0 说明有文件要上传
        const data = new FormData()
        // file: file类型
        data.append('file', files[0]) // 将文件参数加入到formData中
        try {
          await uploadExcel(data)
          // 成功
          this.$emit('uploadSuccess') // 通知父组件 我上传成功
          this.$emit('update:showExcelDialog', false) // 关闭弹层
          // this.$refs['excel-upload-input'].value = ''
        } catch (error) {
          // 捕获失败
          // this.$refs['excel-upload-input'].value = ''
        } finally {
          // 不论成功或者失败都会执行finally
          this.$refs['excel-upload-input'].value = ''
        }
      }
}
//接口
export function uploadExcel(data) {
  return request({
    url: "/sys/user/import",
    method: "post",
    data, // form-data类型 因为要上传文件类型
  });
}

上传成功
在这里插入图片描述
1、首先封装一个员工导入组件,利用elementUI提供的组件铺设静态页面,
2、给导入模板绑定点击事件,弹出弹层,封装下载Excel模板接口,调用接口,这里和导出一样,需要用到file-saver来下载模板。
3、给点击上传按钮绑定点击事件,这里我们使用的是模拟点击,其实真正被点击的是input文本框,由于它放置页面不太协调,所以采用模拟点击。
4、封装上传Excel模板文件接口,这里上传data必须是formdata类型,因为要上传文件类型,调用接口,监听change事件,当用户选择了文件,而且这个文件和上次的不一样,就会触发change,所以这里使用e.target.files,files是数组,如果files的长度大于0,说明有文件要上传,此时就把文件封装到formdata对象中,根据文档提示,这里需要file类型,然后将文件参数加入到formData中,成功就提示父组件刷新页面,关闭弹层,失败就清空文件,这里即使上传成功也一样要清空文件,所以使用finally来控制。


http://www.niftyadmin.cn/n/5230147.html

相关文章

训练 CNN 对 CIFAR-10 数据中的图像进行分类-keras实现

1. 加载 CIFAR-10 数据库 import keras from keras.datasets import cifar10# 加载预先处理的训练数据和测试数据 (x_train, y_train), (x_test, y_test) cifar10.load_data() 2. 可视化前 24 个训练图像 import numpy as np import matplotlib.pyplot as plt %matplotlib …

conda和pip常用命令整理

文章目录 一、conda常用指令1. 更新2 .环境管理3. 包管理 二、pip常用命令1. 常用命令2. 国内镜像 一、conda常用指令 1. 更新 conda --version 或 conda -V #查看conda版本 conda update conda # 基本升级 conda update anaconda # 大的升级 conda upd…

PostGIS学习教程八:空间关系

PostGIS学习教程八&#xff1a;空间关系 到目前为止&#xff0c;我们只使用了测量&#xff08;ST_Area、ST_Length&#xff09;、序列化&#xff08;ST_GeomFromText&#xff09;或者反序列化&#xff08;ST_AsGML&#xff09;几何图形&#xff08;geometry&#xff09;的空间…

长沙电信大楼火灾调查报告发布:系烟头引发。FIS来护航安全

近日&#xff0c;长沙电信大楼的火灾调查报告引起广泛关注。调查发现&#xff0c;火灾是由未熄灭的烟头引发&#xff0c;烟头点燃了室外平台的易燃物&#xff0c;迅速蔓延至整个建筑。这起悲剧再次提醒我们&#xff0c;小小的疏忽可能酿成大灾难。但如果我们能及时发现并处理这…

2023年小美赛认证杯B题:工业表面缺陷检测(Industrial Surface Defect Detection)思路模型代码解析

2023年小美赛认证杯B题&#xff1a;工业表面缺陷检测&#xff08;Industrial Surface Defect Detection&#xff09; 【请电脑打开本文链接&#xff0c;扫描下方名片中二维码&#xff0c;获取更多资料】 一、问题重述 金属或塑料产品表面的缺陷不仅影响产品外观&#xff0c;…

Flink(八)【窗口】

前言 终于忙完了四门专业课的期末&#xff0c;确实挺累啊。今天开始继续学习 Flink &#xff0c;接着上次的内容。 今日摘录&#xff1a; 他觉得一个人奋斗更轻松自在。跟没有干劲的人在一起厮混&#xff0c;只会徒增压力。 -《解忧杂货店》 1、窗口 之前我们已经了解了…

切水果小游戏

欢迎来到程序小院 切水果 玩法&#xff1a;点击鼠标左键划过水果&#xff0c;快去切水果&#xff0c;看你能够获划出多少水果哦^^。开始游戏https://www.ormcc.com/play/gameStart/205 html <div id"game" class"game" style"text-align: center;…

华为云SDK的全部Maven参考

文档原始地址:https://console.huaweicloud.com/apiexplorer/#/sdkcenter?OBS= 弹性云服务器 3.1.70 源码仓库包仓库 <dependency> <groupId>com.huaweicloud.sdk</groupId> <artifactId>huaweicloud-sdk-ecs</artifactId> <version>3…