jasperReports入门

jasperReports入门1 JasperReport 概述 nbsp nbsp nbsp nbsp JasperReport 是一个基于 Java 的开源报表工具 它可以在 Java 环境下像其它 IDE 报表工具一样来制作报表 JasperReport 支持 PDF HTML XLS CSV 和 XML 等文件输出格式 JasperReport 是当前 Java 开发者最常用的报表工具 nbsp JasperReport 采用 LGPL 开源协议 nbsp 2 Jaspe

1. JasperReports概述 
    JasperReports是一个基于Java的开源报表工具,它可以在Java环境下像其它IDE报表工具 一样来制作报表。JasperReports支持PDF、HTML、XLS、CSV和XML等文件输出格式。JasperReports是当前Java开发者最常用的报表工具。 
JasperReports采用LGPL开源协议。 

2. JasperReports的一些特性 

灵活的报表布局

 
    JasperReports允许你将报表分割成多个部分:报表标题、表头、报表内容、表尾和汇总,这样你就可以分开进行处理,以达到灵活布局的效果。

多种方式显示数据 
JasperReports可以以文本或者图形的方式显示数据,可以通过使用表达式来产生显示动态数据的报表。  

多种方式提供数据 
JasperReports可以以传参数的方式将数据传给报表,这个参数可以是任一Java类的实例;也可以通过数据源的方式传递。

支持多种数据源 
JasperReports可以通过多种数据源产生报表:JDBC、XML文件、POJOs(Plain Old Java Objects)以及任何实现了java.util.Map或者javax.swing.TableModel接口的Java类。

水印功能 
JasperReports可以在产生的报表上生成背景图或者文字。 

子报表功能 
JasperReports可以产生子报表、嵌套报表。 

支持多种文件输出格式 
JasperReports支持多种文件输出格式:PDF、XLS(Excel)、RTF、HTML、XML、CSV等。






















 
 
 
 
 

3. 实战JasperReports 

准备: 
1. 首先在实战前,我们需要下载JasperReports。 
您可以通过 
http://www.jasperforge.org/index.php?option=com_content&task=section&id=16&Itemid=277下载到最新版本的JasperReports 1.3.4(下载前,您首先需要注册、登录)。

2. 解压下载的jasperreports-1.3.4-project.zip,将dist和lib目录下的所有jar文件添加到项目的CLASSPATH中。 

HelloWorld 
在开始我们第一个JasperReports程序之前,让我们先来了解下使用JasperReports创建报表的典型流程: 
创建一个XML格式的报表模板文件(JRXML文件),这个文件的后缀是.jrxml。JRXML文件,可以手工编辑完成,也可以使用可视化的设计工具来完成,例如JasperReports的官方图形化报表设计器——iReport。 
通过调用JasperReports类库中的相应方法(compileReportToFile())或者使用自定义的ant任务,把JRXML文件编译成JasperReports本地二进制模板,这个模板通常被称为Jasper文件。 
通过为Jasper文件提供所需的数据,产生最终的报表。 
导出生成的报表。 

Ok,让我们开始HelloWorld吧! 
建立一个JRXML文件,HelloReportWorld.jrxml: 


 


PUBLIC “-//JasperReports//DTD Report Design//EN” 
“http://jasperreports.sourceforge.net/dtds/jasperreport.dtd”> 


 

 
 
 
 </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><band height="60"> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><textField> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><reportElement x="0" y="10" width="500" height="40"/> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><textElement textAlignment="Center"> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><font size="24"/> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"></textElement> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><textFieldExpression class="java.lang.String"> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><![CDATA[$P{reportTitle}]]> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"></textFieldExpression> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"></textField> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><textField> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><reportElement x="0" y="40" width="500" height="20"/> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><textElement textAlignment="Center"/> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><textFieldExpression class="java.lang.String"> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><![CDATA["Run by: " + $P{author} </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">+ ” on ” + $P{startDate}]]> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"></textFieldExpression> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"></textField> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"></band> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"> 


 



 



 


 



Hello Report World!
 

 
 
 
 

