Use ireport to create a Report Template

Source: Internet
Author: User
Use iReport to make a report template (http://www-900.ibm.com/developerworks/cn/java/j-ireport/index.shtml)

At present, many companies and individuals are using iReport to create Report Templates. I have not been in touch for a long time. Here, I will discuss some of my experiences and problems encountered during my practice.

I. iReport Introduction
Speaking of iReport, you have to first introduce Jasperreport. Jasperreport is a report production program. You need to write an XML file according to the rules it has set, and then get the format file to be output by the user. The output file formats include PDF, HTML, XML, XLS, CVS, and so on. IReport is a visual development tool for creating XML files of Jasperreport.

Ii. template Board
First you have to download an iRport (Address: http://ireport.sourceforge.net), now the highest version is 0.4.0, we use 0.2.2 here.

Decompress iReport and edit the iReport. bat file in the directory.


@echo offset JAVA_HOME=C:/j2sdk1.4.0_03set ANT_HOME=C:/antset IREPORT_HOME=C:/Documenti/progetti/iReport/iReport2/rem %ANT_HOME%/bin/ant javadocs%ANT_HOME%/bin/ant iReport

You only need to set the corresponding path. Here ant is used for running. Of course, if you do not want to install ant, you can also find startup in the noAnt folder. the bat file can be run.

Create a new template.


Select the menu item "Datatsource" and click its sub-menu item "Connection/Datasources". An edit box is displayed, and select "New,


Enter the data source name and select JDBC Driver. when entering the jdbc url, you can click "Wizard" to configure your URL Based on the JDBC Driver you selected, however, you must modify the IP address and DatabaseName, and enter the database name, user name, and password. Click "Test" to confirm the connection status and save the settings.

Is a prepared report template:


$ F {} is used to indicate that this is a database field. $ F {WINDOWS_NAME}, $ F {NUMBER}, $ F {vname}, $ F {SP_NAME}, and $ F {SP_TYPE} Are Fields in the database, it is also the field I want to use in the template. For the $ F {NUMBER}, $ F {vname}, $ F {SP_NAME}, and $ F {SP_TYPE} fields, the results can be displayed even if they are placed in other fields. For example: pageHeader domain, columnHeader domain, columnFooter domain, and so on, but because there are multiple columns of data to be displayed, it must be placed in the "detail" domain.

After the template is finished, configure the template query statement, as well as fields, variables, and parameters.

First, enter the query statement, such as "select * from v_baseinfo_statbanjiereport". Click "Read fields" to query all the field names of the table.


If you want a conditional query, add the Where statement next to it, for example, "select * from v_baseinfo_statbanjiereport where DEPT_NAME = $ P {orgname} and END_SP_DAY = $ P {etime }", click "Save query to report" to Save.

Note that you can directly write the parameter to the Where statement, as shown in the following figure: "where DEPT_NAME = 'China' and END_SP_DAY = '2017-11-23 '"; however, if the parameters passed from the java program or jsp page are Chinese characters, you must convert the codes or replace them with English characters and numbers. For example, the date parameter "2004-11-23 ", jasperreports does not seem to be parsed. If you change the SQL statement to "select * from v_baseinfo_statbanjiereport where DEPT_NAME = '$ P {orgname}' and END_SP_DAY = '$ P {etime }'", then an exception is thrown, because Jasperreport sets the SQL statement to a precompiled statement "select * from v_baseinfo_statbanjiereport where DEPT_NAME =? And END_SP_DAY =? ", When parameters are used in SQL statements, except for the specific" $ P {} "symbol, there cannot be other punctuation marks, do not include punctuation marks, so the date format must be changed to "20041123 ".

Set template fields:


Set template parameters:


After all these are done, click the "Save" button to generate an XML file, and then click the "Compile" button to generate a file with the suffix jasper.

Now you can preview the Template Effect, select the sub-menu items "PDF preview", "HTML preview", "JAVA 2D preview", "Excel preview", "CSV preview", and "JRViewer preview" for the menu item "Build ". .. and then click "Execute report". If you want to preview the data, activate the newly configured data source first, select the sub-menu bar "Set active connection" under "Build" in the menu bar. an edit box is displayed. Select a data source name and click "OK ".

In this case, we will introduce a jsp page. Suppose it is pdf. jsp, then the following is the code of pdf. jsp:


<% @ Page import = "dori. jasper. engine. * "%> <% @ page import =" java. util. * "%> <% @ page import =" java. io. * "%> <% @ page import =" java. SQL. * "%> <% File reportFile = new File (application. getRealPath ("/reports/ribanjietongji. jasper "); Map parameters = new HashMap (); parameters. put ("orgname", "zhongguo"); parameters. put ("etime", "20041123"); Connection conn = null // The following ignores the database Connection process //...... Byte [] bytes = JasperRunManager. runReportToPdf (reportFile. getPath (), parameters, conn); response. setContentType ("application/pdf"); response. setContentLength (bytes. length); ServletOutputStream ouputStream = response. getOutputStream (); ouputStream. write (bytes, 0, bytes. length); ouputStream. flush (); ouputStream. close (); %>

In the program, we need to explain that "/reports/test. jasper "this path is relative to your jsp page. For example, if your jsp page is saved in D ://.. in the/ROOT/directory, you must create a report directory under this directory, and then put the prepared report template file to this directory.


Map parameters = new HashMap();parameters.put("orgname", "zhongguo");parameters.put("etime", "20041123");

"Orgname" and "etime" must be the same as the parameter name defined in the template.

Iii. Conclusion
Once the entire template is complete, there is nothing to mention or wrong. I hope you can discuss it with me.

About the author
Shu Feng, contact: feng.shu@chinacreator.com, profile: I am currently Hunan Changsha Railway college science and technology computer system integration Co., Ltd. Software Center Software Engineer, mainly research J2EE programming technology.

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.