EasyExcel导出excel
1.首先建立一个实体类
public class ExcelMode extends BaseRowModel {
@ExcelIgnore //excel中不导出字段
private Integer id;
@ExcelProperty(value = "姓名" ,index = 0)
private String name;
@ExcelProperty(value = "密码" ,index = 1)
private String password;
@ExcelProperty(value = "手机号码",index = 2)
private String phone;
@ExcelProperty(value = "性别",index = 2,converter = SettlementConverter.class )
//SettlementConverter.class 将数据库枚举字段转换成字符串类型(例:1/男,2/女
private Integer sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
}
- SettlementConverter.class 实现 Converter 接口
public class SettlementConverter implements Converter<Integer> {
@Override
public Class supportJavaTypeKey() {
return null;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return null;
}
@Override
public Integer convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
return null;
}
@Override
public CellData convertToExcelData(Integer s, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
if(s == 1){
return new CellData("男");
}else {
return new CellData("女");
}
}
- 导出excel
@RequestMapping(value = "/toExcel")
@ResponseBody
public Object toExcel(HttpServletRequest rep , HttpServletResponse reps) {
try {
ExcelMode u = new ExcelMode();
List<ExcelMode> list = userservice.ExcelMode();
String fileName = new String("文件名称.xlsx".getBytes(), "ISO-8859-1");
reps.addHeader("Content-Disposition", "filename=" + fileName);
ServletOutputStream out = reps.getOutputStream();
EasyExcelFactory.write(out, ExcelMode.class).sheet("文件标签名称").doWrite(list);
}catch (Exception e){
e.printStackTrace();
}
return null;
}