这个文件中,使用了
等模板元素,下面来具体介绍JRXML模板元素: </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><jasperReport>:根元素 </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><import>:用于导入Java类或包 </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><reportFont>:用于定义显示在报表上的文本的字体 </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><parameter>:用于定义报表参数。参数值通过调用JasperReports API中的相关方法,利用 </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">java.util.Map来提供。 </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><queryString>:用于定义从数据库中得到数据的SQL查询语句。 </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><field>:用于将利用数据源或者查询得到的数据映射到报表模板中。 </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><variable>:用于简化报表模板。将报表中多次使用的报表表达式指定给variable。 </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><group>:用于报表分组。 </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><background>:用于定义报表中的页面背景。 </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><title>:用于定义报表标题。只在报表的开头出现一次。 </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><pageHeader>:用于定义表头。在报表每个页面的开头都出现。 </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><columnHeader>:用于定义列头部分。 </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><detail>:用于定义报表的细节部分。 </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><columnFooter>:用于定义列尾部分。 </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><pageFooter>:用于定义表尾。在报表每个页面的底部都出现。 </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><lastPageFooter>:用于定义报表最后一个页面的表尾。 </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"></p> <summary>:用于报表汇总。 </span></p> <p><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> </p> <div style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <band>:用于包含显示在报表中的数据。前面介绍的每个元素都包含一个<band>元素作为它的子元素。 </div> <div style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑">   </div> <div style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑">   </div> <div style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑">   </div> <p><span style="font-family:微软雅黑; font-size:18px; color:#000000">看完了各个模板元素的具体含义,下面让我们看看如何利用这个模板产生报表。 </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">建立一个Java类文件,HelloReportWorld.java: </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">import java.util.HashMap; </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">import java.util.Map; </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">import net.sf.jasperreports.engine.JREmptyDataSource; </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">import net.sf.jasperreports.engine.JRException; </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">import net.sf.jasperreports.engine.JasperCompileManager; </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">import net.sf.jasperreports.engine.JasperExportManager; </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">import net.sf.jasperreports.engine.JasperFillManager; </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">import net.sf.jasperreports.engine.JasperPrint; </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">import net.sf.jasperreports.engine.JasperReport; </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">import net.sf.jasperreports.view.JasperViewer; </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">public class HelloReportWorld { </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">public HelloReportWorld() { </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">} </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">public static void main(String[] args) { </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">String reportSource = “./report/templates/HelloReportWorld.jrxml”; </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">String reportDest = “./report/results/HelloReportWorld.html”; </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">Map<String, Object> params = new HashMap<String, Object>(); </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">params.put(“reportTitle”, “Hello Report World”); </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">params.put(“author”, “Thomas Qi”); </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">params.put(“startDate”, (new java.util.Date()).toString()); </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">try { </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">JasperReport jasperReport = </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">JasperCompileManager.compileReport(reportSource); </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">JasperPrint jasperPrint = </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">JasperFillManager.fillReport( </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">jasperReport, params, new JREmptyDataSource()); </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">JasperExportManager.exportReportToHtmlFile( </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">jasperPrint, reportDest); </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">JasperViewer.viewReport(jasperPrint); </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">} </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">catch (JRException ex) { </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">ex.printStackTrace(); </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">} </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">} </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">} </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">通过以下代码: </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">try { </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">JasperReport jasperReport = </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">JasperCompileManager.compileReport(reportSource); </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">JasperPrint jasperPrint =JasperFillManager.fillReport(jasperReport, params, new JREmptyDataSource()); </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">JasperExportManager.exportReportToHtmlFile(jasperPrint, reportDest); </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">JasperViewer.viewReport(jasperPrint); </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">} </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">可以看出使用JasperReports创建报表典型流程中的2、3、4步骤,分别对应于JasperCompileManager.compileReport()、JasperFillManager.fillReport()、 </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">JasperExportManager.exportReportToHtmlFile()。 </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">我们将报表导出成HTML格式。 </span></p> <p><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> </p> <div style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑">   </div> <div style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑">   </div> <div style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑">   </div> <div style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑">   </div> <div style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> 到目前为止,一个简单的JasperReports应用就完成了 </div> <div style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑">   </div> <div style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑">   </div> <div style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑">   </div> <div style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑">   </div> <p><strong style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑">JDBC数据源报表</strong><span style="font-family:微软雅黑; font-size:18px; color:#000000"> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">重点还是JRXML文件: </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><?xml version="1.0"?> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><!DOCTYPE jasperReport </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">PUBLIC “-//JasperReports//DTD Report Design//EN” </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">“http://jasperreports.sourceforge.net/dtds/jasperreport.dtd”> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><jasperReport name="DbReport"> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><parameter name="reportTitle" class="java.lang.String"/> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><parameter name="author" class="java.lang.String"/> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><parameter name="startDate" class="java.lang.String"/> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><queryString> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><![CDATA[SELECT * FROM user ORDER BY id]]> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"></queryString> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><field name="id" class="java.lang.Integer"/> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><field name="email" class="java.lang.String"/> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><field name="password" class="java.lang.String"/> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><title> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><band height="60"> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><textField> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><reportElement x="0" y="10" width="500" height="40"/> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><textElement textAlignment="Center"> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><font size="24"/> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"></textElement> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><textFieldExpression class="java.lang.String"> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><![CDATA[$P{reportTitle}]]> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"></textFieldExpression> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"></textField> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><textField> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><reportElement x="0" y="40" width="500" height="20"/> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><textElement textAlignment="Center"/> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><textFieldExpression class="java.lang.String"> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"><![CDATA["Create by: " + $P{author} </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000">+ ” on ” + $P{startDate}]]> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"></textFieldExpression> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"></textField> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"></band> </span><br style="font-size:18px; color:rgb(0,0,0); font-family:微软雅黑"> <span style="font-family:微软雅黑; font-size:18px; color:#000000"> 



 



 



 


 


 

 


 


 


 



