Preliminary JasperReports of HelloWorld

Source: Internet
Author: User
Tags cdata first string

In enterprise application system, it is often to output data reports in various formats.

The famous open source project <jasperreports can solve this problem very well.

Using JasperReports, you can populate your data with pre-formatted reports and export reports in a variety of formats.

Explains the basic steps of JasperReports report generation:

1. A file with the extension. Jrxml is a standard XML file that defines the format and data composition of the report. This file can be generated and edited visually by JasperReports's "golden partner" ireport.

2. jrxml files are compiled by the JasperReports API and will generate a binary file with a. jasper extension.

3. You can call the JasperReports API to populate the data and parameters for the Jasper file, generating files with the extension. Jrprint.

4. Call the JasperReports API to eventually export the Jrprint file to a variety of files in PDF, Excel, HTML, and other formats.

The following will be used ireport + jasperreports generated in the form of a PDF report, the table data from the database, is based on a fixed format report, by the Jasperreport filled.

The build process for the entire report requires two steps:

1. Use ireport to generate a Jrxml file that specifies the format and data of the report.

2. Call the JasperReports API to compile, populate, and export a fixed-format report.

I. Generating a report file (. jrxml) with ireport

The Open Source Tool ireport is used to define the format of the report and can be downloaded from its official website

Http://ireport.sourceforge.net/cap3.html

This application is using the Ireport-1.2.2-windows-installer.exe installer, after booting, follow the wizard to install (to confirm that the system is equipped with 1.4 or more JDK).

The following operations are broadly divided into 12 steps:

Step 1: Launch the program after successful installation, the following screen will be displayed, select the menu "File->new document" to create a new report document, you can select the size of the report, layout, etc. in the popup dialog box.

Step 2: Create a report by default layout, select Menu "Data->connections/datasources"

Specify a database connection or data source, click New in the Pop-up dialog box, and specify the connection parameters for the database (driver class, URL, user name, password, and so on) in the popup dialog box

ireport connect to the database through JDBC, to confirm that the driver class for the corresponding database is placed under the Lib folder of the ireport installation path.

Step 3: Select the menu "Data->report Query"

Enter the SQL statement in the dialog box that pops up:

Select ID, name, salary from EMP,

When you have selected the fields for all queries, click OK.

The SQL statement queries the data that will be displayed in future reports, in addition to SQL statements, there are many other ways to query hql (Hibernate query Language).

Such an operation would generate the following definition in the report's XML file:

<querystring><! [cdata[select ID, name, salary from emp]]></querystring>

<field name= "id" class= "java.lang.Integer"/>

<field name= "name" class= "java.lang.String"/>

<field name= "Salary" class= "java.lang.Double"/>

Where the element defines the query statement used to get the data, in the future jasperreports will query the data in this way.

The $f{element defines the field name and type that corresponds to the query result, which can be used to obtain and display data from the query in future reports using the field name}.

Step 4: Select the menu "Format->styles" to specify the style that the report element will use,

The style here is similar to the CSS style in HTML, a style that specifies the display features of the font, foreground color, background color, and so on, which can be applied to specific report elements after the style is created.

For ease of maintenance, inheritance relationships can be implemented between styles, and child styles will have all the characteristics of the parent style.

In the dialog box that pops up, click New to specify a style named Default (by using style name to specify style names), specifying its font, color, and so on.

Step 5: Continue to specify some other styles, such as "gray-white" style headers for header fonts, specifying the parent style value of these styles as the default style you just created.

That is, specify that default is the parent style for all styles, so that you can specify some characteristics that are common to all styles by editing the default style.

Step 5: Continue to specify some other styles, such as "gray-white" style headers for header fonts, specifying the parent style value of these styles as the default style you just created.

That is, specify that default is the parent style for all styles, so that you can specify some characteristics that are common to all styles by editing the default style.

Step 6: Click Edit->insert element...->static Text or use the toolbar buttons to

Add a static text box to the report page, place the text box in the appropriate position and fill in the text as the header,

Use the element properties view on the right to set the style (style) of these text boxes to the header you just created.

Step 7: Open the fields icon in the Library view on the right, you can see the id,name,salary three identities, which correspond to the "query field" created in step 3.

Drag these fields directly to the appropriate location on the report page with your mouse, which will produce a shape like this in the report XML text:

<textfieldexpression class= "Java.lang.Integer" ><! [cdata[$F {field name}]]></textfieldexpression>

The text. Where the $f{field name} represents the query value for the field.

Step 8: Right-click in the Library view and select "Add...->parameter" from the popup shortcut menu to create the parameter, specifying the name and data type of the parameter in the popup dialog box.

Step 9: Under parameter in the Library view, you can see the parameter named "title" that you just created, drag it to the specified location, and apply the title style you created in step 5, which is the caption for the report.

Such an operation would result in the following in the XML file:

<textfieldexpression class= "Java.lang.String" ><! [Cdata[$P {title}]]></textfieldexpression>

The $p{title} corresponds to the value of the title parameter, which will be assigned to the "title" parameter in the future through the JasperReports API.

Step 10: Select the menu "File->save" to save the generated XML file, note that the file extension is generally jrxml, the file defines the format of the report and its data is composed.

