Jasper Reports 简明教程

JasperReports - Compiling Report Design

我们在前一章中生成了 JasperReport 模板(JRXML 文件)。该文件无法直接用于生成报表。必须将它编译成 JasperReport 的原生二进制格式,称为 Jasper 文件。在编译时,我们将 JasperDesign 对象转换为 JasperReport 对象 −

We have generated the JasperReport template (JRXML file) in the previous chapter. This file cannot be used directly to generate reports. It has to be compiled to JasperReport' native binary format, called Jasper file. On compiling, we transform JasperDesign object into JasperReport object −

jasper report compiling

接口 net.sf.jasperreports.engine.design.JRCompiler 在编译期间起到了核心作用。此接口具有多个实现,具体取决于用于报表表达式的语言,后者可以用 Java、Groovy、JavaScript 或任何其他脚本语言编写,只要编译器实现能够在运行时评估它们即可。

Interface net.sf.jasperreports.engine.design.JRCompiler plays a central role during compilation. This interface has several implementations depending on the language used for report expressions, which can be written in Java, Groovy, JavaScript, or any other scripting language as long as compiler implementation can evaluate it at runtime.

我们可以通过以下两种方式编译 JRXML 文件 −

We can compile JRXML file in the following two ways −

  1. Programmatic compilation.

  2. Compilation through ANT task.

Programmatic Compilation of JRXML

JasperReports API 提供了一个外观类 net.sf.jasperreports.engine.JasperCompileManager 用于编译 JasperReport。此类包含用于编译报表模板的多个公共静态方法。模板的源可以是文件、输入流和/或内存对象。

JasperReports API offers a facade class net.sf.jasperreports.engine.JasperCompileManager for compiling a JasperReport. This class consists of several public static methods for compiling report templates. The source of templates can be files, input streams and/or, memory objects.

JRXML 文件(jasper_report_template.jrxml)的内容如下。它被保存在目录 C:\tools\jasperreports-5.0.1\test 中 −

The contents of the JRXML file (jasper_report_template.jrxml) are as follows. It is saved at directory C:\tools\jasperreports-5.0.1\test

<?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>

以下代码展示了上述 jasper_report_template.jrxml 文件的编译。

The following code demonstrates compilation of the above jasper_report_template.jrxml file.

package com.tutorialspoint;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;

public class JasperReportCompile {

   public static void main(String[] args) {
      String sourceFileName = "C://tools/jasperreports-5.0.1/test" +
         "/jasper_report_template.jrxml";

      System.out.println("Compiling Report Design ...");
      try {
          /**
          * Compile the report to a file name same as
          * the JRXML file name
          */
         JasperCompileManager.compileReportToFile(sourceFileName);
      } catch (JRException e) {
         e.printStackTrace();
      }
      System.out.println("Done compiling!!! ...");
   }
}

Template Compilation

作为下一步,我们将在 C:\tools\jasperreports-5.0.1\test\src\com\tutorialspoint\JasperReportCompile.java 文件中保存上述内容,并在 build.xml 文件中导入 baseBuild.xml,如下所示。baseBuild.xml 已具有 compilerun 目标 −

As next step, let’s save above content in the file C:\tools\jasperreports-5.0.1\test\src\com\tutorialspoint\JasperReportCompile.java and import the baseBuild.xml in the build.xml file as below. The baseBuild.xml already has the compile and run targets −

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

   <import file = "baseBuild.xml"/>

</project>

接下来,我们打开命令行窗口,然后转到放置 build.xml 的目录中。最后,执行命令 ant -Dmain-class = com.tutorialspoint.JasperReportCompile ,如下所示 −

Next, let’s open command line window and go to the directory where build.xml is placed. Finally, execute the command ant -Dmain-class = com.tutorialspoint.JasperReportCompile as −

C:\tools\jasperreports-5.0.1\test>ant -Dmain-class = com.tutorialspoint.JasperReportCompile
Buildfile: C:\tools\jasperreports-5.0.1\test\build.xml
compile:
   [javac] C:\tools\jasperreports-5.0.1\test\baseBuild.xml:27:
   warning: 'includeantruntime' was not set, defaulting to
   build.sysclasspath=last;set to false for repeatable builds
   [javac] Compiling 1 source file to C:\tools\jasperreports-5.0.1\test\classes

