Jasper Reports 简明教程

JasperReports - Designs

JasperReport 中的 JRXML 模板(或 JRXML 文件)是标准 XML 文件,具有 .jrxml 扩展名。所有 JRXML 文件都包含标签 <jasperReport> 作为根元素。这反过来又包含许多子元素(所有这些都是可选的)。JasperReport 框架可以处理不同类型的数据源。在本教程中,我们将演示如何通过将 Java 数据对象集合(使用 Java bean)传递到 JasperReport Engine 来生成基本报表。最终报表将显示包含姓名和所在国家/地区的人员列表。

The JRXML templates (or JRXML files) in JasperReport are standard XML files, having an extension of .jrxml. All the JRXML files contain tag <jasperReport>, as root element. This in turn contains many sub-elements (all of these are optional). JasperReport framework can handle different kinds of data sources. In this tutorial, we shall show how to generate a basic report, just by passing a collection of Java data object (using Java beans), to the JasperReport Engine. The final report shall display a list of people with the categories including their names and countries.

本章中介绍了以下步骤来描述如何设计 JasperReport:

The Following steps are covered in this chapter to describe — how to design a JasperReport −

  1. Creating a JRXML Report Template and.

  2. Previewing the XML Report Template.

Creating a JRXML Report Template

创建 JRXML 文件,该文件 jasper_report_template.jrxml 使用文本编辑器,并根据我们的环境设置将其保存为 C:\tools\jasperreports-5.0.1\test。

Create the JRXML file, which is jasper_report_template.jrxml using a text editor and save this file in C:\tools\jasperreports-5.0.1\test as per our environment setup.

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

<jasperReport xmlns = "http://jasperreports.sourceforge.net/jasperreports"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://jasperreports.sourceforge.net/jasperreports
   http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"
   name = "jasper_report_template" language = "groovy" pageWidth = "595"
   pageHeight = "842" columnWidth = "555" leftMargin = "20" rightMargin = "20"
   topMargin = "20" bottomMargin = "20">

   <queryString>
      <![CDATA[]]>
   </queryString>

   <field name = "country" class = "java.lang.String">
      <fieldDescription><![CDATA[country]]></fieldDescription>
   </field>

   <field name = "name" class = "java.lang.String">
      <fieldDescription><![CDATA[name]]></fieldDescription>
   </field>

   <columnHeader>
      <band height = "23">

         <staticText>
            <reportElement mode = "Opaque" x = "0" y = "3" width = "535"
               height = "15" backcolor = "#70A9A9" />

            <box>
               <bottomPen lineWidth = "1.0" lineColor = "#CCCCCC" />
            </box>

            <textElement />
            <text><![CDATA[]]> </text>
         </staticText>

         <staticText>
            <reportElement x = "414" y = "3" width = "121" height = "15" />

            <textElement textAlignment = "Center" verticalAlignment = "Middle">
               <font isBold = "true" />
            </textElement>

            <text><![CDATA[Country]]></text>
         </staticText>

         <staticText>
            <reportElement x = "0" y = "3" width = "136" height = "15" />

            <textElement textAlignment = "Center" verticalAlignment = "Middle">
               <font isBold = "true" />
            </textElement>

            <text><![CDATA[Name]]></text>
         </staticText>

      </band>
   </columnHeader>

    <detail>
      <band height = "16">

         <staticText>
            <reportElement mode = "Opaque" x = "0" y = "0" width = "535"
               height = "14" backcolor = "#E5ECF9" />

            <box>
               <bottomPen lineWidth = "0.25" lineColor = "#CCCCCC" />
            </box>

            <textElement />
            <text><![CDATA[]]> </text>
         </staticText>

         <textField>
            <reportElement x = "414" y = "0" width = "121" height = "15" />

            <textElement textAlignment = "Center" verticalAlignment = "Middle">
               <font size = "9" />
            </textElement>

            <textFieldExpression class = "java.lang.String">
               <![CDATA[$F{country}]]>
            </textFieldExpression>
         </textField>

         <textField>
            <reportElement x = "0" y = "0" width = "136" height = "15" />
            <textElement textAlignment = "Center" verticalAlignment = "Middle" />

            <textFieldExpression class = "java.lang.String">
               <![CDATA[$F{name}]]>
            </textFieldExpression>
         </textField>

      </band>
   </detail>

