vue中联调后端导入excel接口,并且获取导入后的excel表格数据

本文介绍如何封装一个可复用的Excel文件上传组件,并通过该组件实现文件上传及数据获取的功能。组件支持预览、移除文件操作,限定文件类型与大小,并在上传成功后返回数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需求:联调excel导入接口,并且获取excel导入的数据
实现:封装el-upload组件,实现复用

在components中新建UploadDialog文件,封装uploadDialog组件
uploadDialog.vue文件内容(直接cv就可以使用)

<template>
  <el-dialog
    title="上传文件"
    :visible.sync="dialogShow"
    custom-class="customWidth"
    :close-on-click-modal="false"
    :show-close="false"
  >
    <el-upload
      ref="upload"
      class="upload-demo"
      :action="action_"
      :headers="headers"
      :on-preview="handlePreview"
      :on-remove="handleRemove"
      :before-remove="beforeRemove"
      :before-upload="checkFileSize"
      accept=".xls, .xlsx"
      :limit="3"
      :on-exceed="handleExceed"
      :on-success="uploadSuccess"
      :on-error="uploadErr"
      :file-list="fileList"
    >
      <el-button
        slot="trigger"
        class="filter-item"
        size="small"
        type="primary"
        icon="el-icon-upload"
        >选择文件</el-button
      >
      <div slot="tip" class="el-upload__tip" style="font-size: 14px">
        只能上传提供的
        <span style="color: red">模板EXCEL文件</span>
        ,且不超过2M
      </div>
    </el-upload>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">关 闭</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { getToken } from "../../utils/cookie";
export default {
  name: "UploadDialog",
  props: {
    url: {
      type: String,
      default: "",
    },
    dialogShow: {
      type: Boolean,
      default: false,
    },
  },
  computed: {
    action_: function () {
      return process.env.VUE_APP_BASE_API + this.url;
      // return "http://127.0.0.1:9091"+this.url
    },
  },
  data() {
    return {
      dom: null,
      fileList: [],
      headers: { Authorization: getToken() },
      // action_:window.location.protocol+'//'+window.location.host+'/appService'+this.url
    };
  },
  mounted() {},
  beforeDestroy() {},
  methods: {
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 3 个文件,本次选择了 ${files.length} 个文件,共选择了 ${
          files.length + fileList.length
        } 个文件`
      );
    },
    beforeRemove(file, fileList) {
      return false;
    },
    checkFileSize(file) {
      let fileSize = Number(file.size / 1024 / 1024);
      if (fileSize > 2) {
        this.$message.warning("文件大小不能超过2M");
        return false;
      }
    },
    uploadSuccess(response, file, fileList) {
      if (response && response.code == 200) {
        this.$message({
          type: "success",
          message: "上传成功!",
        });
        //console.log("excel数据", response.data);
        this.$emit("toFather", response.data);
        this.$refs.upload.clearFiles();
        this.closeDialog();
      } else {
        this.$message({
          type: "error",
          message: "文件导入时数据错误," + response.message,
        });
        //console.log("response", this.fileList);
        fileList.pop();
      }
    },
    uploadErr(err, file, fileList) {
      this.$message({
        type: "error",
        message: "文件导入失败",
      });
    },
    closeDialog() {
      this.$emit("closeListener", false);
    },
  },
};
</script>
<style lang="less">
.el-icon-close-tip {
  display: none !important;
}
.el-icon-close {
  display: none !important;
}
</style>

在需要用到的父组件页面中进行引入

    <upload-dialog
      :url="url"
      :dialogShow="dialogShow"
      @closeListener="getFromChild"
      @toFather="getExcelTabel"
    ></upload-dialog>
  </el-container>

dialogShow:控制上传excel文件对话框的显示与隐藏
url:后端的接口地址
getFromChild:关闭excel上传对话框
getExcelTabel:获取excel上传的数据

父组件中的方法

    getFromChild(false) {
      this.dialogShow = false;
    }
  //获取excel数据
    getExcelTabel(data) {
      this.excelData = data;
      console.log("excel", this.excelData);
      this.innerTableData.push(...this.excelData);
    },    

至此上传excel文件并且获取上传文件后的内容功能就实现了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端万古长青

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值