官术网_书友最值得收藏!

Creating a binary report template

The JRXML files cannot be used directly to generate reports. They need to be compiled into JasperReports' native binary format. Compiled report templates are called jasper files. There are two ways to compile a JRXML file into a jasper file: We can do it either programmatically or through a custom ANT task provided by JasperReports.

Compiling a JRXML template programmatically

A JRXML template can be compiled into a jasper file and saved to disk by calling the compileReportToFile() method on the net.sf.jasperreports.engine.JasperCompileManager class. The three overloaded versions of the JasperCompileManager.compileReportToFile() method are listed below:

  • JasperCompileManager.compileReportToFile(String sourceFileName)
  • JasperCompileManager.compileReportToFile(String sourceFileName, String destFileName)
  • JasperCompileManager.compileReportToFile(JasperDesign jasperDesign, String destFileName)

The following table illustrates the parameters used in these methods:

The following code fragment demonstrates the use of the JasperCompileManager.compileReportToFile() method:

package net.ensode.jasperbook;

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

public class FirstReportCompile
{
  public static void main(String[] args)
  {
    try
    {
      System.out.println("Compiling report...");
 JasperCompileManager.compileReportToFile("reports/FirstReport.jrxml");
      System.out.println("Done!");
    }
    catch (JRException e)
    {
      e.printStackTrace();
    }
  }
}

After compiling and executing this code, we should see a file called FirstReport.jasper in the filesystem. This file is the compiled template in JasperReports' native format. For this example, we chose to use the first version of JasperCompileManager.compileReportToFile() method we discussed. As by default the root filename is used for the compiled report, and we did not have an in-memory representation of the JRXML template. If we had wished to use a different root filename for the compiled report template, we should have used the second version of JasperCompileManager.compileReportToFile() and specified the desired filename as the second parameter.

Previewing the compiled report template

The net.sf.jasperreports.view.JasperDesignViewer discussed previously can be used to preview compiled report templates as well as JRXML templates. Again, the easiest way to execute this utility is to wrap a call to it into an ANT target. Let's add a second ANT target to our build.xml file. We will call this new target viewDesign. It will let us preview the compiled report.

<project name="FirstReport XML Design Preview" default="viewDesignXML" basedir=".">
  <description>Previews and compiles our First Report</description>
  <property name="file.name" value="FirstReport" />
<!-- Directory where the JasperReports project file was extracted, 
     needs to be changed to match the local environment -->
  <property name="jasper.dir"value="/opt/jasperreports-3.5.2" />
  <property name="classes.dir" value="${jasper.dir}/build/classes" />
  <property name="lib.dir" value="${jasper.dir}/lib" />
  <path id="classpath">
    <pathelement location="./" />
    <pathelement location="${classes.dir}" />
    <fileset dir="${lib.dir}">
      <include name="**/*.jar" />
    </fileset>
  </path>

  <target name="viewDesignXML"description="Launches the design viewer to preview the XML 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>

 <target name="viewDesign"description="Launches the design viewer to preview thecompiled report design.">
 <java classname="net.sf.jasperreports.view.JasperDesignViewer"fork="true">
 <arg value="-F${file.name}.jasper" />
 <classpath refid="classpath" />
 </java>
 </target>  <target name="compile" 
          description="Compiles the XML report design 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>

  <target name="view" 
          description="Launches the report viewer to preview thereport stored in the .jrprint file.">
    <java classname="net.sf.jasperreports.view.JasperViewer"fork="true">
      <arg value="-F{file.name}.jrprint" />
      <classpath refid="classpath" />
    </java>
  </target>
</project>

We can invoke the new target from the command line as follows:

ant viewDesign

After invoking the target, we should see a window very similar to the one we saw when previewing the JRXML.

Compiling a JRXML template through ANT

JasperReports includes a custom ANT task that can be used to compile report templates. Compiling reports in this manner is very convenient because we don't have to write code to perform the compilation. For certain applications, however, we need to compile a report programmatically (in situations where the JRXML file is created at runtime, for example). The custom ANT task included by JasperReports is called jrc. It is defined in the net.sf.jasperreports.ant.JRAntCompileTask class. Let's add a third target to our build.xml file to invoke the jrc task.

<project name="FirstReport XML Design Preview" default="viewDesignXML"basedir=".">

  <description>
    Previews and compiles our First Report
  </description>

  <property name="file.name" value="FirstReport" /><!-- Directory where the JasperReports project file was extracted,needs to be changed to match the local environment -->
  <property name="jasper.dir"value="/opt/jasperreports-3.5.2" />
  <property name="classes.dir" value="${jasper.dir}/build/classes" />
  <property name="lib.dir" value="${jasper.dir}/lib" />

  <path id="classpath">
    <pathelement location="./" />
    <pathelement location="${classes.dir}" />
    <fileset dir="${lib.dir}">
      <include name="**/*.jar" />
    </fileset>
  </path>

  <target name="viewDesignXML"description="Launches the design viewer to preview the XMLreport design.">
    <java classname="net.sf.jasperreports.view.JasperDesignViewer"fork="true">
      <arg value="-XML" />
      <arg value="-F${file.name}.jrxml" />
      <classpath refid="classpath" />
    </java>
  </target>

  <target name="viewDesign" 
          description="Launches the design viewer to preview thecompiled report design.">
    <java classname="net.sf.jasperreports.view.JasperDesignViewer"fork="true">
      <arg value="-F${file.name}.jasper" />
      <classpath refid="classpath" />
    </java>
  </target>

 <target name="compile" description="Compiles the XML report design and producesthe .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>

The new target can be invoked from the command line:

ant compile

The compile target produces the following output:

Buildfile: build.xml

compile:

 [jrc] Compiling 1 report design files.

 [jrc] File : /home/heffel/NetBeansProjects/Code_8082/jasper_book_chapter_3/reports/FirstReport.jrxml ... OK.

BUILD SUCCESSFUL

Total time: 3 seconds

After successful execution of the compile target, we should have a FirstReport.jasper file in the filesystem. This file is identical to the one generated programmatically by calling the net.sf.jasperreports.engine.JasperCompileManager.compileReportToFile() method.

We can preview the generated jasper file by using the JasperDesign utility included by JasperReports, as explained in the previous section. The output will be identical.

主站蜘蛛池模板: 漳平市| 清流县| 汉沽区| 宜春市| 新昌县| 福安市| 张掖市| 巫山县| 红安县| 永清县| 白朗县| 砚山县| 武冈市| 古交市| 德格县| 吴堡县| 东阿县| 柞水县| 通榆县| 临朐县| 民乐县| 精河县| 桐庐县| 陆丰市| 隆德县| 宜兴市| 资溪县| 彭山县| 临夏市| 南通市| 安康市| 华坪县| 瑞丽市| 巴林右旗| 社旗县| 沭阳县| 庆元县| 溧阳市| 通道| 临西县| 米林县|