SpringBoot+EasyExcel实现Excel导入功能
需求:需要把一个Excel大文件的数据存入数据库。
技术:SpringBoot + EasyExcel + Postman
1、加入EasyExcel依赖
<!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.6</version>
</dependency>
2.添加excel数据对应的对象类
import com.alibaba.excel.annotation.ExcelProperty;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
@Data
/**
* 读写反射对象用到了Cglib,所以成员变量必须符合驼峰规范,而且使用@Data不能使用 @Accessors(chain = true)
* @ExcelProperty注解用来匹配excel表的字段名,下面使用name匹配,也可以使用index匹配。
*/
public class ExcelData {
/**
* 电缆名称
*/
@ApiModelProperty(value = "电缆名称")
@ExcelProperty("电缆名称")
private String name;
/**
* 首端设备ID号
*/
@ApiModelProperty(value = "首端设备ID号")
@ExcelProperty("首端设备ID号")
private String channelHeadName;
/**
* 首端设备通道号
*/
@ApiModelProperty(value = "首端设备通道号")
@ExcelProperty("首端设备通道号")
private Integer headId;
/**
* 首端接头名称
*/
@ApiModelProperty(value = "首端接头名称")
@ExcelProperty("首端接头名称")
private String headConnectorName;
/**
* 末端设备ID号
*/
@ApiModelProperty(value = "末端设备ID号")
@ExcelProperty("末端设备ID号")
private String channelTailName;
/**
* 末端设备通道号
*/
@ApiModelProperty(value = "末端设备通道号")
@ExcelProperty("末端设备通道号")
private Integer tailId;
/**
* 末端接头名称
*/
@ApiModelProperty(value = "末端接头名称")
@ExcelProperty("末端接头名称")
private String tailConnectorName;
/**
* 电缆长度
*/
@ApiModelProperty(value = "电缆长度")
@ExcelProperty("电缆长度")
private Double length;