JasperReports学习笔记

reference:
http://www.javaworld.com/javaworld/jw-09-2002/jw-0920-opensourceprofile.html
JasperReports是一个非常流行的开源报表工具
所有的报表工具都离不了三个步骤:第一个就是定义报表模板, 第二个就是给模板填充数据, 第三个就是将最终数据的报表输出。
JasperReports的模板文件是一个后缀名为jrxml的xml文件, 它包含这样几个主要元素:
<jasperReport> 根元素
<title>标题
<pageHeader>页头
<detail>具体内容
<pageFooter>页尾
<band>作为以上元素的子元素, 用来定义报表区域
除了jasperReport之外, 其他元素都不是必须的
下面是一个jrxml文件的定义,用来输出helloworld
Xml代码
<?xml version="1.0"?>
<!DOCTYPE jasperReport
PUBLIC "-//JasperReports//DTD Report Design//EN"
"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">

<jasperReport name="Simple_Report">
<detail>
<band height="20">
<staticText>
<reportElement x="180" y="0" width="200" height="20"/>
<text><![CDATA[Hello World!]]></text>
</staticText>
</band>
</detail>
</jasperReport>

<?xml version="1.0"?>
<!DOCTYPE jasperReport
PUBLIC "-//JasperReports//DTD Report Design//EN"
"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">

<jasperReport name="Simple_Report">
<detail>
<band height="20">
<staticText>
<reportElement x="180" y="0" width="200" height="20"/>
<text><![CDATA[Hello World!]]></text>
</staticText>
</band>
</detail>
</jasperReport>

接下来就是将报表模板文件编译成二进制文件,然后就是给报表模板填充数据,最后是输出(比如输出为PDF文件),下面的代码完成这个工作:
Java代码
public class JasperReportsIntro
{
public static void main(String[] args)
{
JasperReport jasperReport;
JasperPrint jasperPrint;
try
{
jasperReport = JasperCompileManager.compileReport("reports/jasperreports_demo.jrxml");
jasperPrint = JasperFillManager.fillReport( jasperReport, new HashMap(), new JREmptyDataSource()); JasperExportManager.exportReportToPdfFile( jasperPrint, "reports/simple_report.pdf");
}
catch (JRException e)
{
e.printStackTrace();
}
}
}

public class JasperReportsIntro
{
public static void main(String[] args)
{
JasperReport jasperReport;
JasperPrint jasperPrint;
try
{
jasperReport = JasperCompileManager.compileReport("reports/jasperreports_demo.jrxml");
jasperPrint = JasperFillManager.fillReport( jasperReport, new HashMap(), new JREmptyDataSource()); JasperExportManager.exportReportToPdfFile( jasperPrint, "reports/simple_report.pdf");
}
catch (JRException e)
{
e.printStackTrace();
}
}
}


上面的代码是JasperReport的早期版本的写法,不过原理还是一样的。

