Jasper Reports 简明教程
JasperReports - Environment Setup
JasperReports 是一个纯粹的 Java 库,而不是一个独立的应用程序。它无法自己运行,因此需要将其嵌入另一个客户端或服务器端 Java 应用程序中。由于它基于 Java,因此可以在支持 Java(JDK 1.3 及更高版本)的任何平台上运行。所有 JasperReports 的功能都汇集在单个 JAR 文件 jasperreports-x.x.x.jar 中。此 JAR 文件以及必需和可选库(.ZIP 文件)可以从以下网站下载: JasperReport Library Link 。从该链接下载最新版本。
JasperReports is a pure Java library and not a standalone application. It cannot run on its own, hence it needs to be embedded into another client or server-side Java application. As it is Java based, it can be run on any platform that supports Java (JDK 1.3 and above). All the JasperReport’s functionalities are gathered in a single JAR file, jasperreports-x.x.x.jar. This JAR along with the required and optional libraries (.ZIP file) can be downloaded from the site: JasperReport Library Link. Download the latest version from this link.
ZIP 文件包括 JasperReports JAR 文件,以及 JasperReports 源代码、依赖的 JAR 文件和大量用于演示 JasperReport 功能的示例。
The ZIP file includes the JasperReports JAR file along with the JasperReports source code, dependent JARs, and a lot of examples demonstrating JasperReport’s functionalities.
JasperReport Environment
为开始创建报告,我们需要设置好环境。将下载的 JasperReport.ZIP 文件解压到任意位置(我们情况下,已将它解压到 C:\tools\jasperreports-5.0.1)。下面是解压的文件的目录结构:
To start creating the reports, we need to set up the environment ready. Extract the downloaded JasperReport.ZIP file to any location (in our case, we have extracted it to C:\tools\jasperreports-5.0.1). The directory structure of the extracted file is same as shown below −

