SpringBoot中Excel的上传

一. 创建好一个SpringBoot项目

关键依赖

	<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>3.1.0</version>
        </dependency>

二.关键代码

2.1 关闭权限

在这里插入图片描述

2.2 配置Excel的解析监听器

package com.huang.vhr.framework.excel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.huang.vhr.framework.web.entity.Employeeremove;
import com.huang.vhr.framework.web.service.EmployeeremoveService;
import java.util.ArrayList;
import java.util.List;

public class EmployeeremoveListener implements ReadListener<Employeeremove> {

    private List<Employeeremove> employeeremoves = new ArrayList<>();
    private EmployeeremoveService employeeremoveService;

    public EmployeeremoveListener(EmployeeremoveService employeeremoveService) {
        this.employeeremoveService = employeeremoveService;
    }


    /**
     * Excel 文件是被 EasyExcel 自动解析的,每解析一行数据,就通过反射生成一个 Position 对象
     * @param
     * @param analysisContext
     */
    @Override
    public void invoke(Employeeremove employeeremove, AnalysisContext analysisContext) {
        employeeremove.setId(null);
        employeeremoves.add(employeeremove);
    }


    /**
     * 整个 Excel 文件解析完成时,会触发整个方法
     * @param analysisContext
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        employeeremoveService.saveBatch(employeeremoves);
    }
}

2.3Controller中写入文件上传的接口

这里使用的是Mybatis-plus 框架 ,开启了批处理

 @PostMapping("/import")
    //参数名为file,即我们airpost里要给他file一个对应的名字file
    public ResponseCode importPositionData(MultipartFile file) {
        return employeeremoveService.importEmployeeremoveData(file);
    }

2.4 yaml配置的开启批处理

注意:不开启批处理会十分慢

1.&rewriteBatchedStatements=true 开启批处理
2.
mybatis:
configuration:
map-underscore-to-camel-case: false
关闭mybatis的驼峰命名识别
3.mybatis-plus:
configuration:
map-underscore-to-camel-case: false
关闭mybatis-plus的驼峰命名识别

spring:
  datasource:
    username: root
    password: 1234
    url: jdbc:mysql:///vhr?serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
server:
  port: 8081


mybatis:
  configuration:
    map-underscore-to-camel-case: false

mybatis-plus:
  configuration:
    map-underscore-to-camel-case: false
#pagehelper: helper-dialect=mysql

2.5 实体类

package com.huang.vhr.framework.web.entity;

import java.io.Serializable;
import java.util.Date;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Data
@ToString
@NoArgsConstructor
public class Employeeremove implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     *
     */
    @TableId(value = "id", type = IdType.AUTO)
    @ExcelProperty("调动编号")
    private Integer id;

    /**
     *
     */

    @ExcelProperty("调动与主表关联")
    private Integer eid;

    /**
     * 调动后部门
     */
    @ExcelProperty("调动后部门")
    private Integer afterDepId;

    /**
     * 调动后职位
     */
    @ExcelProperty("调动后职位")
    private Integer afterJobId;

    /**
     * 调动日期
     */
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "Asia/Shanghai")
    @ExcelProperty("调动日期")
    private Date removeDate;

    /**
     * 调动原因
     */
    @ExcelProperty("调动原因")
    private String reason;

    /**
     *
     */
    @ExcelProperty("调动评语")
    private String remark;
}


postman测试
在这里插入图片描述

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值