vue+springboot导入Excel表格

1.创建一个excel表格,与数据库需要的表头对应

2.(前端)导入excel的按钮
复制代码
<template class="importExcel">
  <el-button type="primary"  @click="chooseFile">导入<i class="el-icon-upload el-icon--right"></i></el-button>
  <input type="file" ref="fileInput" style="display: none"
         @change="handleFileChange" accept=".xlsx, .xls" />
</template>

显示为这样

3.按钮涉及到的方法
javascript 复制代码
chooseFile() {
  this.$refs.fileInput.click();
},
handleFileChange(event) {
  const file = event.target.files[0];
  if (file) {
    this.$message('所选文件:' + file.name);
    const fData = new FormData();
    fData.append('fData', file);
    const 
    axios.post("http://localhost:8081/probationaryPartyMember/importExcel", fData).then((response) => {
      console.log(response.data);
      if(response.data.code === 1) {
        // 处理成功响应
        this.$message('文件上传成功', response);
        this.formInline.name = '';
        this.formInline.studentId = '';
        this.formInline.reply = '';
        this.getList();
      }else {
        if (response.data.data === 2) {
          this.$message("编号为" + response.data.data + "的预备党员已存在,请删除这条数据上传之后的数据");
          if (response.data.data === 0) {
            this.$message("申请编号为" + response.data.data + "及之后的数据上传失败");
          }
        }
      }
    }).catch((error) => {
          // 处理错误
          console.error('文件上传失败', error);
          // 显示一个友好的错误消息给用户
          this.$message.error('文件上传失败,请检查文件并重试');
        });
  }


},
4 . (后端) 导入依赖
java 复制代码
 <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>3.1.1</version>
        </dependency>
5.创建一个实体类
6.创建一个类,继承ReadListener接口
7.在其他类中调用上述方法
8.得到列表,进行后续操作
相关推荐
卤蛋fg614 小时前
高性能 Vue 甘特图:vxe-gantt 如何秒级渲染万级任务数据
vue.js
逐光老顽童16 小时前
用 Go 实现一个 LLM 路由网关:Thompson Sampling 与自适应故障转移实践
vue.js·go
倒流时光三十年19 小时前
第十八章 搜索历史保存功能实现记录
spring boot·微信小程序
倒流时光三十年19 小时前
第十七章 投票页面增加搜索功能
spring boot·微信小程序
郑洁文20 小时前
基于Springboot的足球青训俱乐部管理系统的设计与实现
java·spring boot·后端·足球青训俱乐部管理系统
我登哥MVP20 小时前
Spring Boot 从“会用”到“精通”:自定义参数绑定原理
java·spring boot·后端·spring·servlet·maven·intellij-idea
ken223221 小时前
excel表格为什么越存文件越大? libreoffice 有效
excel
xkxnq21 小时前
第八阶段:工程化、质量管控与高级拓展(132天),Vue项目文档自动化:VuePress搭建组件文档(组件示例+API说明)
javascript·vue.js·自动化
小江的记录本21 小时前
【Spring全家桶】Spring AI核心原理、大模型集成、Prompt工程、RAG实现、AI Agent开发(附《思维导图》+《面试高频考点清单》)
java·人工智能·spring boot·后端·spring·面试·prompt
云烟成雨TD21 小时前
Spring AI 1.x 系列【40】MCP 客户端 Spring Boot 启动器
人工智能·spring boot·spring