1. 程式人生 > >java+jasperreport開發web報表

java+jasperreport開發web報表

本文以生成pdf為例:

1,引入jar


2,JRPDFServlet.java

package net.xnzz.servlet;


import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.util.Enumeration;
import java.util.HashMap;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperRunManager;
import net.xnzz.util.DBUtils;

/**
 * jasperreport 生成pdf檔案
 * @author WANGJIE
 *
 */
public class JRPDFServlet extends HttpServlet 
{
	private static final long serialVersionUID = 1L;

	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException 
	{
		Connection connection = DBUtils.getDBInstance().getInitDBConnection();
		try 
		{
			//據據jasper檔案生成JasperPrint物件
			ServletContext context = this.getServletConfig().getServletContext();
			String fileName = request.getParameter("fileName");//ireport編譯檔案:*.jasper(由模板檔案*.jrxml檔案編譯生成)
			File reportFile = new File(context.getRealPath("/WEB-INF/jaspers/"+fileName));
			HashMap<String, Object> parameters = new HashMap<String, Object>();//給報表模板檔案傳參

			//得到列舉型別的引數名稱,引數名稱若有重複的只能得到第一個--獲取頁面傳來的引數,和模板中檔案的sql引數名稱一一對應
			Enumeration temp = request.getParameterNames();
			   while (temp.hasMoreElements()) 
			   {
			    String paramName = (String) temp.nextElement();
			    String paramValue = request.getParameter(paramName);
			    parameters.put(paramName, paramValue);
			   }
			byte[] bytes = JasperRunManager.runReportToPdf(reportFile.getPath(), parameters,connection);
			response.setContentType("application/pdf");
			response.setContentLength(bytes.length);
			ServletOutputStream out = response.getOutputStream();
			out.write(bytes, 0, bytes.length);
			out.flush();
			out.close();
		} 
		catch (JRException e) 
		{
			e.printStackTrace();
		}		
	}
}

3,print**.jsp
<iframe id="reportFrame" width="100%" height="100%"  frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="no" allowtransparency="yes"></iframe>  
<input type="button"  value="下載"  onclick="getSearch()"  class="butn" />
<script type="text/javascript">
function getSearch()
{
	   var con = $("#con").val();
	   var kdCode = $("#kdCode").val();
	    if(kdCode=="")
	   {
	   		alert("請選擇考點");
	   		return;
	   }
	   var path = "${pageContext.request.contextPath}/servlet/JRPDFServlet?kdcode="+kdCode+"&fileName=printzq.jasper";
	   $("#reportFrame").attr("src",path);
}
</script>
4,web.xml
<servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>JRPDFServlet</servlet-name>
    <servlet-class>net.xnzz.servlet.JRPDFServlet</servlet-class>
  </servlet>
<servlet-mapping>
    <servlet-name>JRPDFServlet</servlet-name>
    <url-pattern>/servlet/JRPDFServlet</url-pattern>
  </servlet-mapping>
5,printzq.jrxml

模板檔案的sql面板及其引數截圖: