Generate reports using JFREEREPORT

xiaoxiao2021-03-06  64

Generate reports using JFREEREPORT

Author: Xiao Jing

Thesis

JFReereport is used to generate a report. JFReereport's data inherits the TableModel interface from Swing Components, formatting the report using XML-based report formatting files. The report generated by JFReeReport can make a preview, print or save files such as PDF, Excel, HTML, etc. in multiple formats. The author introduces how to define the report format definition file in this paper, how to use the JFReeReport generation / preview report and turn the report to other formats.

Key words

JFReerePort Report Generation Preview

The report generation has always been an important role in the process of enterprise informationization. It is also a one-course that is more difficult to implement. Today the author introduces a new report to generate components - Jfreereport. JFReereport is a set of Java packets provided by the LGPL authorization protocol, which uses a variety of reports. The data of JFReereport inherits the TableModel interface from the Swing component, formatting the report using XML-based report format definition files. The report generated by JFReereport can make a preview, print, and support the export of files such as PDF, Excel, CSV, HTML, etc. in multiple formats. More importantly, JFReereport not only supports a C / S structure system, but also supports online reports in systems based on B / S structures. More detailed information about JFREEREPORT, please visit JFReereport's official website JFree.org.

1 environment preparation

1.1 JFREEREPORT components

Please go to http://prdownloads.sourceforge.net/jfreeereport/jfreereport-0.8.4_7.zip?download to download the JFReerePort component, download a zip file, then decompress the zip file to C: / JFREEREPORT (later chapter The% jfreereport_home% will expressed this directory using% JFREEREPORT_HOME%).

1.2 JFREEREPORT extension components

Please go to http://www.jfree.org/jfreeereport/jfreeereport-ext-0.8.4_7.zip to download the JFReerePort extension component, which is used to support the online display of the report generated by the JFReereport component. Please decompress the decade to the C: / JFREEREPORT-EXT directory (% jfreereport_ext_home by% JFREEREPORT_EXT_HOME%) is used to indicate this directory)

1.3 ANT tool

Apache provides a Java-based automation scripting engine, please go to http://ant.apache.org/ download Ant's executable, about how to use Ant, please view the help documentation of Ant or http: // Ant. Online Help Document on the Apache.org / website. In the example, it is mainly used to compile the Java code.

1.4 The code provided by the author

In order to run the examples and related resource files mentioned in this article, please download the author of the VivianjDemo.zip file and Chinese conversion tool GB2UNICODE.jar. Then decompress the% JFREEREPORT_HOME% / VIVIANJDEMO (% demo _Home% in the later chapter) is used.

2 JFREEREPORT basic steps to generate reports

We first demonstrate a simple example that describes some of the necessary steps to generate reports using JFReeReport.

2.1 instance description

In this example, we generate 100 data in TableModel and use the preview feature provided by the JFReereport component to display the generated report on the screen.

[Note] To simplify, here is just displaying data by one, not any modification and statistical work, so the report format definition file is not used. 2.2 Code Preparation

The code and related comments of the entire demo instance (HelloWorld.java) are as follows, if you perform the steps specified in 1.3, you can see this file in% demo _Home% / src / org / vivianj / jfreereport /.

/ **

* HelloWorld.java

* /

Package Org.vivianj.jfreereport;

Import java.awt.color;

Import java.awt.event.windowadapter;

Import java.awt.event.windowevent;

Import java.awt.geom.point2d;

Import javax.swing.table.defaulttablemodel;

Import javax.swing.table.tablemodel;

Import org.jfree.report.boot;

Import org.jfree.report.ementAlignment;

Import org.jfree.report.jfreereport;

Import org.jfree.report.reportprocessingexception;

Import org.jfree.report.efficient.textfieldElementFactory;

Import org.jfree.report.modules.gui.base.previewdialog;

Import org.jfree.ui.floatdimension;

/ **

* Simple examples of generating reports using JFReereport, used to demonstrate some basic steps to generate reports using JFReereport

*

* In this example, in order to simplify the operation, the report definition is to use Java direct encoding.

*

* @ Author: Bookman

* /

Public class helloworld

