Getting Started With JasperReports
来源:百度文库 编辑:神马文学网 时间:2024/04/29 12:42:14
David R. Heffelfinger Introduction
I‘ve recently been researching reporting tools for a project I will be soon be working on. One of the tools I‘ve been looking at is JasperReports. JasperReports is a very popular open source (LGPL) reporting library written in Java. Unfortunately it is not very well documented and I had a hard time coming up with a simple report. After some research, I was able to generate a simple report, this article summarizes what needs to be done to get started with JasperReports. See Resources for information on how to find additional information and documentation about JasperReports.
Getting Started
JasperReports‘ reports are defined in XML files, which by convention have an extension of jrxml. A typical jrxml file contains the following elements:
- the root element. - its contents are printed only once at the beginning of the report - its contents are printed at the beginning of every page in the report. - contains the body of the report. - its contents are printed at the bottom of every page in the report. - defines a report section, all of the above elements contain a band element as its only child element.
All of the elements are optional, except for the root jasperReport element. Here is an example jrxml file that will generate a simple report displaying the string "Hello World!"
PUBLIC "-//JasperReports//DTD Report Design//EN"
"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
For this simple example, I omitted the optional, and elements. The element, unsurprisingly, displays static text on the report, as can be seen, it contains a single element defining the text that will be displayed.
jrxml files need to be "compiled" into a binary format that is specific to JasperReports, this can be achieved by calling the compileReport() method on the net.sf.jasperreports.engine.JasperCompileManager class. There are several overloaded versions of this method, in our example, we will use the one that takes a single String parameter, consult the JasperReport documentation for details on the other versions of the 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 needs to be compiled only once, but for this simple example it is compiled every time the application is executed. Before a report can be generated, it needs to be "filled" with data, this is achieved by calling the fillReport() method on the net.sf.jasperreports.engine.JasperFillManager class. Again, there are several overloaded versions of the fillReport() method, here we will use one that takes three parameters, an instance of net.sf.jasperreports.engine.JasperReport, a java.util.HashMap containing any parameters passed to the report, and an instance of a class implementing the net.sf.jasperreports.engine.JRDataSource interface. The line that accomplishes this in our example above is
jasperPrint = JasperFillManager.fillReport( jasperReport, new HashMap(), new JREmptyDataSource());Since our simple report takes no parameters, we pass an empty HashMap as the second parameter, and an instance of net.sf.jasperreports.engine.JREmptyDataSource as the third parameter. JREmptyDataSource is a convenience class included with JasperReports, it is basically a DataSource with no data.
Finally, in our example, we export the report to a PDF file, this way it can be read by Adobe Acrobat, XPDF, Evince, or any other PDF reader, the line that accomplishes this in our example is
JasperExportManager.exportReportToPdfFile(
jasperPrint, "reports/simple_report.pdf");The parameters are self-explanatory. Conclusion
JasperReports is a very good and popular open source reporting engine, this guide provides enough information to get started with JasperReports. For additional documentation, JasperSoft publishes an eBook titledThe JasperReports Ultimate Guide . Resources
Displaying JasperReports PDF Reports on the BrowserJasperReports‘ Web SiteThe JasperReports Ultimate Guide - an eBook published by JasperSoft, creators of JasperReports
Comments?Contact the author.
I‘ve recently been researching reporting tools for a project I will be soon be working on. One of the tools I‘ve been looking at is JasperReports. JasperReports is a very popular open source (LGPL) reporting library written in Java. Unfortunately it is not very well documented and I had a hard time coming up with a simple report. After some research, I was able to generate a simple report, this article summarizes what needs to be done to get started with JasperReports. See Resources for information on how to find additional information and documentation about JasperReports.
Getting Started
JasperReports‘ reports are defined in XML files, which by convention have an extension of jrxml. A typical jrxml file contains the following elements:
All of the elements are optional, except for the root jasperReport element. Here is an example jrxml file that will generate a simple report displaying the string "Hello World!"
PUBLIC "-//JasperReports//DTD Report Design//EN"
"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
For this simple example, I omitted the optional
jrxml files need to be "compiled" into a binary format that is specific to JasperReports, this can be achieved by calling the compileReport() method on the net.sf.jasperreports.engine.JasperCompileManager class. There are several overloaded versions of this method, in our example, we will use the one that takes a single String parameter, consult the JasperReport documentation for details on the other versions of the 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 needs to be compiled only once, but for this simple example it is compiled every time the application is executed. Before a report can be generated, it needs to be "filled" with data, this is achieved by calling the fillReport() method on the net.sf.jasperreports.engine.JasperFillManager class. Again, there are several overloaded versions of the fillReport() method, here we will use one that takes three parameters, an instance of net.sf.jasperreports.engine.JasperReport, a java.util.HashMap containing any parameters passed to the report, and an instance of a class implementing the net.sf.jasperreports.engine.JRDataSource interface. The line that accomplishes this in our example above is
jasperPrint = JasperFillManager.fillReport( jasperReport, new HashMap(), new JREmptyDataSource());Since our simple report takes no parameters, we pass an empty HashMap as the second parameter, and an instance of net.sf.jasperreports.engine.JREmptyDataSource as the third parameter. JREmptyDataSource is a convenience class included with JasperReports, it is basically a DataSource with no data.
Finally, in our example, we export the report to a PDF file, this way it can be read by Adobe Acrobat, XPDF, Evince, or any other PDF reader, the line that accomplishes this in our example is
JasperExportManager.exportReportToPdfFile(
jasperPrint, "reports/simple_report.pdf");The parameters are self-explanatory. Conclusion
JasperReports is a very good and popular open source reporting engine, this guide provides enough information to get started with JasperReports. For additional documentation, JasperSoft publishes an eBook titledThe JasperReports Ultimate Guide . Resources
Displaying JasperReports PDF Reports on the BrowserJasperReports‘ Web SiteThe JasperReports Ultimate Guide - an eBook published by JasperSoft, creators of JasperReports
Comments?Contact the author.
Getting Started With JasperReports
Getting Started with Eclipse and the SWT - hunterK的专栏
Getting started with the Oracle BPEL Process Manager
The Process (or undertaking) of Getting Started with OFBiz
Part 1 - Getting Started
VisualSVN Server | Getting started
Google Gadgets API - Getting Started
Tutorial: Getting Started Using RMI-IIOP
2010/10-29 Getting Up With You
Getting Your Feet Wet with the SWT StyledText Widget
Geek To Live: Getting Things Done with Google Notebook
使用JasperReports
使用JasperReports
Getting to Grips with Latex - Tables - Latex Tutorials by Andrew Roberts @ School of Computing, University of Leeds
ireport jasperreports报表
Getting intense
It Started Online
Getting Old Before Getting Rich
对JasperReports的调研小结
Getting Things Done: Getting Projects Under Control
Getting to Know Groovy
再看Getting Real
Getting Real: Code Speaks
always getting over you