Step 11: The generated report files can be tested with the ireport compilation tool,

Click the toolbar button to compile the report file,

If it doesn't, you can click the toolbar button to display the style generated by the report.

If you click on the toolbar button, ireport will query the data to display the report after populating the data.

Note: The title of the report here shows NULL because there is no "title" assignment to the report, and this is done only by testing the generated report with the ireport tool.

The following will compile, populate, and export the report programmatically through the JasperReports API.

Step 12: In order to be exported in the future PDF format report supports the Chinese display, you can manually edit the ireport generated XML file,

Add the PDF font and encoding settings that support Chinese in the location where the default style is defined:

<style name= "Default" isdefault= "true" mode= "Opaque" pdffontname= "Stsong-light" pdfencoding= "unigb-ucs2-h"/ >

Because other styles inherit the style, they automatically have settings that support the Chinese language.

Second, compile, populate with JasperReports, and export the report can be from the official website of JasperReports

http://www.jasperforge.org/jaspersoft/opensource/business_intelligence/jasperreports/

Download the JasperReports release package, the version used by this app is jasperreports-1.2.3, which will be in the Dist directory of its release package:

Jasperreports-1.2.3.jar

And the following jar files in the Lib directory:

Poi-2.0-final-20040126.jar

Jdt-compiler-3.1.1.jar

Itext-1.3.1.jar

Commons-beanutils-1.5.jar

Commons-collections-2.1.jar

Commons-digester-1.7.jar

Commons-javaflow-20060411.jar

Commons-loggin-1.0.2.jar

Copy to the application's Classpath (CLASSPATH).

In order to support the export of PDF reports in Chinese, a Itextasian.jar file is also required.

The file can be downloaded from http://prdownloads.sourceforge.net/itext/iTextAsian.jar?download.

Note that because you are connecting to the database, you also need to copy the JDBC Driver class package for the database to the classpath.

Compile the report (. Jrxml. Jasper), the static method of the Jaspercompilemanager class Compilereporttofile used to compile the report file the first string parameter is the report XML file to be compiled,

The second string parameter is the compiled file name.

The code looks like this:

 Public void throws Exception {  jaspercompilemanager.compilereporttofile ("Test.jrxml", "Test.jasper");}   

Populate the report (. Jasper. Jrprint), the static method of the Jasperfillmangager class Fillreporttofile used to populate the binary report file.

The method has three parameters:

1. A parameter of type string representing the name of the file to be populated.

2. The second parameter is the map object, which holds the key-value pairs used to populate the report parameters (corresponding to the $p{parameter name} in the report), where the key-value pair {title= Employee list} is stored in the Map object, and the $p{title} location in the corresponding report is populated with the employee list.

3. The third parameter is the database's connection (Java.sql.Connection) object, which requires the connection to implement the query when the method is populated with data.

The code looks like this:

 public  void  Testfill () throws   Exception {map map  = new  Span style= "color: #000000;"    > HashMap ();    Map.put ( "Title", "Employee List" ); Jasperfillmanager.fillreporttofile ( "Test.jasper" , map, getconnection ());  private  Connection getconnection ()   Exception {Class.forName (" Com.mysql.jdbc.Driver "); Connection con  = drivermanager.getconnection ("Jdbc:mysql://localhost:3306/test", "root", "root" );  return   con;}  

Export the report, the Jrpdfexporter class is used to export the. Jrprint formatted report in PDF format,

Similar classes also include the following:

Jrxlsexporter Export to Excel file format

Jrhtmlexporter Export to HTML file format

Jrcsvexporter Export to CSV file format

Jrrtfexporter Export to RTF file format

Jrtextexporter Export to plain text file format

Jrxmlexporter Export to XML file format

The Jasperprint class is used to encapsulate. jrprint file data.

Jrpdfexporter's setparameter is used to specify export properties such as jasperprint objects that need to be everywhere, file names for exported files, and so on.

Jrpdfexporter's Exprotreport method will produce a corresponding PDF report document for exporting the report.

 Public void throws Exception {    = (jasperprint) jrloader.loadobject ("Test.jrprint");     New Jrpdfexporter ();     // Specify the Jrprit data    to export Exporter.setparameter (Jrexporterparameter.jasper_print, JP);      // Specify file name  for export  "Test.xls");     // Implementing export     of reports Exporter.exportreport ();  }   

The following two methods are used to demonstrate the export of Excel format and HTML format reports.

 Public voidTestexporttoexcel ()throwsException {jasperprint JP= (jasperprint) jrloader.loadobject ("Test.jrprint"); Jrxlsexporter exporter=NewJrxlsexporter ();      Exporter.setparameter (Jrexporterparameter.jasper_print, JP); Exporter.setparameter (Jrexporterparameter.output_file_name,"Test.pdf"); Exporter.exportreport (); }    Public voidTestexporttohtml ()throwsException {jasperprint JP= (jasperprint) jrloader.loadobject ("Test.jrprint"); Jrhtmlexporter exporter=NewJrhtmlexporter ();      Exporter.setparameter (Jrexporterparameter.jasper_print, JP); Exporter.setparameter (Jrexporterparameter.output_file_name,"Test.html"); Exporter.exportreport (); }    

Preliminary JasperReports of HelloWorld

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.