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.得到列表,进行后续操作
相关推荐
GitLqr4 分钟前
提升 Web 开发效率:我常用的 10 个 TypeScript 实战技巧
vue.js·react.js·typescript
huahailing102435 分钟前
Spring Boot 集成 XXL-Job 完整实现方案(支持动态CRUD)
java·spring boot·后端
音符犹如代码2 小时前
Arthas Profiler 火焰图实战:CPU 热点在哪一目了然
java·jvm·spring boot
大模型丫丫3 小时前
Spring Boot 入门指南:从零开始构建微服务
spring boot·微服务·firefox
AI_paid_community4 小时前
机械研发人员如何使用 Claude?
java·vue.js
cdcdhj4 小时前
vue3中的watchEffect()监听,什么时候监听,什么时候清理,什么时候停止
前端·javascript·vue.js
无人生还4 小时前
从 Vue3 到 React · 快速上手系列第 6 篇:状态管理 useState 与 useReducer
前端·vue.js·react.js
Flynt4 小时前
Spring Boot 3开虚拟线程,我把生产服务搞崩了两次
java·spring boot
蓝创工坊Blue Foundry5 小时前
发票、凭证和对账单怎么整理成 Excel?先定义字段,再逐项复核
pdf·ocr·excel·文心一言·paddlepaddle
林间码客5 小时前
全栈视角下的 Mock 指南:从后端单元测试到前端 Vue 3 联调
前端·vue.js·单元测试