一.控制器
public void export(HttpServletRequest request, HttpServletResponse response) throws IOException {
List<SysUserEntity> list=sysUserService.queryAllList(); //查询结果集
ExportParams exportParams = new ExportParams(null,new SimpleDateFormat("yyyyMMdd").format(new Date()));
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, SysUserExportEntity.class,list); //SysUserExportEntity 为导出实体类 通过注解方式来配置标题
ServletOutputStream outputStream=null;
try{
String fileName="用户列表";
response.reset();
response.setContentType("application/x-download;charset=utf-8");
response.setHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xls");
outputStream=response.getOutputStream();
workbook.write(outputStream);
}catch (Exception e){
e.printStackTrace();
}finally {
workbook.close();
outputStream.close();
}
}
二.实体类
@TableName("sys_user")
public class SysUserExportEntity implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 用户ID
*/
@Excel(name="用户编号",width = 20)
private Long userId;
/**
* 用户名
*/
@Excel(name="用户名称",width = 20)
private String username;
/**
* 密码
*/
@Excel(name="密码",width = 20)
private String password;
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}