ireport客户端打印

 

Ireport客户端打印

1. 新建一个web项目Outireport。如图:

 

 

2. lib导入ireport报表需要的包jasperreports-4.0.2.jar, jasperreports-applet-4.0.2.jar, commons-collections-2.1.jar, commons-logging-1.0.2.jar, groovy-all-1.7.5.jar, iText-2.1.7.jar, iTextAsian.jar(这几个表可能有个别包不是必须)。如图:

 

3. WebRoot中新建一个名为ireport的目录,里面存放打印的ireport报表,我这里是一个用户的报表。如图:

 

4. src中新建一个applet的目录,在applet中新建一个名为IreportServlet.javaservlet。代码如下:

 

Java代码 复制代码 收藏代码
  1. package applet;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.ObjectOutputStream;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8.  
  9. import javax.servlet.ServletException;
  10. import javax.servlet.ServletOutputStream;
  11. import javax.servlet.http.HttpServlet;
  12. import javax.servlet.http.HttpServletRequest;
  13. import javax.servlet.http.HttpServletResponse;
  14.  
  15. import net.sf.jasperreports.engine.JasperFillManager;
  16. import net.sf.jasperreports.engine.JasperPrint;
  17. import net.sf.jasperreports.engine.JasperReport;
  18. import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
  19. import net.sf.jasperreports.engine.util.JRLoader;
  20.  
  21. public class IreportServlet extends HttpServlet {
  22.  
  23. private static final long serialVersionUID = 1L;
  24. public void destroy() {
  25. super.destroy(); // Just puts "destroy" string in log
  26. }
  27.  
  28. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  29. throws ServletException, IOException {
  30. this.doPost(request, response);
  31. }
  32.  
  33. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  34. throws ServletException, IOException {
  35. List list=new ArrayList();
  36. try{
  37. Users users=null;
  38. for(int i=0;i<2;i++)
  39. {
  40. users=new Users();
  41. users.setUserName("yzp"+i);
  42. users.setSex(""+i);
  43. users.setPhone("13410479904");
  44. users.setPhone("19890214");
  45. list.add(users);
  46.  
  47. }
  48. JRBeanCollectionDataSource jds =new JRBeanCollectionDataSource(list); //使用javabean数据源
  49. String url=this.getServletContext().getRealPath("/")+"/ireport/users.jasper";//报表模板的文件路径
  50. File reportFile = new File(url);
  51. JasperReport report=(JasperReport) JRLoader.loadObject(reportFile.getPath());
  52. JasperPrint jasperPrint=null;
  53.  
  54. jasperPrint= JasperFillManager.fillReport(report, null, jds);//将所有的数据填充到jasperPrint,形成正常的报表
  55. response.setContentType("application/pdf");
  56. ServletOutputStream ouputStream = response.getOutputStream();
  57. ObjectOutputStream oos = new ObjectOutputStream(ouputStream);
  58. oos.writeObject(jasperPrint);//将JasperPrint对象写入对象输出流中
  59. oos.flush();
  60. oos.close();
  61. }catch(Exception e)
  62. {
  63. e.printStackTrace();
  64. }
  65. }
  66. }  
package applet;

import java.io.File;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

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.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.util.JRLoader;

public class IreportServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List list=new ArrayList();
		try{
			Users users=null;
			for(int i=0;i<2;i++)
			{
				users=new Users();
				users.setUserName("yzp"+i);
				users.setSex(""+i);
				users.setPhone("13410479904");
				users.setPhone("19890214");
				list.add(users);
				
			}
			JRBeanCollectionDataSource jds =new JRBeanCollectionDataSource(list);  //使用javabean数据源
			String url=this.getServletContext().getRealPath("/")+"/ireport/users.jasper";//报表模板的文件路径
			File reportFile = new File(url);
			JasperReport report=(JasperReport) JRLoader.loadObject(reportFile.getPath());
			JasperPrint jasperPrint=null;

			jasperPrint= JasperFillManager.fillReport(report, null, jds);//将所有的数据填充到jasperPrint,形成正常的报表
			response.setContentType("application/pdf"); 
			ServletOutputStream ouputStream = response.getOutputStream(); 
			ObjectOutputStream oos = new ObjectOutputStream(ouputStream); 
			oos.writeObject(jasperPrint);//将JasperPrint对象写入对象输出流中 
			oos.flush(); 
			oos.close(); 
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}

 

你可能感兴趣的:(打印,客户端)