Ireport and JasperReport Integrated Development Web Report Wizard
I am in touch with it a week.
Ireport
with
JasperReport
Downloading initially they did not attract my interest (or trust), mainly their version of the question (I have never dare to use the version.
1.0
Tool software, current version:
Ireport0.2.2, JasperReport0.5.0
), But I heard that someone is using them, so I look at it carefully (because
Java
Application
Web
I have been printing in printing, very painful, so I would like to be credible, huh, huh ...)
A week, I tried some demand points about the report on the project, I found that they can solve it, really happy, so I recommend them to you today.
In fact, we are just JasperReport, Ireport is a visual development tool for JasperReport (do not look at its interface, we have to do it to help us get easy.), Of course Ireport has other use, but here Not elaborate.
There is almost no Chinese introduction to the introduction of their integration development, although I don't prepare for very detailed introduction, I only write some experience, there is no shortage to taste, if you want to study them, please also ask them. Read them in English documents.
JasperReport's supporting document is very detailed. This document is charged, but there is a circulating electronic version on the Internet. I am fortunate to get a copy. If you need to get it to my site (http://plateau.sicool.com), thank you JasperReport's contribution to the open source world.
On the contrary, IREPORT has nothing more. Perhaps the author thinks it is simple enough J, I am also an experienced experience in countless exploration.
Ok, let us enter the journey of the above three to integrate reports ...
1. First you have to get the latest version, you can download the address below
Irport: http://ireport.sourceforge.net
JasperReport: http://jasperreports.sourceforge.net
Their configuration is also very simple:
Ireport is extracted into the Ireport.bat file in the edit directory after the next, as follows
@echo off
Set java_home = c: / jdk14 set ant_home = d: / ant set ireport_home = C: / Documenti / Progetti / Ireport / IREPORT2 /
REM% ANT_HOME% / bin / ant javadocs% Ant_Home% / BIN / ANT IREPORT
Is it very simple, you only need to set some of the corresponding roads, here is Ant to run, of course, if you don't have an Ant, you don't want to install it, then you can, but here I bought an official, you can go Find the corresponding operating method in the directory after decompression. Oh, install ANT J
JasperReport does not require any configuration, you only need to put the JAR package after downloading to ClassPath. Oh, it is simple, but if there is no Ireport to do "dirty" for us, what will be the pain of the report?!
2. How is JasperReport work?
Here I will say it simply.
First, there is an XML file first, then compile into a Jasper file (end of .jasper), we really want to use .jasper files after compilation, this is much like we are familiar with .jsp file, in fact, we call it compilation Subsequent servlet. (Image from http://ireport.sourceforge.net/cap3.html# 3.1)
However, the static report does not mean, so there is a saying:
"A DataSource a jasper = a print"
It can support the output format as follows: PDF, HTML, XML, XLS, CVS, etc.
Therefore, JasperReport's original input is a .xml document, imagine that there is a simple report to the XML file, which is there, how painful before Ireport is used. (I am incredible, I have used JasperReport to do reports with JasperReport, but they have not used Ireport, day!)
Now don't be afraid, use Ireport to easily get this XML, write here, I think of struts_config.xml, and there is a lot of editors about it, huh, huh J
If you want to do a report, if you want to do a report, you have a long time to familiarize yourself with JasperReport's document, a use of familiar documents is to write that XML file, now you are familiar with the time you are familiar with IREPORT.
3. Ireport
Let's take a look at this (Rich and Very Simple to User GUI):
If you have had experience in developing reports in VB or other development tools, this interface should be very familiar, if you happen, you are familiar with Java language, wow, then you should use Ireport to be easy, huh, J
In the previous document, we initially introduced Ireport and JasperReport, basically we have an initial understanding of them. In today's document, I will explain in depth to IREPORT tools.
Then the end of the previous document, if we successfully run Ireport, you can see the following main interface:
Click "New"
The button can create a new empty report, considering the introduction of the generated XML file editing, you can display Chinese, please GB2312 on the XML Encoding option (without the option yourself), as shown below:
When setting the option to play the necessary options, you can get a blank report, as shown below:
Ok, enter a piece of text, very simple, click to create a "static text" button
, Then enter any workspace (such as Title, Pageheader, Detail, Pagefooter Summary, etc.), if you enter "Today's weather is good!" You can control the input content attribute, such as font, color, size And other properties, any elements in the report (static text, lines, boxes, etc.) have their own property pages, which generally double-click any element to pop up the property page of the element, the property page as shown below:
Finished, a simplest statement is generated, Ireport is equipped with an operational environment, as shown in the following shortcut:
The button on the left is running a static report (no database environment), the right is the running dynamic report (which needed to set up a database connection, the database is described below) Then through the efforts, it is clear that we will run the static report, So click on the button on the left, you can see the following PDF, as shown:
Of course, the example is the simplest, huh, hure.
Below I will use the question and answer method to gradually explain the usage of IREPORT. How to connect to the database?
2. How to define variables, parameters, and fields?
3. How to define fonts and how to use custom fonts (such as black body, body or comparative artized sub)?
4. How do I perform SQL scripts to query?
5. How to introduce a good statement into the app?
Ok, for the above problem, I give a solution in the following paramount and intersect other function menus in Ireport. 1. How to connect to the database?
The static report is actually very small. I mentioned a classic words in the previous document: "A Datasource a Jasper = a print" there is therefore necessary to first introduce how to connect the database, in fact, simply enter DataSource / Connections / DataSources menu or click
The shortcut button appears, the following property page appears: make the necessary database connection properties configuration, then click the Test button to test, if the prompt is successful, save the database connection, you can.
2. How to define variables, parameters, and fields?
The definitions of variables, parameters, and fields are more convenient in Ireport, and some control points can be seen under menu view, as shown below:
Report Properties Settings Report Properties Report Query Settings Query statements, this is described below with the Report Fields Settings Field (Database) Report Variables Settings Variable Report Parameters Settings Parameters Report Fonts Setting Font By the way, the following you also explain the BANDS, pop-up report work Zone (such as title detail, etc.) property page Report Groups Setup Group
Element Properties element property page Element Browser displays a list of all elements of the report
The setting is easy, but these variables are quoted in the report, the parameters and fields are written, huh, huh J, let's take a step step by step. The ways referenced in IREPORT, as follows
Element Name Reference Variable VariablesName $ V {VariableSname} Parameters File FileDsName $ f {fileDsName} In fact, these do not indicate that I don't know how to do, one is not described, and they are also very easy to use. Use more
3. How to define fonts and how to use custom fonts (such as black body, body or comparative artized sub)?
Because taken into account the use of Asian fonts, you need to under additional jar package ITextasian.jar, if this JAR package is included in the Ireport download package, this step can be avoided.
Currently in Ireport uses Chinese fonts, it seems to have only one of the Song, if you want to use other fonts (such as black body, body, etc.) need additional methods, here, here, here, how to use common Chinese font "Song" settings Method, in question 2 we see the introduction of the font option, click "Report Fonts" to see the following property page: Good, if the settings above have set a good Song, after future reports, you can reference the font anywhere in the report. As we have to put our elements in the examples of us "Good today!" Changed to the Song, as follows:
This way as we choose Songti in the Report Font option to automatically inherit, huh, it is convenient to say how to use Chinese fonts other than the Song, such as: black body, body or other artistic font.
It is also very simple, but it seems that Ireport is not perfect, but also manually modify the XML file to meet the requirements, as shown below, we define the black body:
First, the External TTF Font ... is selected in the PDF Font Name option, according to the common sense, at this time, select the black body TTF file Simhei.ttf, but I don't know why TrueType font is empty, (at the time When I am studying, this step will make me better than enough.), God, what should I do? !
Later I got the corresponding XML file, as follows: (Due to the big file, only we care about us)
Everyone can see the black body pdffontname = "" is empty, so I tried to write the TTF file path of the system black body (c: /winnt/fonts/simhei.tf) to this place, haha, actually succeeded. So I got such a conclusion, so I now use other fonts in the report, first do the property settings you made in the above figure, then manually write the TTF file of the font to pdffontname = "", although it is trouble, but This tricks, huh, huh, let us first review the problems already elaborated in the previous document:
1. How to connect to the database?
2. How to define variables, parameters, and fields?
3. How to define fonts and how to use custom fonts (such as black body, body or comparative artized sub)?
Below we will explain the two problems under the rest, and I have added two places that should be elaborated by the previous document before the start of the previous document.
The first is that I forgot to describe how Ireport compiles XML files into a Jasper file, which is very simple, click "Compile" button
That is, in fact, you click two "run"
"The default operation is to compile the XML file.
The second is how to set the output format of IREPORT. We have said that IREPORT can be output in many formats, such as PDF, HTML, XML, XLS, CVS, etc., how do we set it to output? ?
In the menu build, we can see that IREPORT's default output format is PDF, as shown in the figure:
PDF Preview Default Output HTML Preview In the format of the page, it will not be said, and everyone will understand, J
However, only the output format is set here, there is no setup, and there is a required setting is to select the "environment" for each setting, for example, if you choose to output with a PDF, then you need to indicate Acrobat Reader The path, as shown in the figure, the menu Tools / Options, will pop up the following property page:
There is still something that I want to explain here. The settings of External Editor are the editor of the EXTERNAL EDITOR. Here I usually use UltraEdit to edit, you may like to use other, such as EditPlus, then use common The XML editor is entered here to edit the XML file as you want, edit the XML file into the menu "Edit / Edit XML Source", will pop up your familiar editor to edit the XML file, as shown:
There is also the last point to add, in the previous document, the PDFFontName of the font of the non-Synchron is absolutely path (such as: c: /winnt/fonts/simhei.ttf), in fact, this is wrong , You should pack it in the font in the app, so that the absolute path is the relative path (such as ReportFonts / Simhei.ttf), so there is no loss, huh J J
Ok, do the complement of the upper side, let us follow the problems in front, first listed the following questions to the following:
4. How do I perform SQL scripts to query?
5. How to introduce a good statement into the app?
Let's solve it by one of us a problem.
4. How do I perform SQL scripts to query?
Everyone knows that from the data query must have a query statement, when the specifically inquiry may be more complicated, how does IREPORT make us use their own query statement to output? We have mentioned in the previous one, huh, hun, I hope you will not forget, there is a reprot query item under the menu view, click, click the following property page: Click on "Save Query to Report" on the image. This SQL statement will introduce this query statement to the report. If you view the XML file at this time, you will find the following piece:
At this time, we started with the above articles, let us "processing", as shown below:
Suppose we have set up the connection data, then click the "Run" button.
The following effects will be obtained:
Oh, I got the effect of our expectations. At this time, you may ask if I want to use the parameters in the SQL statement, then what? In fact, I will immediately say this question. If we want to make a fuzzy query with a name, the parameter passed is Name, then the expected SQL statement may be the following:
Select * from users where name limited '% name% "SELECT *
In fact, it can be easily realized in Ireport, remember that we have said the use of parameters in the previous document, so I don't have much to say, I only write the final SQL statement to everyone, I believe that everyone will understand:
Select * from users where name limited '% $ p {name}%'
Is not it simple? J
5. How to introduce a good statement into the app?
In fact, in Question 4, if you want to run a query report with parameters, you will involve problems 5, so I still want to quote the problem of 4 problems here.
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.Engine. *"%> <% @ page import = "java.util. *"%> <% @ page import = "java.io. *"%> <% @ Page Import = "java.sql. *"%>
<% File reportfile = new file (Application.getRealPath ("/ Reports / Test.jasper"));
Map parameters = new hashmap (); parameters.put ("name", "p"); connection conn = null // below ignore 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 ();%> There is also a necessary step in putting the jasperreport.jar you download under your application's lib.
Ok, start your app, run dbf.jsp, you will see the following effects
How, according to the fuzzy query result is in line with our expectations, I believe that you have already mastered the method of the upper side.
Of course, the examples I have given are the easiest, and the typography of the report is also more casual. I believe a little, beautiful reports can be inactivated, huh, huh J, practice more.
to sum up
This series of documents finally finished writing, always feel a bit random, but I hope to give a little help, there are many places that have some shallow tastes, I hope you don't understand or think that I will leave a message or write. letter.
My site: http://plateau.sicool.com