{

/ **

* Handling window closing events

* /

Protected Static Class CloseHandler Extends WINDOWADAPTER

{

Public void windowClosing (Final WindowEvent Event)

{

System.exit (0);

}

}

/ **

* Create and display simple reports

* /

Public HelloWorld ()

{

// Get the data that needs to be used to create a report

FINAL TABLEMODEL DATA = CREATEDATA ();

// Get the definition content you want to use by the report

Final Jfreereport Report = CreateReportDefinition ();

// Connect the report definition and data

Report.SetData (DATA);

Try

{

// Put the generated report into the preview window

Final PreviewDialog Preview = New PreviewDialog (Report);

Preview.addwindowlistener (New CloseHandler ());

Preview.pack ();

/ / Show report preview windows

Preview.setvisible (TRUE);

}

Catch (ReportProcessingException E)

{

System.out.println (e);

}

}

/ **

* Create data that generates reports needed

*

* @ Return to a TableModel instance

* /

Private TableModel CreateData ()

{

Final Object [] columnNames = new string [] {"column1", "column2"};

Final DefaultTableModel Result = New DefaultTableModel (ColumnNames, 100);

INT rownum = 0;

INT colnum = 0;

For (; rownum <100; rownum )

{

Result.SetValueat ("Say Hello" Rownum "Supreme", ROWNUM, 0);

Result.SetValueat ("Say World" Rownum "Times", RowNum, 1);

}

Return Result;

}

/ **

* Create a report definition

*

* @ Return to a report definition instance

* /

Private jfreereport cretereportdefinition ()

{

Final Jfreereport Report = New JFREEREPORT ();

Report.setName ("a very Simple report");

/ **

* Define the style to display the first column of the report

* /

TextFieldElementFactory Factory = new textfieldElementFactory ();

Factory.setName ("T1");

Factory.Setabsolute (new point2d.float);

Factory.SetMinimumsize (New Floatorension (150, 20));

Factory.SetColor (color.ble);

Factory.sethorizontalalignment (ElementAlignment.Left);

Factory.setVerticalAlignment (ElementAlignment.middle);

Factory.setnullstring ("-");

Factory.setfieldName ("Column1");

Report.getItemband (). addelement (factory.createElement ());

/ **

* Define the style to display the second column of the report

* /

Factory = new textFieldElementFactory ();

Factory.setName ("T2");

Factory.SetabsolutePosition (New Point2D.float (200, 0));

Factory.SetMinimumsize (New Floatorension (150, 20));

Factory.SetColor (color.ble);

Factory.sethorizontalalignment (ElementAlignment.Left);

Factory.setVerticalAlignment (ElementAlignment.middle);

Factory.setnullstring ("-");

Factory.setfieldName ("Column2");

Report.getItemband (). addelement (factory.createElement ());

/ **

* Returns an instance of a report definition

* /

RETURN Report;

}

Public static void main (final string [] ARGS)

{

// Initialize JFReereportBoot.start ();

// Call the demo instance

New helloworld ();

}

}

2.3 Operation example

If you have executed the steps specified in 1.3, you can enter the command line interface, then enter the% demo_home% directory, modify the relevant settings in the setEnv.cmd, and perform Serenv.cmd to set the environment variable. Execute java org.vivianj.jfreereport.helloWorld to view the run results. Below this picture is a screenshot of the author's post-executive results:

Everyone can see that JFReereport has automatically implemented a paging. The above picture shows the first page of the data, you can view the contents of the other pages through the toolbar.

2.4 Basic steps explain

Generating a report using JFReereport usually requires the following three basic steps:

Generate data that can be accessed via the TableModel interface, such as the function of completing the CreateData method in this example generates a JFReeRerePort instance, he defines how we formatted data, such as the function completed by the CreateReportDefinition method in this example to connect the data and JFReereport instances Get up, and the JFReereport instance is passed to an instance of PreviewDialog to display to the user.

3 Generate complex reports using JFReereport

3.1 Report Definition File

The report definition file is an important file for JFReereport to generate a complex report. He is an XML document that mainly describes how to generate a complex report using the specified format, and use the report definition file. You can only need to update the report definition file when you need to modify the report format. No need to modify application code.

3.1.1 Report Definition File Classification

Two XML-based report definition files are used to save report definition information: Simple format and extension formats. Obviously, the simple format is unable to describe all report definition information supported by JFReereport, but he is easier to use it. The extension format can provide complete support for the report definition of JFReereport, but the extension format is too detailed, not easy to use.

About these two report definition format files supported by the label content and how to write report definition files in these two formats, please refer to the relevant section of the JFReereport-0.8.3-A4.pdf under the% jfreeereport_home, this file appendix Also included DTD documents for these two format report definition files for your reference. Of course, everyone also provides a variety of formal definition files in the example of JFReereport, which basically covers the commonly used report format definition, and you can refer to these examples to write your own report definition files.

