I spent a few days writing JasperReport and hope to help my colleagues who are getting started with JasperReport.
First, you need to understand what is report. In other words, data is displayed in a certain format. Here 2 has two important aspects: Data and presentation.
The data is nothing more than the data in the database and the data processed by the original data. The data is displayed in a certain format.
For a person who is new to JasperReport, I am afraid I don't understand the concept at first. OK. I will focus on the two aspects mentioned above.
We are most concerned about how to create a JasperReport report.
The steps are generally as follows:
Step 1: Create a template that represents data. This is a model. You only need to install the data in it.
How to Create a template? You can use iReport. The latest version supports the latest version of JasperReport 1.3.3 (as of the date of writing this article ). in this way, you only need to drag the element, such as StaticText and TextField.
Note: This template file ends with. jrxml and is opened in Notepad. It is nothing more than an xml file.
Step 2: Compile the template created in step 1, that is, the jrxml file. This will be compiled into a jasper file, which is a binary file.
Q: Why compile it?
In fact, it is not necessary. You can also compile it during the runtime. If you compile it at the runtime and 100 users call the same report, will the system compile the report 100 times? In this way, the efficiency is extremely low. if you compile it directly, it will save the 100 times of time. therefore, compilation is better. From another perspective, the report template is rarely changed.
The problem arises again. How can we compile it? The simplest way is to use iReport for compilation, which is a bit similar to IDE. of course, there are other methods to compile, that is, the API provided by JasperReport to compile. However, this requires code writing and so on, which is a little troublesome. However, after we are familiar with JasperReport, you cannot use it again. at the beginning, the simpler the better.
Step 3: fill in the Data. Let's say it's fill report.
This step may have been involved in step 2. but for the sake of system explanation, move it here. jasperReport has multiple data filling methods, but the most common two are: first, embed SQL statements directly in the jrxml file, but create a data source of your own, remember that this data source is inherited from JRDataSource. JRDataSource has a method that must be implemented, namely, next and getFieldValue. For details, see its API
Step 4: display the data
JasperReport can obviously store data as PDF, xls, html, etc.
Maybe the most common thing is to display data directly in a browser. How can this problem be solved? JRHtmlExporter is simple. set the following parameters and then export them. html data is exported. you only need to display the html data on the page. for more information, see the api documentation of JRHtmlExporter.
You may also want to export the report to pdf, xls, and doc files. It is very simple to use the serverlet class provided by jasperReport. net. sf. jasperreports. j2ee. servlets. servlet connector for PDF, net. sf. jasperreports. j2ee. servlets. xlsServlet for xls, net. sf. jasperreports. j2ee. servlets. rtfServlet is used for rtf.
If you are not satisfied with the built-in JasperReport, write it yourself.
The above are some basic steps. Next I will write an article to introduce the structure and composition of the template.