Practice of open-source reports of japertreports & ireport

Source: Internet
Author: User
Tags ultraedit
Jasperreports + ireports, similar to the open-source Report System of crystalreport, mainly helps us to separate data and representation during design, and help us implement paging printing and grouping of web reports, variable addition and subtraction, output formatting, and export of Excel, PDF, etc. People who have used it will not go back to redraw their own HTML pages to generate reports. There are getting started tutorials on Google, which is very easy. However, when you encounter complicated reports, you will not be able to get started quickly. If there is no intermediate research, you may have to go back and use HTML to draw reports.
First, there is a jasperreport billing document online, but to be honest, this document is too thin to charge. 1. Custom Data Sources. Input VO arrays instead of querying SQL directly in jasperreports.

1.1 why Custom Data sources
Simple reports, of course, do not use custom data sources. You can simply write SQL statements in jasperreports.
However, when the query is complicated, it will become long and difficult to read after writing an SQL statement. It has never been the most annoying for others to show off their SQL skills. At this time, data is collected in a Java program outside jasperreport (similar queries can be further encapsulated into a data collection class), and the results are passed into jasperreports through a custom data source, jasperreport is only responsible for data display. It is more in line with the universal mode of data and display separation.
After the data source that can be decomposed and reused is completed, I dare to use jasperreports on the system. Otherwise, you cannot imagine how to maintain so many SQL statements in the jasperreport file.

1.2 Custom Data Source Mechanism
The Dori. Jasper. Engine. jrdatasource interface is very simple. You only need to implement two methods:

public boolean next() throws JRException;
public Object getFieldValue(JRField jrField) throws JRException;
The function name makes sense. Fortunately, we don't even need to implement these two methods. jaspert already has two default implementations of VO bean. They are Dori. Jasper. Engine. Data. jrbeanarraydatasource and Dori. Jasper. Engine. Data. jrbeancollectiondatasource. It can be seen from the name that an array is used for bean and a collection. Unfortunately, if VO has embedded objects, it cannot be read using the syntax $ f {order. Customer. name.
Nor does it support hql query results of hibernate. Because each row returned by the query is object [], it can only be set by index instead of by name as resultset, and getfileldvalue (field) is not supported) function. There are compromise methods on hibernate.org, but none of them are good.

1.3 sample code and Explanation

// Encapsulate the reporter class for a large number of queries
Mooncakereporter = new mooncakereporter ();
// Create a map of VO. Because you need to execute the query multiple times, you need to quickly retrieve VO to assign values. Therefore, map is used.
Map map = new hashmap ();
Rs = mooncakereporter. findallvaildproduct ();
While (Rs. Next ())
{
Mooncakevo = new mooncakevo ();
String goods_no = Rs. getstring ("goods_no ");
String goods_name = Rs. getstring ("name ");
Mooncakevo. setname (goods_name );
Mooncakevo. setcode (CODE );

Map. Put (goods_no, mooncakevo );
}

// Call the reporter encapsulated query function to obtain the data and obtain the VO value through the primary key goods_no.
Rs = mooncakereporter. findsaleorderbyshoptype ("Shop ");
While (Rs. Next ())
{
String goods_no = Rs. getstring ("goods_no ");
Mooncakevo = (mooncakevo) map. Get (goods_no );
If (mooncakevo! = NULL)
{
Mooncakevo. setshopcount (New INTEGER (Rs. getint ("count ")));
}
}

// Repeated query steps
.....

// Because jrbeancollectiondatasource needs collection or array, map is converted to list.
List result = new arraylist (Map. Values ());

// Sort the results by beanutils's beancomparator, which will be discussed in the next section.
Collections. Sort (result, new beancomparator ("Code "));

// Finally, assign the result to jrbeancollectiondatasource.
Jasperprint = jasperfillmanager. fillreport (jasperreport, parameters,
New jrbeancollectiondatasource (result ));

1.4 extensions
1. Sort with beancomparator
The VO stored by map has no sequence and must be sorted. Using the beancomparator class of jakata commons-beanutils, You can dynamically change the columns to be sorted, which is better than implementing the compare () interface.

  Collections.sort(result, new BeanComparator("code"));

For the case of "order by lastname, firstname", you can also write it as follows. comparatorchain belongs to the jakata commons-collections package.

     ArrayList sortFields = new ArrayList();
     sortFields.add(new BeanComparator("lastName"));
     sortFields.add(new BeanComparator("firstName"));
     ComparatorChain multiSort = new ComparatorChain(sortFields);
     Collections.sort(rows,multiSort);

2. The <Box> element does not need to be manually formatted.

Ireports is available in version 0.41, supporting jasperreports 0.64.

Why is it so important? Since jasperreports supports box attributes that start with 0.63, you can set whether the four sides of the table are wired like other business report tools. In the past, although everyone said that the gods are good, but when you think of the need to manually draw table lines, but also accurate in the two fields between one pixel, the scalp is a burst of trouble.

In addition, the new box also performs well in Excel, and there is no need to create two different template files for HTML and excel.

3. Quick Design of ireport reports

At first, I couldn't understand anything. I dragged the elements on the full screen for a long time and aligned them. I almost couldn't use them anymore.
3. 1. quickly sort and align the elements in the report.
The quickest method is demonstrated here:
1. Click any alignment element. The corresponding session will be opened in the document structure list on the left.
2. Use shift to select all elements in the session
3. CTRL + Shift + L, CTRL + Shift + up arrow

The original disorganized elements were all arranged at once.
After adding or deleting elements in a report, you can use this method to quickly realign the page.
Step 1 and 2 are to quickly select all elements in batches. You can also use the mouse to select elements or press shift to select one element.
Steps 2 are left-aligned and top-aligned respectively, which are aligned with the first selected element.

3. 2. Modify element attributes in batches
Like many web editors, you can select multiple elements at the same time and click attributes to modify them in batches.

3. 3 ireport and ultraedit
When there are many variables, using ultraedit to copy paste directly is much faster than using ireport to add filed and Varible.

4. Export an Excel file

The Excel file exported at the beginning is incredibly disappointing. A hand-made Excel file should at least:
1. All fields must be filled with the height of the band. Do not leave gaps.
2. Select transparent for all objects
3. Set Parameters

     exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,
                    Boolean.TRUE);
     exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,
                    Boolean.FALSE);

5. Some Supplements to jasperreports

5. 1Output GBK character set

exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "GBK");

5. 2 define the pattern displayed by filed
For example, take the last two digits of a decimal number and calculate the financial statement (one number for every three digits). If the pattern provided by ireport is uncomfortable, you can directly modify the XML file.

5. 3 expresstion use the three-object operator to implement simple if-else Selection
In some cases, for example, if the denominator cannot be 0, you can use the three-object operator. It is not necessary to bother VO for computation.

5. 4. compile files in batches
Ireport's plug-in contains commands for compiling all files in the directory in batches.
Jiangnan Baiyi

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.