导出Excel功能有多种方式,这里简单介绍一下直接将jsp导出方式。
直接上代码:
页面添加导出按钮,进入后台:
@RequestMapping(value = "/exportList") public ModelAndView exportList(HttpServletRequest request) { try { ModelAndView mv = new ModelAndView("/foo.jsp");//指定导出jsp页面 ......//业务逻辑 mv.addObject("list", list);//返回列表 mv.addObject("exportToExcel", "YES"); mv.addObject("nowTime", DateUtil.DateToString(new Date(), "yyyyMMddHHmmss")); return mv; }catch (Exception e) { e.printStackTrace(); } return null; }
foo.jsp 页面添加如下代码,就会将该页面jsp原样导出
<% String exportToExcel = (String)request.getAttribute("exportToExcel"); String nowTime = (String)request.getAttribute("nowTime"); if (exportToExcel != null && exportToExcel.toString().equalsIgnoreCase("YES")) { response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment; filename=" +new String(("月报-"+nowTime).getBytes("gbk"),"iso8859-1")+".xls"); } %>
这样就能导出Excel了。