前台代碼:
$("#btnExport").click(function(){ top.$.jBox.confirm("確認要導出房屋信息嗎?","系統提示",function(v,h,f){ if(v=="ok"){ $("#searchForm").attr("action","${ctx}/sys_house_apply/sysHouseApply/exportsd"); $("#searchForm").submit(); } $("#searchForm").attr("action","${ctx}/sys_house_apply/sysHouseApply/houseList"); },{buttonsFocus:1}); top.$('.jbox-body .jbox-icon').css('top','55px'); })
<input id="btnExport" style="font-size:13px;width: 40px;padding-left: 6px;height: 26px;padding-top: 2.5px;" class="btn btn-primary" type="button" value="導出"/>
給導出按鈕(id=”btnExport“綁定點擊方法),此處使用jBox提示框 form表單提交方式
后台代碼:
controller中代碼
@RequestMapping(value = "exportsd",method=RequestMethod.POST) public String exportproject(SysHouseApply sysHouseApply,HttpServletRequest request, //SysHouseApply:前台form表單中的對象 HttpServletResponse response,RedirectAttributes redirectAttributes)throws Exception{ List<SysHouseApply> waitList = new ArrayList<SysHouseApply>() ;//查詢需要的數據try{ SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");//格式化時間 String fileName = "demo"+DateUtils.getDate("yyyyMMddHHmmss");//導出的文件名 //用來存放導出的數據 List<SysHouseApplyException> exporthistor = new ArrayList<SysHouseApplyException>(); for(int i=0;i<waitList.size();i++){ SysHouseApplyException obj1 = new SysHouseApplyException();
obj1.setHouseId(waitList.get(i).getHouseId());//將查詢出的數據 set到要導出的文件對象中
exporthistor.add(obj1); } new ExportExcel("房屋信息", SysHouseApplyException.class).setDataList(exporthistor).write(response, fileName).dispose();//SysHouseApplyException導出文件對象 return null; } catch (Exception e) { addMessage(redirectAttributes, "導出房屋信息失敗!失敗信息:"+e.getMessage()); } return "redirect:"+Global.getAdminPath()+"/sys_house_apply/sysHouseApply/houseList?repage";//頁面查詢連接 }
SysHouseApplyException導出文件對象
/**
* 租房信息Entity
*/
public class SysHouseApplyException extends DataEntity<SysHouseApplyException> {
private static final long serialVersionUID = 1L;
private String houseId;//房屋Id
@ExcelField(title="房屋ID", align=2, sort=1)
public String getHouseId() {
return houseId;
}
public void setHouseId(String houseId) {
this.houseId = houseId;
}
}