model层
/**
* 用户信息Entity
* @author dongf
* @version 2018-09-08
*/
public class UserInfo extends DataEntity<UserInfo> {
private static final long serialVersionUID = 1L;
private String headImg; // 用户头像
private String name; // 昵称
private String realName;
private String phone; // 手机号码
private String wechatId; // 微信id
private String province; // 省
private String city; // 市
private String area; // 区
private String address; // 地址
private String isNew; // 是否为新用户
public UserInfo() {
super();
}
public UserInfo(Integer id){
super(id);
}
@Length(min=0, max=255, message="用户头像长度必须介于 0 和 255 之间")
public String getHeadImg() {
return headImg;
}
public void setHeadImg(String headImg) {
this.headImg = headImg;
}
@Length(min=0, max=255, message="用户昵称长度必须介于 0 和 255 之间")
@ExcelField(title = "昵称", type = 0, align = 2, sort = 0)
@NotEmpty
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Length(min=0, max=255, message="用户姓名长度必须介于 0 和 255 之间")
@ExcelField(title = "真实姓名", type = 0, align = 2, sort = 1)
@NotEmpty
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
@Length(min=0, max=255, message="手机号码长度必须介于 0 和 255 之间")
@ExcelField(title = "手机号码", type = 0, align = 2, sort = 2)
@NotEmpty
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Length(min=0, max=255, message="微信id长度必须介于 0 和 255 之间")
public String getWechatId() {
return wechatId;
}
public void setWechatId(String wechatId) {
this.wechatId = wechatId;
}
@Length(min=0, max=255, message="省长度必须介于 0 和 255 之间")
@ExcelField(title = "省", type = 0, align = 2, sort = 3)
@NotEmpty
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
@Length(min=0, max=255, message="市长度必须介于 0 和 255 之间")
@ExcelField(title = "市", type = 0, align = 2, sort = 4)
@NotEmpty
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Length(min=0, max=255, message="区长度必须介于 0 和 255 之间")
@ExcelField(title = "区", type = 0, align = 2, sort = 5)
@NotEmpty
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
@Length(min=0, max=255, message="地址长度必须介于 0 和 255 之间")
@ExcelField(title = "地址", type = 0, align = 2, sort = 6)
@NotEmpty
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Length(min=0, max=255, message="是否为新用户长度必须介于 0 和 255 之间")
public String getIsNew() {
return isNew;
}
public void setIsNew(String isNew) {
this.isNew = isNew;
}
}
controller层
/**
* 用户信息Controller
* @author dongf
* @version 2018-09-08
*/
@Controller
@RequestMapping(value = "${adminPath}/user/userInfo")
public class UserInfoController extends BaseController {
@Autowired
private UserInfoService userInfoService;
/**
* @Description:用户信息列表页面
* @author dongf
*/
@RequestMapping("index")
public String index(){
return bgPath + "user/userInfoList";
}
/**
* @Description:用户信息列表数据
* @author dongf
*/
@RequestMapping("list")
@ResponseBody
public Grid list(UserInfo userInfo, GridParam param) {
return userInfoService.list(userInfo, param);
}
@RequestMapping(value = "export", method = RequestMethod.GET)
@ResponseBody
public String exportFile(UserInfo userInfo, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
try {
String fileName = "用户数据"+ DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
Page<UserInfo> page = userInfoService.findPage(new Page<UserInfo>(request, response, -1), userInfo);
new ExportExcel("用户数据", UserInfo.class).setDataList(page.getList()).write(response, fileName).dispose();
return null;
} catch (Exception e) {
addMessage(redirectAttributes, "导出用户失败!失败信息:"+e.getMessage());
}
return "redirect:" + adminPath + "/user/userInfo/list?repage";
}
}
view层
<button class="btn btn-primary " type="button" onclick="exportUserInfo()"><i class="fa fa-search"></i> 导出excel</button>
<script type="text/javascript">
function exportUserInfo() {
<%--window.location.href = "${ctx}/user/userInfo/export";--%>
var url = '${ctx}/user/userInfo/export';
$('<form method="get" action="' + url + '"></form>').appendTo('body').submit().remove();
}
</script>
`