QQ group: 26651479
Detailed process of using ireport to create a report (in Windows)
I. Preparation
1.1 install JDK
: Http://www.sun.com
Verify whether JDK or JRE can be run by default. a pile of information starting with usage: Java is displayed when the command line (CMD) is entered into X:> JAVA.
If it is unavailable, you must configure the environment variables in Windows:
Path: Add "; Java installation directory" to the end"
Java_home: "Java installation directory"
Classpath: "Java installation directory/bin"
Verify whether JDK or JRE can be run in cmd.
1.2 download ireport
Address: http://ireport.sourceforge.net/
1.3 prepare the database
Ireport supports most databases as long as the database can provide JDBC drives.
[Note] the database version must be compatible with the driver used in the lib directory in the ireport folder. We recommend that you download the latest driver version.
Ii. Configure Basic Information
2.1 language used in the configuration interface
Set the environment and Click Tools-options to open the ireport System Configuration basic information dialog box. Select the language in "language" and [apply] to confirm.
2.2 configure database connection
This is the interface between reports and databases. Click [report CES] to open the dialog box.
Ireport records all previously used connections. Unless you delete them manually, they will exist in the configuration list, regardless of whether they are available or not.
Click new to enter the new connection configuration page, and enter the information required for the JDBC connection. ireport supports multiple data source connections.
If you need to provide Chinese content for the report, you can work in the jdbc url, for example, enter:
JDBC: mysql: // localhost/subrdb? User = ***** & Password = ***** & useunicode = true & characterencoding = gb2312
The [test] button can be used to test whether the database is available.
2.3 create basic configurations for an empty report
Click "New Report", the first tool in the toolbar, to create a report.
2.4. Several Important Concepts
1) ireport output format
Supported: PDF, HTML, CSV, java2d, Excel, plain text, jrviewer, among which PDF and jrviewer are most commonly used.
Jrviewer uses the C/S method as the report output format and outputs it in the jframe framework. Jasperreport provides the default jrviewer output class.
2) dynamic object variables, parameters, and fields of reports
· Field (fields): the content of the database extracted from the database and expected to appear in the report. For example, all values of an ID $ f {filedsname}
· Parameter (parameters): This is the entry that your application needs to provide to the report.
For example, if you want to provide the where statement condition value when the report is interpreted, you can use the $ P {parametername} parameter}
· Variables: This is the performance of some logical operations in the report, such as the Statistical Value $ V {variablesname}
3) Compiling, static running, and Dynamic Running
What jasperreport needs to run is a file with the Jasper suffix. During the compilation process, the file with the jrxml suffix is actually generated into a file with the Jasper suffix.
Static and dynamic operations are relative. The latter includes data sources, such as databases.
Static running is not related to the data source. If an object related to the data source appears in the report, it is displayed as null.
4) report structure
These include title, pageheader, columnheader, detial, columnfooter, pagefooter, summary, groupheader, and groupfooter.
· Title: each report generally has a name, such as a sales report. title is the best place to hold the name.
· Pageheader: some common elements of a report, such as the page number, creation time, creator, and so on, which is a good choice.
· Columnheader: the name of the column to be placed. Remember not to use column data.
· Detial: place data that needs to be recycled, such as sales record data.
· Columnfooter: statistical calculation value or description of columns at the column level.
· Pagefooter: statistical value or page description at the page level.
· Summary: the number of pages that your report may consist. After several pages of detial, a statistics page appears.
· Groupheader: the content of each table may need to be divided and displayed and computed based on a certain attribute.
For example, if records are separately displayed in each group, you can define a group. The groupheader is the best place to place the group description or group logo.
· Groupfooter: group placement statistics or descriptions.
2.5 add objects to a table
1) Add a static object (static text)
Add text, drag it to the [static text] label to the desired location, and double-click it to write it into the content. You can also edit its size and color.
Add an image, click [image tool], and select an image. The operation is similar to text. The operation steps for other static objects are similar.
2) create an SQL query statement
Choose data source> REPORT query to open the SQL input dialog box and enter an SQL statement in the reportsql query label.
You can automatically or manually obtain available fields for database tables. Click OK to save the report.
3) create a dynamic field object
The field is also a field in the database. You can drag and drop the detial segment through the Field List ([text field] can be found on the toolbar ).
4) create a group
A group is a very important concept. A report can have multiple groups. Each group is marked with a keyword.
For example, it is expected that the bug statistics are based on projects (or products. You can set up a project tag group.
5) add and use parameters (parameter)
The function of a parameter is generally to provide a parameter to the report entry, such as the where condition expression of an SQL statement.
Choose preview> report parameters to open the report Parameter List dialog box (corresponding tools can be found on the toolbar ).
When a parameter is provided by an application, the report interpretation engine can replace these variables and then execute the SQL statement.
For example, after the parameter ID is added, you can add where memid = $ P {ID} to the query statement of the report. If this parameter is not provided on the page, its value is null.
6) add and use variables
Similar to the definition of variables, choose preview> report variables to open the report variable List dialog box (corresponding tools can be found on the toolbar)
For example, define a counter for a bug. In addition to custom variables, ireport also provides embedded (buildin) variables, such as page number and row record number.
Iii. jsp file generation
The following is an example of a JSP page that generates a report file:
<% @ Page pageencoding = "UTF-8" %>
<% @ Page import = "net. SF. jasperreports. Engine. *" %>
<% @ Page import = "net. SF. jasperreports. Engine. util. *" %>
<% @ Page import = "net. SF. jasperreports. Engine. Export. *" %>
<% @ Page import = "net. SF. jasperreports. View. *" %>
<% @ Page import = "Java. util. *" %>
<% @ Page import = "Java. Io. *" %>
<% @ Page import = "Java. SQL. *" %>
<HTML>
<Head>
<Title> </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
</Head>
<Body>
<%
Request. setcharacterencoding ("UTF-8 ");
String printer = request. getparameter ("Printer"); // parameters sent from the page
String Doc = request. getparameter ("Doc"); // The name of the report to be called (for example, "member. Jasper" indicates member)
String loadtype = request. getparameter ("loadtype"); // file type of the report to be generated
// Connect to the database
String url = "JDBC: mysql: // 127.0.0.1: 3306/test ";
Class. forname ("com. MySQL. JDBC. Driver ");
Connection conn = drivermanager. getconnection (URL, "root", "root ");
// Set the number of rows in the generated orders table
Map parameters = new hashmap ();
Parameters. Put ("Printer", printer); // send the page parameters to the report.
// Set the Jasper File
// If it is written in servlet, the "application" of this sentence should be written as "this. getservletcontext ()"
File reportfile = new file (application. getrealpath ("/reports/" + Doc + ". Jasper "));
Jasperprint = jasperfillmanager. fillreport (reportfile. getpath (), parameters, Conn );
Try
{
Jrexporter exporter = NULL;
If ("xls". compareto (loadtype) = 0)
{
Response. setcontenttype ("application/vnd. MS-excel ");
Response. addheader ("content-disposition", "attachment; filename =" + jasperprint. getname () + ". xls ");
Exporter = new jrxlsexporter ();
Exporter. setparameter (jrxlsexporterparameter. is_one_page_per_sheet, Boolean. False );
Exporter. setparameter (jrxlsexporterparameter. is_remove_empty_space_between_rows, Boolean. True );
Exporter. setparameter (jrxlsexporterparameter. is_white_page_background, Boolean. False );
}
Else if ("pdf". compareto (loadtype) = 0)
{
Response. setcontenttype ("application/pdf ");
Response. addheader ("content-disposition", "attachment; filename =" + jasperprint. getname () + ". pdf ");
Exporter = new jr1_exporter ();
}
Else if ("html". compareto (loadtype) = 0)
{
Response. setcontenttype ("application/octet-stream ");
Response. addheader ("content-disposition", "attachment; filename =" + jasperprint. getname () + ". html ");
Exporter = new jrhtmlexporter ();
Exporter. setparameter (jrhtmlexporterparameter. is_using_images_to_align, Boolean. False );
}
Else if ("rtf". compareto (loadtype) = 0)
{
Response. setcontenttype ("application/octet-stream ");
Response. addheader ("content-disposition", "attachment; filename =" + jasperprint. getname () + ". rtf ");
Exporter = new jrrtfexporter ();
}
Else if ("CSV". compareto (loadtype) = 0)
{
Response. setcontenttype ("application/octet-stream ");
Response. addheader ("content-disposition", "attachment; filename =" + jasperprint. getname () + ". CSV ");
Exporter = new jrcsvexporter ();
}
Else if ("XML". compareto (loadtype) = 0)
{
Exporter = new jrxmlexporter ();
}
Else
{
Jasperviewer. viewreport (jasperprint); // This is not recommended
Throw new exception ();
}
Exporter. setparameter (jrexporterparameter. jasper_print, jasperprint );
Exporter. setparameter (jrexporterparameter. output_stream, response. getoutputstream ());
Exporter. exportreport ();
}
Catch (jrexception E)
{
System. Out. println ("----- report. jsp. Err:" + E );
Throw new servletexception (E );
}
Catch (exception E)
{
System. Out. println ("----- View Report -----");
}
Finally
{
If (Conn! = NULL) Try {conn. Close ();} catch (exception e) {e. printstacktrace ();}
}
%>
</Body>
</Html>