Original author: David R. Heffelfinger
English Original: Getting Started with JasperReports
preface:
Because the needs of the new project, I have been studying the report tool recently. One is JasperReports. JasperReports is a very popular open source (LGPL) report tool library written in Java. But unfortunately, it lacks a good document so I have a simple report. After a period of pondering, I made a simple statement, this article is to summarize some of the work you need to do in JasperReport. In the resource information you can find more documentation and information about JasperReports.
Quick start
JasperReports' reports are defined with an XML file, and the JRXML will be used for the name. A typical JRXML file contains the following elements:
In addition to the root element, all elements are optional. Here is an example of a JRXML file, which will generate a simple report that displays string "Hello World!".
XML Version = "1.0"?>
In this simple example, I have not written option
The JRXML file requires "Compilation" into a binary format of the JasperReports specification, which can call the method CompileReport () for class net.sf.jasperreports.Engine.jasperCompileManager. This method has several overloaded forms, in our example, we will use a single string as a method of parameter. Refer to the JasperReport documentation to get the details of other versions of this method.
Public Class JasperReportsIntro {
Public static void main (string [] args)
{
JasperReport JasperReport;
JasperPrint JasperPrint
Try
{
jasperReport = JasperCompileManager.compileReport ( "reports / jasperreports_demo.jrxml"); jasperPrint = JasperFillManager.fillReport (jasperReport, new HashMap (), new JREmptyDataSource ()); JasperExportManager.exportReportToPdfFile (jasperPrint, "reports / simple_report.pdf");
}
Catch (JRException E)
{
E.PrintStackTrace ();
}
}
}
A JRXML file only needs to be compiled once, but in this simple example, each executing program will be compiled. Before the report is generated, you need to "fill" it, here we call the FillReport () method in the net.sf.jasperreports.Engine.jasperfillManager class. Similarly, it also has many overloaded forms. Here we use one of the methods with three parameters, the first is the example of JasperReport, the second is HashMap, which can contain any parameters to be passed to Report, Three parameter objects are to implement JRDataSource interface. This method is in our example:
Jasperprint = JasperfillManager.FillReport
JasperReport, new hashmap (), new jremptyDataSource ());
Because the Report class in our example does not require any parameters, it is passed to a empty HashMap, the third parameter net.sf.jasperReports.Engine.jremptyDataSource is a simple class that implements JRDataSource interface, this basic class is not Contains any data.
The parameters are self-evlantory.
Finally, in the example, we export the report as a PDF file, you can open with Adobe Acrobat, XPDF, EVINCE, or other PDF reading tools. The code in the example is: JaspeExportManager.exportReportTopdffile (JasperPrint, "Reports / Simple_Report.pdf");
end
JasperReports is a very excellent and popular open source report engine, which provides enough information to quickly start JasperReports. To get a richer document, Jaspersoft released an e-book: The JasperReports Ultimate Guide
related resources:
Displaying JasperReports PDF Reports on The Browser
Creating Database Reports with JasperReports
JasperReports' Web Site