</jasperReport>

以下是上述报表模板中主要字段的详细信息:

Here are the details of main fields in the above report template −

  1. <queryString> − This is empty (as we are passing data through Java Beans). Usually contains the SQL statement, which retrieves the report result.

  2. <field name> − This element is used to map data from data sources or queries, into report templates. name is re-used in the report body and is case-sensitive.

  3. <fieldDescription> − This element maps the field name with the appropriate element in the XML file.

  4. <staticText> − This defines the static text that does not depend on any datasources, variables, parameters, or report expressions.

  5. <textFieldExpression> − This defines the appearance of the result field.

  6. $F{country} − This is a variable that contains the value of result, predefined field in the tag <field name>.

  7. <band> − Bands contain the data, which is displayed in the report.

一旦报告设计准备就绪,将其保存到 C:\ 目录。

Once the report design is ready, save it in C:\ directory.

Previewing the XML Report Template

JasperReports JAR 文件中提供了一个实用工具类 net.sf.jasperreports.view.JasperDesignViewer,它有助于在无需编译或填充的情况下预览报告设计。此实用工具是一个独立的 Java 应用程序,因此可以使用 ANT 执行。

There is a utility net.sf.jasperreports.view.JasperDesignViewer available in JasperReports JAR file, which helps in previewing the report design without having to compile or fill it. This utility is a standalone Java application, hence can be executed using ANT.

让我们编写 ANT 目标 viewDesignXML 来查看 JRXML。因此,让我们在 C:\tools\jasperreports-5.0.1\test 目录下创建并保存 build.xml (应放置在放置 JRXML 的同一目录中)。以下是 build.xml 文件−

Let’s write an ANT target viewDesignXML to view the JRXML. So, let’s create and save build.xml under C:\tools\jasperreports-5.0.1\test directory (should be placed in the same directory where JRXML is placed). Here is the build.xml file −

<?xml version = "1.0" encoding = "UTF-8"?>
<project name = "JasperReportTest" default = "viewDesignXML" basedir = ".">

   <import file = "baseBuild.xml" />
   <target name = "viewDesignXML" description = "Design viewer is
      launched to preview the JXML report design.">

      <java classname = "net.sf.jasperreports.view.JasperDesignViewer" fork = "true">
         <arg value = "-XML" />
         <arg value = "-F${file.name}.jrxml" />
         <classpath refid = "classpath" />
      </java>
   </target>

</project>

接下来,让我们打开命令提示符并转到放置 build.xml 的目录。执行命令 ant (因为 viewDesignXML 是默认目标)。输出如下所示−

Next, let’s open a command prompt and go to the directory where build.xml is placed. Execute the command ant (As the viewDesignXML is the default target). Output is follows −

C:\tools\jasperreports-5.0.1\test>ant
Buildfile: C:\tools\jasperreports-5.0.1\test\build.xml

viewDesignXML:
[java] log4j:WARN No appenders could be found for logger
(net.sf.jasperreports.engine.xml.JRXmlDigesterFactory).
[java] log4j:WARN Please initialize the log4j system properly.

可以忽略 Log4j 警告,并且作为上述执行的结果,“JasperDesignViewer” 窗口打开,显示我们的报告模板预览。

Log4j warning can be ignored, and as a result of above execution, a window labeled "JasperDesignViewer" opens, displaying our report template preview.

jasper design viewer

正如我们所看到的,仅显示用于获取数据的报告表达式,因为 JasperDesignViewer 无法访问实际数据源或报告参数。通过关闭窗口或在命令行窗口中按 Ctrl-c 终止 JasperDesignViewer。

As we see, only report expressions for obtaining the data are displayed, as JasperDesignViewer doesn’t have access to the actual data source or report parameters. Terminate the JasperDesignViewer by closing the window or by hitting Ctrl-c in the command-line window.