3.2 Code Preparation

This example is basically consistent with the code in 2.2, but the report definition is no longer implemented by Java coding, but is provided by the report definition file, so the call is slightly different. The detailed code is as follows, please pay attention to the part of the bold display. :

/ **

* JFReereport.java

* /

Package Org.vivianj.jfreereport;

Import java.io.file;

Import java.text.MessageFormat;

Import javax.swing.table.tablemodel;

Import javax.swing.jframe;

Import org.jfree.ui.refineryutilities;

Import org.jfree.report.boot;

Import org.jfree.report.modules.gui.base.previewFrame

Import org.jfree.report.Modules.Parser.Base.ReportGenerator; import Org.jfree.report.jfreereport;

Import Org.vivianj.jfreereport.tablemodel.sampledata;

/ **

* Use JFReereport to generate an example of complex reports,

* Some basic steps used to demonstrate using JFReereport to generate complex reports

*

* In this example, the report definition uses a report definition file, which is saved in the Report3.xml file under C: \

* The report definition used in this example uses a simple report definition format

*

* @ Author: Bookman

* /

Public Class Jfretest

{

Public String Urlname, Final TableModel Data

{

// Create a reference to the report definition file

Final file in = new file (urlname) ;;

IF (in == NULL)

{

System.out.Print ("in is null");

Return;

}

System.out.print ("Processing Report: in);

Final ReportGenerator Gen = ReportGenerator.getInstance ();

Try

{

// Get the definition content you want to use from the report definition file

Final Jfreereport Report1 = Gen.ParsReport (in);

IF (Report1 == Null)

{

System.out.print ("Report1 Is Null");

Return;

}

// Connect the report definition and data

Report1.SetData (DATA);

// Put the generated report into the preview window

Final PreviewFrame Frame1 = New PreviewFrame (Report1);

Frame1.SetDefaultCloseOperation (jframe.exit_on_close);

Frame1.pack ();

RefineryUtilities.positionFramerandomly (frame1);

Frame1.setvisible (TRUE);

Frame1.Requestfocus ();

}

Catch (Exception E)

{

System.out.print ("Report.definitionFail -------------------- / r / n" e);

}

}

Public static void main (string [] args)

{

Boot.start ();

Final TableModel Data3 = New Sampledata ();

JFretest JFT = New JFREETEST ("C: //report3.xml", DATA3);

}

}

4 Chinese garbled problem

Everyone will find some display content in the report header definition inside, if you change him directly into Chinese, displaying it on the report, because these report definition files are XML documents His encoding default setting is ISO-8859-1, so there is a problem with Chinese garbled, and there are two solutions:

1. The easiest way is to modify the Encoding settings to GB2312

2. There is also a method that does not modify the set of encoding, but uses these Chinese content to replace them. [Note] The author provides a GUI interface to provide this conversion function, you only need to enter Chinese, you can get the corresponding Unicode code. Please download the author's GB2UNICODE.jar, set the Java runtime environment, then execute java -jar GB2UNICODE.JAR is OK.

5 summary

The reporting problem is a focus of concern in corporate informationization. It is also difficult to achieve customization, the author introduces a new report generation package-Jfreereport in this article, and he can generate compliance based on the report format defined in the XML document. Reports required by customers. 2 simple examples have been given in the article, demonstrate how to use the JFReereport to generate a report, I hope to help you familiarize yourself with JFReereport's work. At the same time, a solution to Chinese issues that may encounter in the JFReereport process is given.

In fact, JFReereport also supports the report in JSP, servlet, just need to use his extended components, the author is not given here, you can refer to the downloaded JFREEREPORT exemplary exemplass.

Reference:

JFreereport online help document http://www.jfree.org/jfreeereport/index.html

JFREEREPORT's javadoc http://www.jfree.org/jfreeeport/javadoc/index.html

About author

Xiao Jing is currently a software center software engineer, IBM DeveloperWorks / Bea dev2dev, IBM DeveloperWorks / Bea dev2dev, IBM DeveloperWorks / Bea dev2dev, mainly studying J2EE programming technology, Web Service technology and them on WebSphere, WebLogic, Apache platform. Implementation, there is IBM's Developing with WebSphere Studio certificate, personal website: http://vivianj.go.nease.net/

转载请注明原文地址:https://www.9cbs.com/read-119937.html

New Post(0)