Nowadays, many companies and individuals are using Ireport to make report templates. I have no longer contact. Here, I will discuss some of my experiences in practice and some problems encountered.
First, Ireport introduces that Ireport has to introduce JasperReport, JasperReport is a report creation program, and the user needs to write an XML file according to the rules it make, and then get the format file that the user needs to output. It supports the output file formats including PDF, HTML, XML, XLS, CVS, and more. And Ireport is a visual development tool for making JasperReport's XML files.
Second, the template board first must download an Irport (address: http://ireport.sourceforge.net), the current highest version is 0.4.0, we use 0.2.2 here.
Decompose the IREPORT to edit the IREPORT.BAT file in the directory, as follows
@echo off
Set java_home = c: /j2sdk1.4.0_03
SET ANT_HOME = C: / Ant
SET IREPORT_HOME = C: / Documenti / Progetti / Ireport / IREPORT2 /
REM% ANT_HOME% / BIN / ANT JAVADOCS
% Ant_Home% / Bin / Ant Ireport
As long as you set some of the corresponding roads, you can use Ant to run, of course, if you don't install Ant, you don't want to install it, then you can find the Startup.bat file under the Noant folder to run.
When establishing a new template, you get the interface shown in the figure.
Select the menu item "DATATSOURCE", click on its submenu item "Connection / DataSource", will pop up an edit box, select the "New" button,
Enter the data source name, select JDBC Driver, when entering the JDBC URL, you can click on the "Wizard" button, which will configure your URL according to your JDBC Driver, but you have to modify the IP address and DatabaseName, enter the database name, username ,password. Click the "Test" button to determine the connection situation and save it.
The picture below is a report template I do:
$ F {} is used to indicate that this is a database field. $ F {windows_name}, $ f {number}, $ f {sp_name}, $ f {sp_name} and $ f {sp_type} are fields in the database, but also the field I want to use in the template. For $ f {number}, $ f {vname}, $ f {sp_name}, and $ f {sp_type}, although it can also be displayed in other domains, such as: PageHeader, Columnheader, ColumnFooter Domain Wait, but because there will be multiple columns to be displayed, you must be placed in the "Detail" domain.
After the template is drawn, now configure the query statement of the template, as well as the fields, variables, and parameters.
First enter the query statement, such as "Select * from v_baseinfo_statbanjiereport", click the "Read Fields" button, you will check all the field names of the table.
If you have a conditional query, then add a WHERE statement later, such as "SELECT * from v_baseinfo_statbanjiereport where dept_name = $ p {orgname} and end_sp_day = $ p {etime}", click the "SAVE Query to Report" button to save. Here, you should pay attention to it, you can write the parameters directly in the WHERE statement, like this "where dept_name = 'China' and end_sp_day = 2004-11-23 '"; but from the Java program or JSP page If the parameter is Chinese characters, you must do coding conversion or instead with English characters, numbers; there is still like date parameters "2004-11-23", JasperReports seems to be analyzed, if you change the SQL statement to "SELECT * from v_baseinfo_statbanjiereport where DEPT_NAME = '$ P {orgname}' and END_SP_DAY = '$ P {etime}' ", it will throw an exception, because the SQL statement will first Jasperreport arranged precompiled statement" select * from v_baseinfo_statbanjiereport where DEPT_NAME =? And end_sp_day =? ", When the parameter is used in the SQL statement, in addition to its specific" $ p {} "symbol, there is no other punctuation symbol, and the passing parameter value is not as puncture symbol, So the date format is changed to "20041123".
Set template field:
Set template parameters:
When all of which are finished, click the "Save" button to generate an XML file, then click the "Compile" button to generate a file that is a suffix JASPER.
You can now preview the effect of the template, first select the menu item "Build" submenu item "PDF Preview", "HTML Preview", "Java 2D Preview", "Excel Preview", "CSV Preview", "JRVIEWER Preview" One (the configuration of these displays in the menu item Tools / Options.), Click "Execute Report"; if you want to bring data preview, you have to activate the active data source. Select the submenu bar "Set Active Connection" under the menu bar "build", pop up a edit box, select a data source name, click "OK".
At this point we have to introduce a JSP page, assume that pdf.jsp, then the following is the PDF.JSP code:
<% @ page import = "DORI.JASPER.EENGINE. *"%>
<% @ 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 resulting 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 ();
%>
The program is to explain with you, "/ report / test.jasper" path is relative to your JSP page, such as your JSP page is saved in D: //../root / directory, then you You must build a report directory in this directory, then put your report template file in this directory.
Map parameters = new hashmap ();
Parameters.Put ("Orgname", "zhongguo");
Parameters.Put ("ETime", "20041123");
"Orgname" and "ETIME" must be consistent with the parameter name defined when template.
Third, the end of this whole template is completed, there is no explanation of white or wrong places, I hope you can discuss with me.
About the author Shu Feng, contact information: Feng.Shu@chinacreator.com, Introduction: I am currently a software engineer software engineer software engineer, a software center of Kechuang Computer System Integration Co., Ltd., Hunan Province, mainly studying J2EE Programming Technology.