ID
 

 


 


 



Email
 

 


 


 



Password
 

 
 
 



 



 



 


 


 



 

$F{id} 
 
 


 


 


 



 

$F{email} 
 
 


 


 


 



 

$F{password} 
 
 
 
 
 

这个示例与HelloWorld的主要区别,在于黑体部分。 


 

SELECT * FROM user ORDER BY id 
 

这里定义了从user表中查询数据的SQL语句,user表中包含id, email和password三个字段; 

 


用于定义user表中的字段; 


 

$F{id} 




































































































这是将查询出的对应字段的数据填充到报表中。
 
 

使用这个模板的客户端核心代码: 

try { 
JasperReport jasperReport = JasperCompileManager.compileReport(reportSource); 
// Make the driver class available. 
Class.forName(“com.mysql.jdbc.Driver”); 
// Create a connection to pass into the report. 
java.sql.Connection conn = 
DriverManager.getConnection(“jdbc:mysql://localhost:3306/test”,”root”,””); 
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, conn); 
JRCsvExporter csvExporter = new JRCsvExporter(); 
csvExporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint); 
csvExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,reportDest); 
csvExporter.exportReport(); 
JasperViewer.viewReport(jasperPrint); 
} catch (JRException e) { 
e.printStackTrace(); 

catch (ClassNotFoundException ex) { 
ex.printStackTrace(); 

catch (SQLException ex) { 
ex.printStackTrace(); 



JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, conn); 
这句将HelloWorld中空的JasperReports数据源替换成了一个叫做conn的数据库连接参数,这也是程序的重点所在! 
这次,我们将报表导出成CSV格式。 





























 
 
 
Ok,通过这个JDBC数据源报表的应用,您该掌握了如何以JDBC的方式获取数据并创建报表。 
可是您会发现: 



 

SELECT * FROM user ORDER BY id 



 

在OO大行其道的今天,这种直接写SQL语句的做法,就显得太过程化了。POJOs?对,就是它!JasperReports也提供了对POJOs数据源支持。  




 
 
POJOs数据源报表 

建立一个简单的Java类文件(POJOs),User.java: 
public class User implements java.io.Serializable { 
private Integer id; 
private String email; 
private String password; 

public User() { 

public User(String email, String password) { 
this.email = email; 
this.password = password; 

public User(Integer id, String email, String password) { 
this.id = id; 
this.email = email; 
this.password = password; 

public Integer getId() { 
return this.id; 

public void setId(Integer id) { 
this.id = id; 

public String getEmail() { 
return this.email; 

public void setEmail(String email) { 
this.email = email; 

public String getPassword() { 
return this.password; 

public void setPassword(String password) { 
this.password = password; 

JRXML文件 

基本和JDBC数据源的一致,只是这里不再需要: 


 

SELECT * FROM user ORDER BY id 



 

重点看客户端核心代码: 
try { 
JasperReport jasperReport = JasperCompileManager.compileReport(reportSource); 
ArrayList reportRows = new ArrayList(); 
reportRows.add(new User(1, “”, “”)); 
reportRows.add(new User(1, “”, “000000”)); 
reportRows.add(new User(3, “”, “abc123”)); 
reportRows.add(new User(3, “”, “”)); 
JRDataSource dataSource = new JRBeanCollectionDataSource(reportRows); 
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, 
params, dataSource); 
JasperExportManager.exportReportToPdfFile(jasperPrint, reportDest); 
JasperViewer.viewReport(jasperPrint); 
} catch (JRException e) { 
e.printStackTrace(); 

JRDataSource dataSource = new JRBeanCollectionDataSource(reportRows); 
这次,我们把使用POJOs的ArrayList作为了数据源。 
我们将报表导出成pdf格式。 






































































版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/221773.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月17日 下午5:20
下一篇 2026年3月17日 下午5:20


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号