spring到出excel

1.js&jsp
$("#excel").click(function() {
	    	$("#form1").attr("action","<%=contextPath%>/admin/trainning/listtrainningtoexcel");
			$("#form1").submit();
	    });

<button style="height:28px;" id="excel" class="marginL5">Excel</button></td>


2.backend
@RequestMapping(value = "admin/trainning/listtrainningtoexcel")
	public ModelAndView listToExcel(
			Trainning trainning,
			@RequestParam(value = "startDate", required = false) String startDate,
			@RequestParam(value = "endDate", required = false) String endDate,
			@RequestParam(value = "isEmployee", required = false) String isEmployee,
			@RequestParam(value = "status", required = false) String status,
			HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		List<Map<String, Object>> list = this.classService.searchTrainningList(
				trainning, startDate, endDate, status, isEmployee);
		mav.addObject("list", list);
		mav.setViewName("admin/trainning/tosearchExcel.jsp");
		return mav;
	}


3.tosearchExcel.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<jsp:directive.include file="/common/include.jsp" />
<html>
<%
	String contextPath = request.getContextPath();
%>
<% 
	response.setContentType("application/vnd.ms-excel");
	response.setHeader("Content-disposition", "filename=TrainingDeliveryReportExcel.xls");
	//response.setContentType("image/png");
  %>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<body>
	<form name="form1" method="post" id="form1">
	<div id="pageContent">

  <div> </div>
       <table class="">
        <tr>
          <td>姓名</td>
          <td>状态</td>
          <td>邮箱</td>
          <td>部门/公司</td>
          <td>产品线</td>
          <td>课程代码</td>
          <td>课程名字</td>
          <td>培训结束日期</td>
        </tr>
      
       	<c:forEach var="list" items="${list}" varStatus="v">
	         <c:if test="${v.count%2==0 }">
	         <tr bgcolor="B3D9FF">
	       </c:if>
	       	<c:if test="${v.count%2!=0 }">
	       		<tr>
	        </c:if>
	          	<td>${list.user_name}</td>
	          	<td>${list.status}</td>
		        <td>${list.email}</td>
		        <td>${list.department}</td>
		        <td>${list.modality}</td>
		        <td>${list.trainning_code}</td>
		        <td>${list.trainning_name}</td>
	          	<td> <fmt:formatDate value="${list.end_date}" pattern="yyyy-MM-dd HH:mm"/></td>
	        </tr>
        </c:forEach>
      </table>
  </div>
</form>
</body>

你可能感兴趣的:(spring,Excel)