java文件下载导出
前台代码:
$("#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;
}
}