1.maven依赖
<!-- easypoi 依赖 excel导出-->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>4.1.3</version>
</dependency>
2.实体类编写
表示 导出名
@Excel(name ="员工姓名")
表示导出的类
@ExcelEntity(name = "部门")
要在该类上 要导出字段添加 @Excel(name ="员工姓名") 注解
表示日期格式输入
@JsonFormat(pattern = "yyyy-MM-dd",timezone = "Asia/Shanghai")
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "员工编号")
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@ApiModelProperty(value = "员工姓名")
@Excel(name ="员工姓名")
private String name;
@ApiModelProperty(value = "性别")
@Excel(name ="性别")
private String gender;
@ApiModelProperty(value = "出生日期")
@JsonFormat(pattern = "yyyy-MM-dd",timezone = "Asia/Shanghai")
@Excel(name ="出生日期",width = 20,format = "yyyy-MM-dd")
private LocalDate birthday;
@ApiModelProperty(value = "身份证号")
@Excel(name ="身份证号",width = 30)
private String idCard;
@ApiModelProperty(value = "婚姻状况")
@Excel(name ="婚姻状况")
private String wedlock;
@ApiModelProperty(value = "民族")
private Integer nationId;
@ApiModelProperty(value = "籍贯")
@Excel(name ="籍贯")
private String nativePlace;
@ApiModelProperty(value = "政治面貌")
private Integer politicId;
@ApiModelProperty(value = "邮箱")
@Excel(name ="邮箱",width = 30)
private String email;
@ApiModelProperty(value = "电话号码")
@Excel(name ="电话号码",width = 15)
private String phone;
@ApiModelProperty(value = "联系地址")
@Excel(name ="电话号码",width = 40)
private String address;
@ApiModelProperty(value = "所属部门")
private Integer departmentId;
@ApiModelProperty(value = "职称ID")
private Integer jobLevelId;
@ApiModelProperty(value = "职位ID")
private Integer posId;
@ApiModelProperty(value = "聘用形式")
@Excel(name ="聘用形式",width = 15)
private String engageForm;
@ApiModelProperty(value = "最高学历")
@Excel(name ="最高学历",width = 15)
private String tiptopDegree;
@ApiModelProperty(value = "所属专业")
@Excel(name ="所属专业",width = 15)
private String specialty;
@ApiModelProperty(value = "毕业院校")
@Excel(name ="毕业院校",width = 15)
private String school;
@ApiModelProperty(value = "入职日期")
@JsonFormat(pattern = "yyyy-MM-dd",timezone = "Asia/Shanghai")
@Excel(name ="出生日期",width = 20,format = "yyyy-MM-dd")
private LocalDate beginDate;
@ApiModelProperty(value = "在职状态")
@Excel(name ="在职状态",width = 20)
private String workState;
@ApiModelProperty(value = "工号")
@Excel(name ="工号",width = 20)
private String workID;
@ApiModelProperty(value = "合同期限")
@Excel(name ="合同期限",suffix = "年")
private Double contractTerm;
@ApiModelProperty(value = "转正日期")
@JsonFormat(pattern = "yyyy-MM-dd",timezone = "Asia/Shanghai")
@Excel(name ="转正日期",width = 20,format = "yyyy-MM-dd")
private LocalDate conversionTime;
@ApiModelProperty(value = "离职日期")
@JsonFormat(pattern = "yyyy-MM-dd",timezone = "Asia/Shanghai")
@Excel(name ="离职日期",width = 20,format = "yyyy-MM-dd")
private LocalDate notWorkDate;
@ApiModelProperty(value = "合同起始日期")
@JsonFormat(pattern = "yyyy-MM-dd",timezone = "Asia/Shanghai")
@Excel(name ="合同起始日期",width = 20,format = "yyyy-MM-dd")
private LocalDate beginContract;
@ApiModelProperty(value = "合同终止日期")
@JsonFormat(pattern = "yyyy-MM-dd",timezone = "Asia/Shanghai")
@Excel(name ="合同终止日期",width = 20,format = "yyyy-MM-dd")
private LocalDate endContract;
@ApiModelProperty(value = "工龄")
@Excel(name ="工龄")
private Integer workAge;
@ApiModelProperty(value = "工资账套ID")
private Integer salaryId;
@TableField(exist = false)
@ApiModelProperty("民族")
@ExcelEntity(name = "民族")
private Nation nation;
@TableField(exist = false)
@ApiModelProperty("政治面貌")
@ExcelEntity(name = "政治面貌")
private PoliticsStatus politicsStatus;
@TableField(exist = false)
@ApiModelProperty("部门")
@ExcelEntity(name = "部门")
private Department department;
@TableField(exist = false)
@ApiModelProperty("职称")
@ExcelEntity(name = "职称")
private Joblevel joblevel;
@TableField(exist = false)
@ApiModelProperty("职位")
@ExcelEntity(name = "职位")
private Position position;
3. 编写导出操作
重点
filename =
@ApiOperation(value = "导出员工数据")
@GetMapping(value = "/export",produces = "application/octet-stream")
public void exportEmployee(HttpServletResponse response){
List<Employee> list = employeeService.getEmployee(null);
ExportParams params = new ExportParams("员工表","员工表", ExcelType.HSSF);
Workbook workbook = ExcelExportUtil.exportExcel(params, Employee.class, list);
ServletOutputStream outputStream=null;
try {
response.setHeader("content-type","application/octet-stream");
response.setHeader("content-disposition","attachment;filename="+ URLEncoder.encode("员工表.xls","UTF-8"));
outputStream = response.getOutputStream();
workbook.write(outputStream);
} catch (IOException e) {
e.printStackTrace();
}finally {
if (outputStream!=null) {
try {
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}