直接看图
这里就出现双表头的头的导入
第一步 :导包
maven
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.2.0</version>
</dependency>
第二步:
model 层
导入的员工肯定有一个实体类对应。在实体类上加上 @Excel(name = “”)
@Entity
@Table(name = "employee")
public class Employee implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(nullable = false)
@Excel(name = "姓名")
private String empName;
@Column(nullable = false)
@Excel(name = "身份证号")
private String idCardNo;
@Column(nullable = false)
@Excel(name = "手机号")
private String phone;
@Excel(name = "工号")
private String empNo;
@Excel(name = "性别")
private int sex;
private Byte maritalStatus;
@Excel(name = "邮箱")
private String email;
@Excel(name = "QQ")
private String qq;
@Excel(name = "微信号")
private String wechat;
@Column(nullable = false)
private Long deptId;
private Long jobId;
@Excel(name = "员工状态")
private String empStatus;
@Column(nullable = false)
private Boolean isUser;
@Excel(name = "入职时间",format = "yyyy/MM/dd")
private Date hiredate;
private String remark;
private Date createTime;
private Long createBy;
@Column(nullable = false)
private Long guildId;
private Boolean isLeader;
@Excel(name = "民族")
private String nation;
@Excel(name = "户籍性质")
private String householdRegister;
@Excel(name = "户口所在地")
private String idAddress;
private String nowAddress;
@Excel(name = "籍贯")
private String nativePlace;
@Excel(name = "岗位")
private String postName;
@Excel(name = "转正时间",format = "yyyy/MM/dd")
private Date workdate;
@Excel(name = "合同结束日期",format = "yyyy/MM/dd" )
private Date enddate;
@Excel(name = "工资卡号",groupName = "账户信息",orderNum = "16")
private String bankCardNo;
@Excel(name = "开户行及支行")
private String openingBank;
@Excel(name = "名字",groupName = "紧急联系人",orderNum = "21")
private String urgentName;
@Excel(name = "关系")
private String urgentRelationship;
@Excel(name = "电话")
private String urgentPhone;
private String laborUrl;
private String diplomaUrl;
private String identityUrl;
private String nickName;
@Excel(name = "部门",groupName = "职位信息",orderNum = "13")
private String dept;
@Excel(name = "二级部门(运营分组)")
private String twoDept;
一般来说都是写一个 @Excel(name = “电话”) 对应excel的表头就可以,但是有些就是数据就是对应不上。
这个时候你就需要用到
@Excel(name = “部门”,groupName = “职位信息”,orderNum = “13”)
这样实体类的工作就算完成了,日期的很简单转换的,看你传入什么格式,你用format 属性 标记一下就可以
就下来就是导入了
第三步
controller
@RequestMapping(value = "/importEmployeeExcel", method = RequestMethod.POST)
@ApiOperation(value = "导入员工表", notes = "", response = Result.class)
public Result importEmployeeExcel(MultipartFile multipartFile) throws IOException {
ImportParams params = new ImportParams();
// 这里是设置标题的行数。一般都是一行(就是上面第一张图里面的员工信息一行),也有没有标题的,没有标题行就设置为0
params.setTitleRows(1);
// 这里就是双表头的行数。所以它是2 ,因为表头行占了2行。所以如果你只有一行,就是1
params.setHeadRows(2);
// 这个设置的是 sheet 的页数,有可能一个Excel有多个sheet ,默认从0开始
params.setStartSheetIndex(0);
try {
// 这个直接复制过去,改一下实体类就可以
List<Employee> employeeList = ExcelImportUtil.importExcel( multipartFile.getInputStream(), Employee.class, params);
System.out.println(employeeList);
this.employeeService.insertEmployeeList(employeeList);
} catch (Exception e) {
e.printStackTrace();
}
return super.success();
}
当然,这只是我自己做的,测试是没有问题的,如果不能解决你的问题请到官网easyPOI官网
————————————————
版权声明:本文为CSDN博主「memory_cood」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/memory_cood/article/details/104477598