Jmesa示例[分页,排序,导出CSV,EXCEL,PDF]

一 参考资料

1   http://blog.csdn.net/prufeng/archive/2009/05/24/4213218.aspx
2   http://blog.csdn.net/fenglibing/archive/2009/04/24/4106564.aspx
3   http://fansofjava.iteye.com/category/86946
4   http://blog.csdn.net/jockCreate/archive/2008/02/20/2110310.aspx

二 环境:XP+Myeclipse6.6+JDK1.5+Tomcat6.20

1 jmesa官方网址:  http://code.google.com/p/jmesa/
2 所需要的jar文件有:见附件图片:jmesa-lib.jpg
Jmesa示例[分页,排序,导出CSV,EXCEL,PDF]
Jmesa示例[分页,排序,导出CSV,EXCEL,PDF]
3 示例Java代码

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jmesa.facade.TableFacade;
import org.jmesa.facade.TableFacadeFactory;
import org.jmesa.limit.ExportType;

public class ExportServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {		
		doPost(request,response);
	}

	@SuppressWarnings("deprecation")
	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {		
		
		TableFacade tableFacade = TableFacadeFactory.createTableFacade("presidents", request);
		tableFacade.setColumnProperties("id","name.firstName", "name.lastName", "term","career", "born");       
        tableFacade.setItems(new PresidentDao().getPresidents());
        tableFacade.setExportTypes(response, ExportType.CSV, ExportType.EXCEL, ExportType.PDF);
        //直接展现方式
        String html = tableFacade.render();			
		request.setAttribute("presidents", html);
		String tag = request.getParameter("tag");
		String url = "/index.jsp";
		if("2".equals(tag)){
			url = "/index-el.jsp";
		}		
		
		try {   
			RequestDispatcher rd = request.getRequestDispatcher(url);   			
		    rd.forward(request, response);   
		    return;   
		}catch(Exception e){
			/**
			 * 不加上异常处理在控制台报如下错误:
			 *  严重: Servlet.service() for servlet ExportServlet threw exception
				java.lang.IllegalStateException: Cannot forward after response has been committed
					at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:312)
					at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
					at demo.ExportServlet.doPost(ExportServlet.java:55)
					at demo.ExportServlet.doGet(ExportServlet.java:36)
					at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
					at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
					at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
					at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
					at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
					at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
					at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
					at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
					at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
					at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
					at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
					at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
					at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
					at java.lang.Thread.run(Thread.java:619)
			 * */
		}  
		
	}
}


4 页面代码

<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>	
		<title>我的第一个jmesa测试示例</title>	
		<link rel="STYLESHEET" type="text/css" href="css/jmesa.css" />
		<link rel="STYLESHEET" type="text/css" href="css/jmesa-pdf.css" />
		<script type="text/javascript" src="js/jquery-1.3.min.js" /></script>
		<script type="text/javascript" src="js/jmesa.js" /></script>
		<script type="text/javascript" src="js/jquery.jmesa.js" /></script>		
		<script type="text/javascript">
			function onInvokeAction(id) {
			    setExportToLimit(id, '');
			    createHiddenInputFieldsForLimitAndSubmit(id);
			}
			function onInvokeExportAction(id) {       
			    var parameterString = createParameterStringForLimit(id);  			   
			    location.href = "<%= request.getContextPath() %>/ExportServlet?" + parameterString;       
			}   
			
		</script>
</head>
<body>
<form name="personForm" action="<%= request.getContextPath() %>/ExportServlet" method="post">
<table  border="0" align="center" cellpadding="0" cellspacing="0">
<tr><td  align="center" width="100%">${presidents}</td></tr>
</table>	
</form>
</body>
</html>

5 显示效果见附件:jmesa-demo.jpg
6 示例工程见:MyJmesa.rar

你可能感兴趣的:(java,apache,jsp,servlet,Excel)