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.得到列表,进行后续操作
相关推荐
EatFan2 小时前
Spring Boot 4 落地观察:从 yudao-cloud、matecloud、JPower 看国产脚手架的升级路线与迁移清单
java·spring boot·后端·spring cloud·微服务·后端开发·jdk 21
文人sec5 小时前
玩转excel数据透视表和图表
excel
凤山老林8 小时前
Spring Boot 集成 iText 7 实现动态 PDF 生成与电子签章:合同、报表场景实战
spring boot·后端·pdf·itext7·电子签章
卓怡学长8 小时前
w204基于springbo旅行社签证代理服务系统
java·spring boot·spring·intellij-idea
qwfy8 小时前
我要用 AI-native 架构,从零重建一套制造业 ERP/MES
vue.js·spring boot·架构
vipxieliang8 小时前
ValidX 在微服务架构中的验证策略
java·spring boot
❀͜͡傀儡师9 小时前
20 年沉淀,CAS 8.0 重新定义企业级 SSO:适配 JDK 25 与 Spring Boot 4.1
java·开发语言·spring boot
牧瀬クリスだ9 小时前
Spring统一功能处理
java·spring boot·spring·状态模式
dsyyyyy11019 小时前
Vue 3 Watch 监视完全指南
前端·javascript·vue.js
leihefeng9 小时前
PX06-对比两个 Excel 文件的差异:从逐单元格到生产级实现
python·excel