这里是对所有目录的详细说明:
Here is the detail of all the directories −
-
build − Contains the compiled JasperReport class files.
-
demo − Contains various examples, demonstrating several aspects of JasperReports functionality.
-
dist − Contains jasperreports-x.x.x.jar file. We shall add this JAR file to our CLASSPATH to take advantage of JasperReports.
-
docs − Contains a local copy of the JasperReports documentation.
-
lib − Contains all JARs needed, both to build JasperReports and to use it in our applications.
-
src − Contains the JasperReports source code.
-
build.xml − An ANT build file to build the JasperReports source code. If we don’t intend to modify JasperReports, we don’t need to use this file since JasperReports is distributed in the compiled form.
-
changes.txt − A text document, explaining the differences between the current and previous versions of the JasperReports class library.
-
license.txt − A text document that contains the full text of the LGPL (Lesser General Public License) license.
-
readme.txt − A text document, containing instructions on how to build and execute the supplied examples.
基本上,我们仅将 dist 下的 jasperreports-x.x.x.jar 和 lib 目录下的 JAR 用来生成报表。由于 JasperReports 是一个开源工具,如果在 jasperreports-x.x.x.jar 的执行过程中识别到任何缺陷或 bug,我们可以使用 build.xml 文件修复它并再次构建 JAR。
Basically, we only use the jasperreports-x.x.x.jar under the dist and JARs under the lib directory for generating reports. As JasperReports being an open source tool, if any defect or bug is recognized during execution in the jasperreports-x.x.x.jar, we can fix it and build the JAR again using the build.xml file.
Set the CLASSPATH
为使用 JasperReport,我们需将下列文件设置到我们的 CLASSPATH:
To use JasperReport, we need to set the following files to our CLASSPATH −
-
jasperreports-x.x.x.jar, where x.x.x is the JasperReports version. This found under directory C:\tools\jasperreports-x.x.x\dist).
-
All the JAR files under the lib subdirectory (C:\tools\jasperreports-x.x.x\lib).
在安装时,我们使用了 JasperReport 5.0.1 版本。右键单击“我的电脑”,选择“属性”,单击“高级”标签下的“环境变量”按钮。现在使用 C:\tools\jasperreports-5.0.1\dist\jasperreports-5.0.1.jar:C:\tools\jasperreports-5.0.1\lib 更新“路径”变量。现在您已准备好创建您的报表。
At the time of installation, we used JasperReport version 5.0.1. Right-click on 'My Computer' and select 'Properties', click on the 'Environment variables' button under the 'Advanced' tab. Now update the 'Path' variable with this C:\tools\jasperreports-5.0.1\dist\jasperreports-5.0.1.jar:C:\tools\jasperreports-5.0.1\lib. Now you are ready to create your reports.
在本教程的所有示例中,我们都使用 ANT 任务来生成报表。 build 文件负责导入生成报表所需的所有 JAR。因此,正如前面提到的那样,设置 CLASSPATH 仅将帮助那些希望在不使用 ANT 的情况下生成报表的人。
In all the examples in this tutorial, we have used ANT tasks to generate reports. The build file takes care of importing all the required JARs for generating reports. Hence, setting CLASSPATH as mentioned above will only help those who wish to generate reports without using ANT.
Build Setup
本教程中的所有示例 −
All the examples in this tutorial −
-
have been written using simple Text Editor.
-
have been saved under the directory C:\tools\jasperreports-5.0.1\test\src\com\tutorialspoint.
-
have been compiled and executed from command prompt, using Apache ANT. We will use a baseBuild.xml file, which we shall import in ANT build.xml file in the subsequent chapters. Save this file to C:\tools\jasperreports-5.0.1\test. Following is the content of baseBuild.xml file −
<?xml version = "1.0" encoding = "UTF-8"?>
<project name = "JasperReportExample" basedir = ".">
<description>Previews our JasperReport XML Design</description>
<property name = "file.name" value = "jasper_report_template" />
<!-- Directory where the JasperReports project file was extracted
needs to be changed to match the local environment -->
<property name = "jasper.dir" value = "../" />
<property name = "dist.dir" value = "${jasper.dir}/dist" />
<property name = "lib.dir" value = "${jasper.dir}/lib" />
<property name = "src.dir" value = "src" />
<property name = "classes.dir" value = "classes" />
<property name = "main-class" value = "com.tutorialspoint.HelpMe" />
<path id = "classpath">
<pathelement location = "./" />
<pathelement location = "${classes.dir}" />
<fileset dir = "${lib.dir}">
<include name = "**/*.jar" />
</fileset>
<fileset dir = "${dist.dir}">
<include name = "**/*.jar" />
</fileset>
</path>
<target name = "compile" depends = "clean-sample">
<mkdir dir = "${classes.dir}"/>
<javac srcdir = "${src.dir}" destdir = "${classes.dir}"
classpathref = "classpath" />
</target>
<target name = "run" depends = "compile">
<echo message = "Running class : ${main-class}"/>
<java fork = "true" classname = "${main-class}">
<classpath>
<path refid = "classpath" />
</classpath>
</java>
</target>
<target name = "clean-sample">
<delete dir = "${classes.dir}" />
<delete file = "./${file.name}.jasper" />
<delete file = "./${file.name}.jrprint" />
</target>
</project>
此文件包含所有必需的目标,例如清理目录、编译 java 文件以及执行类文件。
This file has all the required targets, like cleaning the directories, compiling the java files, and executing the class files.
以下是对 baseBuild.xml 中提到的各种目录的详细信息。假设当前目录是 C:\tools\jasperreports-5.0.1\test) −
Following are the details, mentioned by various directories in baseBuild.xml. Assuming current directory is C:\tools\jasperreports-5.0.1\test) −
-
jasper.dir − is C:\tools\jasperreports-5.0.1 directory
-
lib.dir − is C:\tools\jasperreports-5.0.1\lib directory
-
src.dir − is C:\tools\jasperreports-5.0.1\test\src
-
classes.dir − is C:\tools\jasperreports-5.0.1\test\classes
-
main-class − com.tutorialspoint.HelpMe. This class is a simple class executed, when no class file name is passed from the command line. Save this file to C:\tools\jasperreports-5.0.1\test\src\com\tutorialspoint.
package com.tutorialspoint;
public class HelpMe {
public static void main(String[] args) {
System.out.println("This is the default class executed."
+ "Please pass the fully qualified class" + " name to be executed as command line"
+ " parameter, for example," + " com.tutorialspoint.HelpMe ");
}
}
Jasper Managers Classes
有多个类可用于编译 JRXML 报告设计、填充报告、打印报告、导出到 PDF、HTML 和 XML 文件、查看生成的报告以及报告设计。
There are number of classes, which will be used to compile a JRXML report design, to fill a report, to print a report, to export to PDF, HTML & XML files, view the generated reports, and report design.

这些类的列表 −
The list of these classes is −
-
net.sf.jasperreports.engine.JasperCompileManager − Used to compile a JRXML report template.
-
net.sf.jasperreports.engine.JasperFillManager − Used to fill a report with data from the data source.
-
net.sf.jasperreports.engine.JasperPrintManager − Used to print the documents generated by the JasperReports library.
-
net.sf.jasperreports.engine.JasperExportManager − Used to obtain PDF, HTML, or XML content for the documents produced by the report-filling process.
-
net.sf.jasperreports.view.JasperViewer − It represents a simple Java Swing application, which can load and display reports.
-
net.sf.jasperreports.view.JasperDesignViewer − Used at design time to preview the report templates.
Setting up Apache ANT
我们将使用 Apache ANT 构建所有示例。因此,请检查 ANT - Environment Setup 章节在您的系统上设置 Apache ANT。
We are going to build all the examples using Apache ANT. So, kindly check ANT - Environment Setup chapter to setup Apache ANT on your system.