java开发调用iReport生成Html报表

1 . 在java项目中新建个文件夹,将 iReport 制作生成的报表Copy到文件夹下,项目中加入相应的JAR包;

 2. 编写SpringBean.java:

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;


public class SpringBeanUtil implements ApplicationContextAware  {

    private static ApplicationContext applicationContext = null;


    /**
     * 取得存储在静态变量中的ApplicationContext.
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
     */
    public static Object getBean(String name) {
        return  applicationContext.getBean(name);
    }

    

    /**
     * 清除SpringContextHolder中的ApplicationContext为Null.
     */
    public static void clearHolder() {
        applicationContext = null;
    }

    /**
     * 实现ApplicationContextAware接口, 注入Context到静态变量中.
     */
    public void setApplicationContext(ApplicationContext applicationContext) {
      //  SpringContextHolder.applicationContext = applicationContext; 
        this.applicationContext = applicationContext;
    }

  
 
}


3.编写ReportServlet.java:

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.Map;

import javax.activation.DataSource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JasperRunManager;
public class MReportServlet extends HttpServlet {
	/**
	 * 打印HTML报表
	 *
	 */
	private static final long serialVersionUID=1L;

	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
//		DataSource dataSource = (DataSource) SpringBeanUtil.getBean("dataSource");
		try {
			response.setContentType("text/html;charset=UTF-8");
			request.setCharacterEncoding("UTF-8");
			
			PrintWriter printWriter = response.getWriter();
			java.io.File file = null;
			//得到项目中的iReport文件
			if(request.getParameter("rpt").equals("jhw")){
				 file = new java.io.File(this.getServletConfig().getServletContext().getRealPath("/iReport/InventoryOverflowForInOut.jasper"));
			}
			Class.forName("oracle.jdbc.driver.OracleDriver");
			Map map = new HashMap();
			Object object = request.getParameter("ticket_id");
			map.put("OVERFLOW_TICKET_ID",object.toString());
			java.sql.Connection connection =  DriverManager.getConnection("jdbc:oracle:thin:@192.168.70.31:1521:orcl","dms_dev","dms_dev");
			JasperRunManager.runReportToHtmlFile(file.getPath(), map, connection);
			response.sendRedirect("iReport/InventoryOverflowForInOut.html?id="+object);
			printWriter.flush();
			printWriter.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	
}


4. 编写Web.xml:

  
    ireport  
    com.ilogst.ireport.MReportServlet  
    
    
    ireport  
    /ireport  
   


5.启动项目 , 根据URl传过去的值 在 if else 里调用不同的报表

http://localhost:8080/iReport?rpt=jhw&ticket_id=PO201704170001 





你可能感兴趣的:(iReport打印报表)