我们再看一个比较复杂的例子, 它的title是在程序中通过参数传递进行来的, 然后通过JDBC访问数据库来取得要填充的数据:
Xml代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jasperReport PUBLIC "-//JasperReports//DTD Report Design//EN"
"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport name="BasicReport" >
<parameter name="Title" class="java.lang.String"/>
<queryString><![CDATA[select name, cost from product]]></queryString>
<field name="NAME" class="java.lang.String"/>
<field name="COST" class="java.lang.Double"/>
<title>
<band height="50">
<textField>
<reportElement x="0" y="0" width="200" height="50" />
<textFieldExpression class="java.lang.String">$P{Title}</textFieldExpression>
</textField>
</band>
</title>
<pageHeader>
<band>
</band>
</pageHeader>
<columnHeader>
<band height="20">
<staticText>
<reportElement x="180" y="0" width="180" height="20"/>
<textElement>
<font isUnderline="true"/>
</textElement>
<text><![CDATA[NAME]]></text>
</staticText>
<staticText>
<reportElement x="360" y="0" width="180" height="20"/>
<textElement>
<font isUnderline="true"/>
</textElement>
<text><![CDATA[COST]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20">
<textField>
<reportElement x="180" y="0" width="180" height="20"/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{NAME}]]></textFieldExpression>
</textField>
<textField pattern="0.00">
<reportElement x="360" y="0" width="180" height="20"/>
<textFieldExpression class="java.lang.Double"><![CDATA[$F{COST}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band>
</band>
</columnFooter>
<pageFooter>
<band height="15">
<staticText>
<reportElement x="0" y="0" width="40" height="15"/>
<textElement/>
<text><![CDATA[Page:]]></text>
</staticText>
<textField>
<reportElement x="40" y="0" width="100" height="15"/>
<textElement/>
<textFieldExpression class="java.lang.Integer"><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression>
</textField>
</band>
</pageFooter>
<summary>
<band>
</band>
</summary>
</jasperReport>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jasperReport PUBLIC "-//JasperReports//DTD Report Design//EN"
"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport name="BasicReport" >
<parameter name="Title" class="java.lang.String"/>
<queryString><![CDATA[select name, cost from product]]></queryString>
<field name="NAME" class="java.lang.String"/>
<field name="COST" class="java.lang.Double"/>
<title>
<band height="50">
<textField>
<reportElement x="0" y="0" width="200" height="50" />
<textFieldExpression class="java.lang.String">$P{Title}</textFieldExpression>
</textField>
</band>
</title>
<pageHeader>
<band>
</band>
</pageHeader>
<columnHeader>
<band height="20">
<staticText>
<reportElement x="180" y="0" width="180" height="20"/>
<textElement>
<font isUnderline="true"/>
</textElement>
<text><![CDATA[NAME]]></text>
</staticText>
<staticText>
<reportElement x="360" y="0" width="180" height="20"/>
<textElement>
<font isUnderline="true"/>
</textElement>
<text><![CDATA[COST]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20">
<textField>
<reportElement x="180" y="0" width="180" height="20"/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{NAME}]]></textFieldExpression>
</textField>
<textField pattern="0.00">
<reportElement x="360" y="0" width="180" height="20"/>
<textFieldExpression class="java.lang.Double"><![CDATA[$F{COST}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band>
</band>
</columnFooter>
<pageFooter>
<band height="15">
<staticText>
<reportElement x="0" y="0" width="40" height="15"/>
<textElement/>
<text><![CDATA[Page:]]></text>
</staticText>
<textField>
<reportElement x="40" y="0" width="100" height="15"/>
<textElement/>
<textFieldExpression class="java.lang.Integer"><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression>
</textField>
</band>
</pageFooter>
<summary>
<band>
</band>
</summary>
</jasperReport>

从上面的模板文件中可以看出, 报表中的parameter, field, variable将通过P${name}, F${name}, and V${name}来进行引用。
下面是对应的java代码:
Java代码
// First, load JasperDesign from XML and compile it into JasperReport
JasperDesign jasperDesign = JasperManager.loadXmlDesign("BasicReport.xml");
JasperReport jasperReport = JasperManager.compileReport(jasperDesign);
// Second, create a map of parameters to pass to the report.
Map parameters = new HashMap();
parameters.put("ReportTitle", "Basic JasperReport");
parameters.put("MaxSalary", new Double(25000.00));
// Third, get a database connection
Connection conn = Database.getConnection();
// Fourth, create JasperPrint using fillReport() method
JasperPrint jasperPrint = JasperManager.fillReport(jasperReport,
parameters, conn);
// You can use JasperPrint to create PDF
JasperManager.printReportToPdfFile(jasperPrint, "BasicReport.pdf");
// Or to view report in the JasperViewer
JasperViewer.viewReport(jasperPrint);

// First, load JasperDesign from XML and compile it into JasperReport
JasperDesign jasperDesign = JasperManager.loadXmlDesign("BasicReport.xml");
JasperReport jasperReport = JasperManager.compileReport(jasperDesign);
// Second, create a map of parameters to pass to the report.
Map parameters = new HashMap();
parameters.put("ReportTitle", "Basic JasperReport");
parameters.put("MaxSalary", new Double(25000.00));
// Third, get a database connection
Connection conn = Database.getConnection();
// Fourth, create JasperPrint using fillReport() method
JasperPrint jasperPrint = JasperManager.fillReport(jasperReport,
parameters, conn);
// You can use JasperPrint to create PDF
JasperManager.printReportToPdfFile(jasperPrint, "BasicReport.pdf");
// Or to view report in the JasperViewer
JasperViewer.viewReport(jasperPrint);

这里我们有几个类需要了解一下:
JasperDesign:可以看成报表模板对应的对象模型, 我们定义的jrxml文件最终需要转换成该模型来使用, 对于模板对象来说, 它是静态的对象, 因此只需在初始化的时候创建一次,而不必在每次生成报表的时候生成。
JasperReport:经过编译的JasperDesign就是JasperReport
JasperPrint:给JasperReport填充数据和传递参数之后得到的就是JasperPrint对象, 他就是我们最终要生成的报表对象。
JasperManager:可以看成以上的对象的工厂类,它提供有相关的方法来创建以上的类实例
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值