`
hanjiangit
  • 浏览: 185395 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

jasperreports-1.3.3与iReport1.3.3初级使用

阅读更多

这几天多在用jasperreports写下一点入门的东西。这里就不讲在ireport里连接jdbc了,不切实际。单说在应用里用javabean提供数据实现报表展现。

不讲别的就贴代码了。具体的操作在代理注释。下面有附用ireport图形界面连接jdbc生成报表。要添加的jar也附上。我开始练手的时候找的要死,因为多是以前的版本。配用jar很难找。还有介绍个根据包名查找具体jar的网站,应该很多人多指导了www.findjar.com,但是好像也有好些超找不到。

以下是文本报表

javabean:

package lld.test.ireport.zhj;


public class Student


{


	private String name;


	private String address;


	private int age;


	private String sex;


	public Student(){}


	public Student(String name, String address, int age, String sex)


	{
















































































		super();
















































































		this.name = name;
















































































		this.address = address;
















































































		this.age = age;
















































































		this.sex = sex;
















































































	}
















































































	
















































































	public String getName()
















































































	{
















































































		return name;
















































































	}
















































































	public void setName(String name)
















































































	{
















































































		this.name = name;
















































































	}
















































































	public String getAddress()
















































































	{
















































































		return address;
















































































	}
















































































	public void setAddress(String address)
















































































	{
















































































		this.address = address;
















































































	}
















































































	public int getAge()
















































































	{
















































































		return age;
















































































	}
















































































	public void setAge(int age)
















































































	{
















































































		this.age = age;
















































































	}
















































































	public String getSex()
















































































	{
















































































		return sex;
















































































	}
















































































	public void setSex(String sex)
















































































	{
















































































		this.sex = sex;
















































































	}
















































































}

 makeData:

package lld.test.ireport.zhj;

































































































































































import java.util.ArrayList;

































































































































































/**
















































































 * 数据制造类
















































































 * @author Administrator
















































































 * @param <T>
















































































 */
















































































public class MakeData<T>
















































































{
















































































	private ArrayList<T> listData = new ArrayList<T>();
















































































	
















































































	public ArrayList<T> getData()
















































































	{
















































































		return listData;
















































































	}

































































































































































	public void addData(T t)
















































































	{
















































































		listData.add(t);
















































































	}
















































































}

 servlet:

package lld.test.ireport.zhj;

































































































































































import java.io.File;
















































































import java.io.IOException;
















































































import java.io.PrintWriter;
















































































import java.net.URLEncoder;
















































































import java.util.ArrayList;
















































































import javax.servlet.ServletException;
















































































import javax.servlet.http.HttpServlet;
















































































import javax.servlet.http.HttpServletRequest;
















































































import javax.servlet.http.HttpServletResponse;
















































































import net.sf.jasperreports.engine.JRDataSource;
















































































import net.sf.jasperreports.engine.JRExporter;
















































































import net.sf.jasperreports.engine.JRExporterParameter;
















































































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.export.JRHtmlExporter;
















































































import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
















































































import net.sf.jasperreports.engine.export.JRPdfExporter;
















































































import net.sf.jasperreports.engine.export.JRRtfExporter;
















































































import net.sf.jasperreports.engine.export.JRXlsAbstractExporter;
















































































import net.sf.jasperreports.engine.export.JRXlsExporter;
















































































import net.sf.jasperreports.engine.util.JRLoader;
















































































import net.sf.jasperreports.j2ee.servlets.ImageServlet;

































































































































































//javabean数据源 单纯报表
















































































public class ZhjBeanRepServlet extends HttpServlet
















































































{
















































































	@Override
















































































	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
















































































			throws ServletException, IOException
















































































	{
















































































		this.doPost(req, resp);
















































































	}

































































































































































	@Override
















































































	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
















































































			throws ServletException, IOException
















































































	{
















































































		int reportType = Integer.parseInt(req.getParameter("reportType"));
















































































		MakeData makeData = new MakeData();
















































































		makeData.addData(new Student("赵汉江", "湖南", 22, "男"));
















































































		makeData.addData(new Student("王冠峰", "河南", 22, "男"));
















































































		makeData.addData(new Student("小钱", "安徽", 22, "男"));
















































































		makeData.addData(new Student("小胡", "江西", 22, "男"));
















































































		makeData.addData(new Student("赵汉江", "湖南", 22, "男"));
















































































		makeData.addData(new Student("赵汉江", "湖南", 22, "男"));
















































































		makeData.addData(new Student("赵汉江", "湖南", 22, "男"));
















































































		makeData.addData(new Student("赵汉江", "湖南", 22, "男"));
















































































		makeData.addData(new Student("赵汉江", "湖南", 22, "男"));
















































































		makeData.addData(new Student("赵汉江", "湖南", 22, "男"));
















































































		makeData.addData(new Student("赵汉江", "湖南", 22, "男"));
















































































		makeData.addData(new Student("赵汉江", "湖南", 22, "男"));
















































































		ArrayList<Student> listStudent = makeData.getData();
















































































		String reportFilePath = this.getServletContext().getRealPath("/").replace('\\',
















































































				'/')
















































































				+ "WEB-INF/classes/lld/test/ireport/zhj/zhjtest.jasper";
















































































		File file = new File(reportFilePath);
















































































		if (!file.exists())
















































































		{
















































































			try
















































































			{
















































































				throw new Exception("jasper文件不存在:" + reportFilePath);
















































































			}
















































































			catch (Exception e)
















































































			{
















































































				e.printStackTrace();
















































































			}
















































































		}
















































































		JRDataSource dataSource = new JRBeanCollectionDataSource(listStudent);
















































































		switch (reportType)
















































































		{
















































































		case ReportDefine.REPORT_PDF:
















































































			try
















































































			{
















































































				exportPdf(req, resp, reportFilePath, dataSource);
















































































			}
















































































			catch (Exception e1)
















































































			{
















































































				e1.printStackTrace();
















































































			}
















































































			break;
















































































		case ReportDefine.REPORT_EXCEL:
















































































			try
















































































			{
















































































				exportExcel(req, resp, reportFilePath, dataSource);
















































































			}
















































































			catch (Exception e1)
















































































			{
















































































				e1.printStackTrace();
















































































			}
















































































			break;
















































































		case ReportDefine.REPORT_HTML:
















































































			try
















































































			{
















































































				exportHtml(req, resp, reportFilePath, dataSource);
















































































			}
















































































			catch (Exception e1)
















































































			{
















































































				e1.printStackTrace();
















































































			}
















































































			break;
















































































		case ReportDefine.REPORT_DOC:
















































































			try
















































































			{
















































































				exportDoc(req, resp, reportFilePath, dataSource);
















































































			}
















































































			catch (Exception e1)
















































































			{
















































































				e1.printStackTrace();
















































































			}
















































































			break;
















































































		default:
















































































			try
















































































			{
















































































				throw new Exception("报表输出类别错误:" + reportType);
















































































			}
















































































			catch (Exception e)
















































































			{
















































































				e.printStackTrace();
















































































			}
















































































		}
















































































	}

































































































































































	private JasperPrint getJasperPrint(String reportFilePath, JRDataSource dataSource)
















































































			throws Exception
















































































	{
















































































		JasperReport report = (JasperReport) JRLoader.loadObject(reportFilePath);
















































































		JasperPrint print = JasperFillManager.fillReport(report, null, dataSource);
















































































		return print;
















































































	}

































































































































































	protected void exportPdf(HttpServletRequest req, HttpServletResponse resp,
















































































			String reportFilePath, JRDataSource dataSource) throws Exception
















































































	{
















































































		 System.out.println("export PDF report by JasperPrint...");
















































































		 JasperPrint jasperPrint = this.getJasperPrint(reportFilePath, dataSource);
















































































//		 OutputStream ouputStream = resp.getOutputStream();
















































































		 resp.setContentType("application/pdf");
















































































		 resp.setCharacterEncoding("UTF-8");
















































































		 resp.setHeader("Content-Disposition", "attachment; filename=\""
















































































		 + URLEncoder.encode("PDF报表", "UTF-8") + ".pdf\"");
















































































		// //使用JRPdfExproter导出器导出pdf
















































































		// JRPdfExporter exporter = new JRPdfExporter();
















































































		// exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
















































































		// exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
















































































		// exporter.exportReport();
















































































		// ouputStream.close();

































































































































































		// System.out.println("export PDF report by JasperRunManager...");
















































































		// OutputStream outputStream = resp.getOutputStream();
















































































		// resp.setContentType("application/pdf");
















































































		// resp.setCharacterEncoding("UTF-8");
















































































		// resp.setHeader("Content-Disposition", "attachment; filename=\""
















































































		// + URLEncoder.encode("PDF报表", "UTF-8") + ".pdf\"");
















































































		// byte[] bytes = JasperRunManager.runReportToPdf(reportFilePath, null,
















































































		// dataSource);
















































































		// outputStream.write(bytes, 0, bytes.length);
















































































		// outputStream.close();

































































































































































		// 生成我们的导出类JRPdfExporter 来自JRExporter
















































































		JRPdfExporter jrpdfExporter = new JRPdfExporter();
















































































		jrpdfExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
















































































		jrpdfExporter.setParameter(JRExporterParameter.OUTPUT_STREAM,resp.getOutputStream());
















































































		jrpdfExporter.exportReport();
















































































	}

































































































































































	protected void exportExcel(HttpServletRequest req, HttpServletResponse resp,
















































































			String reportFilePath, JRDataSource dataSource) throws Exception
















































































	{
















































































		System.out.println("export Excel report...");
















































































		JasperPrint jasperPrint = this.getJasperPrint(reportFilePath, dataSource);
















































































		JRXlsAbstractExporter exporter = new JRXlsExporter();
















































































		resp.setContentType("application/xls;charset=utf-8");
















































































		resp.setHeader("Content-Disposition", "attachment; filename=\""
















































































				+ URLEncoder.encode("Excel报表", "UTF-8") + ".xls\"");
















































































		exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
















































































		exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, resp.getOutputStream());
















































































		exporter.exportReport();
















































































	}

































































































































































	protected void exportHtml(HttpServletRequest req, HttpServletResponse resp,
















































































			String reportFilePath, JRDataSource dataSource) throws Exception
















































































	{
















































































		System.out.println("export HTML report...");
















































































		resp.setCharacterEncoding("UTF-8");
















































































		PrintWriter out = resp.getWriter();
















































































		resp.setContentType("text/html;charset=UTF-8");
















































































		JasperPrint jasperPrint = this.getJasperPrint(reportFilePath, dataSource);
















































































		JRHtmlExporter exporter = new JRHtmlExporter();
















































































		req.getSession().setAttribute(
















































































				ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
















































































		exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
















































































		exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
















































































		exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,
















































































				Boolean.FALSE);
















































































		exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
















































































		exporter.exportReport();
















































































	}

































































































































































	protected void exportDoc(HttpServletRequest req, HttpServletResponse resp,
















































































			String reportFilePath, JRDataSource dataSource) throws Exception
















































































	{
















































































		JasperPrint jasperPrint = this.getJasperPrint(reportFilePath, dataSource);
















































































		JRExporter exporter = new JRRtfExporter();
















































































		resp.setContentType("application/msword;charset=utf-8");
















































































		resp.setHeader("Content-Disposition", "attachment; filename=" + "doc报表.doc");
















































































		exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
















































































		exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, resp.getOutputStream());
















































































		exporter.exportReport();
















































































	}
















































































}

 web.xml:

<?xml version="1.0" encoding="UTF-8"?>
















































































<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
















































































	<display-name>ireport_test</display-name>
















































































	
















































































	<servlet>
















































































		<servlet-name>ZhjReport</servlet-name>
















































































		<servlet-class>lld.test.ireport.zhj.ZhjBeanRepServlet</servlet-class>
















































































	</servlet>
















































































	
















































































	<servlet-mapping>
















































































		<servlet-name>ZhjReport</servlet-name>
















































































		<url-pattern>/zhj_rep.do</url-pattern>
















































































	</servlet-mapping>
















































































	
















































































	<welcome-file-list>
















































































		<welcome-file>index.jsp</welcome-file>
















































































	</welcome-file-list>
















































































</web-app>

 jsp:

<%@ page language="java" contentType="text/html; charset=GB18030"
















































































    pageEncoding="GB18030"%>
















































































<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
















































































<html>
















































































<head>
















































































<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
















































































<title>Insert title here</title>
















































































</head>
















































































<body>
















































































<ul>
















































































	<li>JavaBean数据源报表
















































































		<ul>
















































































			<li><a href="zhj_rep.do?reportType=1">Pdf格式</a></li>
















































































			<li><a href="zhj_rep.do?reportType=2">Excel格式</a></li>
















































































			<li><a href="zhj_rep.do?reportType=3">HTML格式</a></li>
















































































			<li><a href="zhj_rep.do?reportType=4">DOC格式</a></li>
















































































		</ul>
















































































	</li>


























</ul>





















































</body>
















































































</html>

 以下是饼状图

servet:

package lld.test.ireport.zhj;





















































import java.io.IOException;


























import java.io.OutputStream;


























import java.net.URLEncoder;


























import java.util.ArrayList;


























import javax.servlet.ServletException;


























import javax.servlet.http.HttpServlet;


























import javax.servlet.http.HttpServletRequest;


























import javax.servlet.http.HttpServletResponse;


























import net.sf.jasperreports.engine.JRDataSource;


























import net.sf.jasperreports.engine.JRExporterParameter;


























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.export.JRPdfExporter;


























import net.sf.jasperreports.engine.util.JRLoader;





















































public class ZhjChartRepServlet extends HttpServlet


























{


























	private static final long serialVersionUID = -7332836030597016040L;





















































	@Override


























	protected void doGet(HttpServletRequest req, HttpServletResponse resp)


























			throws ServletException, IOException


























	{


























		this.doPost(req, resp);


























	}





















































	@Override


























	protected void doPost(HttpServletRequest req, HttpServletResponse resp)


























			throws ServletException, IOException


























	{


























		try


























		{


























			String root_path = this.getServletContext().getRealPath("/");


























			root_path = root_path.replace('\\', '/');


























			String reportFilePath = root_path


























					+ "WEB-INF/classes/lld/test/ireport/zhj/zhjchart.jasper";


























			MakeData makeData = new MakeData();


























			makeData.addData(new Language("java", 22));



























  
  
分享到:
评论

相关推荐

    jasperreport需要的jar包

    开发者可以使用iReport或Jaspersoft Studio等设计工具来创建JasperDesign文件。 2. **JasperReport**: 报表编译后生成的文件,包含了运行时需要的所有信息,如样式、字段、图像、表格等。它是JasperDesign编译后的...

    JasperReports 3.6.1.jar

    JasperReports 3.6.1 Java报表

    PDF模版设计工具iReport1.3.3+iReport5.6.0+Jaspersoft Studio-6.18.1

    总的来说,"PDF模版设计工具iReport1.3.3+iReport5.6.0+Jaspersoft Studio-6.18.1"的组合为Java开发者提供了一个强大且灵活的报表设计环境,能够满足各种复杂报表的需求。无论是简单的数据展示还是复杂的多页报告,...

    jdk1.4+ireport1.3.3

    将JDK 1.4与iReport 1.3.3结合使用,开发者可以充分利用这两个工具的优势来构建企业级的报表系统。在那个时候,JDK 1.4的稳定性和性能能够支持iReport的运行,而iReport 1.3.3则提供了丰富的报表设计功能,使得非...

    ireport(报表开发工具)

    三、iReport的版本与使用: 你提到的文件“iReport-1.3.3”是iReport的一个较早版本。在早期版本中,用户界面可能相对简单,但基本功能已经相当完善。随着版本的更新,iReport逐渐增加了更多高级特性,如更复杂的...

    JasperReport报表设计总结

    - jasperreports-1.3.3.jar - jdt-compiler-3.1.1.jar - jxl-2.6.jar - png-encoder-1.5.jar - poi-2.0-final-20040126.jar 这些库文件提供了JasperReport运行所需的各种功能支持。 #### 四、总结 本文详细介绍了...

    ireport制作子报表,springmvc

    在`SpringMVC`中,我们可以使用`JasperReports`库来处理`ireport`设计的报表模板。首先,需要在项目中引入`jasperreports`依赖,并配置相应的数据源。`SpringMVC`的控制器负责接收请求,根据参数生成报表,最后将其...

Global site tag (gtag.js) - Google Analytics