- JasperReports 3.5 for Java Developers
- David R. Heffelfinger
- 560字
- 2021-04-01 13:58:23
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.
- Learning Ext JS 3.2
- 專業(yè)級(jí)音樂(lè)制作理論與實(shí)踐Pro Tools:從入門(mén)到應(yīng)用
- AutoCAD 2014實(shí)用教程(第4版)
- 中文版Rhino 5.0完全自學(xué)教程(第3版)
- 正則表達(dá)式必知必會(huì)(修訂版)
- Building Websites with PHP/Nuke
- CakePHP Application Development
- 行攝 Photoshop CC后期修片高手之道(第2版)
- Unity 3D游戲開(kāi)發(fā)(第2版)
- Revit技巧精選應(yīng)用教程
- 這樣學(xué)Excel數(shù)據(jù)處理與分析更高效(視頻版)
- Linux Shell腳本攻略(第3版)
- IBM Lotus Domino: Classic Web Application Development Techniques
- Photoshop CC 從入門(mén)到精通
- Pluggable Authentication Modules: The Definitive Guide to PAM for Linux SysAdmins and C Developers