run:
   [echo] Runnin class : com.tutorialspoint.JasperReportCompile
   [java] Compiling Report Design ...
   [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.
   [java] Done compiling!!! ...

BUILD SUCCESSFUL
Total time: 8 seconds

由于上述编译,您会看到模板文件 jasper_report_template.jasper 已在 C:\tools\jasperreports-5.0.1\test 目录中生成。

As a result of above compilation, you will see that template file jasper_report_template.jasper got generated in C:\tools\jasperreports-5.0.1\test directory.

Preview Compiled Report Template

net.sf.jasperreports.view.JasperDesignViewer 可用于预览已编译的报表模板和 JRXML 模板。

The net.sf.jasperreports.view.JasperDesignViewer can be used to preview compiled report templates and JRXML templates.

要进一步操作,我们向上面的 build.xml 文件添加一个新目标 viewDesign ,这将允许我们预览已编译的报表。以下是经过修改的 build.xml −

To move further, let’s add a new target viewDesign to the above build.xml file, which will allow us to preview the compiled report. Below is the revised build.xml −

导入文件 - baseBuild.xml 从第 Environment Setup 章节中选取,且应放置在与 build.xml 相同的目录中。

The import file - baseBuild.xml is picked from chapter Environment Setup and should be placed in the same directory as the build.xml.

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

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

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

</project>

让我们在命令提示符下执行命令 ant (viewDesign 是默认目标)。JasperDesignViewer 窗口打开,显示如下所示的 Jasper 文件 −

Let’s execute the command − ant (viewDesign is the default target) at command prompt. JasperDesignViewer window opens up displaying the Jasper file as below −

jasper design viewer

Compilation through ANT Task

由于报表模板编译更像是设计时间作业而不是运行时作业,因此 JasperReport 库有一个自定义 ANT 任务。在某些情况下,当 JRXML 文件在运行时创建时,我们就无法使用此 ANT 任务。自定义 ANT 任务称为 JRC,并通过类 net.sf.jasperreports.ant.JRAntCompileTask 实现。它的语法和行为与内置 <javac> ANT 任务非常相似。

As report template compilation is more like a design time job than a runtime job, JasperReport library has a custom ANT task. For certain situations, when JRXML file is created at runtime, we can’t use this ANT task. The custom ANT task is called JRC and is implemented by the class: net.sf.jasperreports.ant.JRAntCompileTask. Its syntax and behavior are very similar to the built-in <javac> ANT task.

Template Compilation

让我们向现有 build.xml 添加新目标 compilereportdesing 。此处,使用嵌套 <src> 标记通过文件集指定源文件夹。嵌套源标记允许编译散布在许多不同位置的且未分组在单个根报告源文件夹下的报表模板。以下是经过修改的 build.xml −

Let’s add new target compilereportdesing to our existing build.xml. Here, the source folder is specified using a nested <src> tag with the filesets. The nested source tag allows compiling report templates that are scattered through many different locations and are not grouped under a single root report source folder. Below is the revised build.xml −

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

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

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

   </target>

   <target name = "compilereportdesing" description = "Compiles the
      JXML file and produces the .jasper file.">

      <taskdef name = "jrc" classname = "net.sf.jasperreports.ant.JRAntCompileTask">
         <classpath refid = "classpath" />
      </taskdef>

      <jrc destdir = ".">
         <src>
            <fileset dir = ".">
               <include name = "*.jrxml" />
            </fileset>
         </src>
         <classpath refid = "classpath" />
      </jrc>
   </target>

</project>

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

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

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

compilereportdesing:
   [jrc] Compiling 1 report design files.
   [jrc] log4j:WARN No appenders could be found for logger
   (net.sf.jasperreports.engine.xml.JRXmlDigesterFactory).
   [jrc] log4j:WARN Please initialize the log4j system properly.
   [jrc] log4j:WARN See
   http://logging.apache.org/log4j/1.2/faq.html#noconfig
   for more info.
   [jrc] File :
   C:\tools\jasperreports-5.0.1\test\jasper_report_template.jrxml ... OK.

BUILD SUCCESSFUL
Total time: 5 seconds

File jasper_report_template.jasper 在文件系统中生成(在我们的案例中是 C:\tools\jasperreports-5.0.1\test 目录)。此文件与通过调用 net.sf.jasperreports.engine.JasperCompileManager.compileReportToFile() 以编程方式生成的文件相同。我们可以执行 ant viewDesign 以预览此 Jasper 文件。

File jasper_report_template.jasper is generated in the file system (in our case C:\tools\jasperreports-5.0.1\test directory). This file is identical to the file generated programmatically by calling the net.sf.jasperreports.engine.JasperCompileManager.compileReportToFile(). We can preview this jasper file, executing ant viewDesign.