Java代码段
/**
* 人员信息导出
*/
@ApiOperation(value = "导出人员信息列表", notes = "导出人员信息列表接口")
@ApiImplicitParams({
@ApiImplicitParam(name = "USERID", value = "用户名"),
@ApiImplicitParam(name = "USERNAME", value = "姓名"),
@ApiImplicitParam(name = "EMP_CODE", value = "职工号")
})
@GetMapping("export")
public ResultData<List<PersonExcel>> export(@ApiIgnore ModelMap map, HttpServletRequest request, HttpServletResponse response, PersonParams personParams) {
ResultData<List<PersonExcel>> result = new ResultData<List<PersonExcel>>();
try {
List<PersonExcel> list = personService.getPersonList(personParams);
ExportParams params = new ExportParams(null, "人员信息", ExcelType.XSSF);
if (!ToolsUtil.isEmpty(list)) {
map.put(TemplateExcelConstants.FILE_NAME, "人员信息");
map.put(NormalExcelConstants.DATA_LIST, list);
map.put(NormalExcelConstants.CLASS, PersonExcel.class);
map.put(NormalExcelConstants.PARAMS, params);
PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
return result.returnMessage(HttpStatus.SUCCESS, "导出成功");
} else {
return result.returnMessage(HttpStatus.SUCCESS, "无数据");
}
} catch (Exception e) {
e.printStackTrace();
}
return result.returnMessage(HttpStatus.ERROR, "导出失败");
}
PersonExcel
import java.util.Date;
@Data
public class PersonExcel {
/**
* ID
*/
@ExcelIgnore
private Long id;
/**
* 用户名
*/
@Excel(name = "用户名", width = 20)
private String userId;
/**
* 姓名
*/
@Excel(name = "姓名", width = 20)
private String userName;
/**
* 职工号
*/
@Excel(name = "职工号", width = 20)
private String empCode;
/**
* 性别
*/
@Excel(name = "性别", width = 10)
private String sex;
/**
* 密码
*/
@ExcelIgnore
private String password;
/**
* 身份证号
*/
@Excel(name = "身份证号", width = 30)
private String personCode;
/**
* 相片
*/
@ExcelIgnore
private String photo;
/**
* 手机号
*/
@Excel(name = "手机", width = 30)
private Long mobilePhone;
/**
* 固定电话
*/
@Excel(name = "座机", width = 30)
private Long phone;
}
pom.xml (全用比较保险吧,懒得去测试排除哪些不需要了)
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.13</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>