JAPERTREPORTS & IREPORT open source report actual combat

xiaoxiao2021-03-06  41

source:

http://blog.9cbs.net/calvinxiu/archive/2005/02/02/277737.aspx

JasperReports Ireports, the open source report system similar to CrystalReport, is most important to help us separate data and representation when designing, helping us to implement the page printing of web reports, packets, and defective and subtraction, and output formatting, There are exported Excel, PDF, etc.

Google has entry tutorials, which is very easy. But in a complex statement, the entry will not be turned quickly. If there is no intermediate study, it may have to go back to the HTML pictorial.

First, there is a JasperReport toll document on the Internet, but honestly say that this document is charged too thin.

1. Custom data source

1.1 Why do you have a simple data source for a data source, of course, you don't have to customize the data source. However, when the query is complicated, it will still be able to make SQL writing will make SQL and difficult to read, and it has never hate others on SQL. At this time, separate the query, each query is only responsible for assigning several columns to the VO (value object bean), and the program will be clear. Moreover, similar queries can also be further encapsulated in a Reporter class, providing reuses within a report or reporting. What's more, some cases are impossible to write in SQL, and put it in a Java program is relaxed. After getting the data source that can be decomposable, I really dare to use JasperReports on our system, otherwise, I can't imagine how much SQL is maintained.

1.2 Customized data source mechanism DORI.JASPER.EENGINE.JRDataSource interface is very simple, as long as two methods are implemented:

Public Boolean next () throws jrexception; public object getfieldValue (JRFIELD JRFIELD) THROWS JREXCEPTION;

Look at the function name knows its meaning. Fortunately, we don't have to achieve these two methods. Jaspert has two implementations about Vo Bean. They are dori.jasper.Engine.Data.jrbeanarrayDataSource and Dori.jasper.Engine.Data.jrbeancollectionDataSource

Look at the name, I can use Array to put bean, one with collection.

Unfortunately, if VO has embedded objects, you cannot read syntax to $ f {order.customer.name}.

Also Hibernate's HQL query, if multiple objects are returned, each ROW is Object [], can only press index without pressing the name, and does not support getFileldValue (Field), Hibernate.org has a trade-off method But it is not good.

1.3 Sample Code and Interpretation // Encapsulated a large number of query reporter class MoonCakeReporter MoonCakeReporter = new mooncakereporter (); // Establish VO's MAP, because you need to perform a query, you need to quickly retrieve VO to assign a value, so use map.map map = new HashMap (); rs = mooncakeReporter.findAllVaildProduct (); while (rs.next ()) {MooncakeVO 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 packaged query function to obtain data, and get VO to get VO by primary key goods_no. rs = mooncakeReporter.findSaleOrderByShopType ( "shop"); while (rs.next ()) {String goods_no = rs.getString ( "goods_no"); MooncakeVO mooncakeVO = (MooncakeVO) map.get (goods_no); if (mooncakeVO =! NULL) {mooncakevo.setshopcount (NEW ITEGER ("count"))))))))))))))))))))))))))))));}} // Some steps are repeatedly executed ..... // Because JrbeancollectionDataSource needs collection or array, convert MAP to LIST . List result = new arraylist (map.values ​​()); // Sort RESULT, implemented by beanutils's beancomparator, will be said in the next section. Collections.sort (result, new BeanComparator ( "code")); // finally result assigned JRBeanCollectionDataSourceJasperPrint jasperPrint = JasperFillManager.fillReport (jasperReport, parameters, new JRBeanCollectionDataSource (result));

1.4 Extension 1. Sort by beancomparator Use MAP to store VOs without order, must be sorted. With Jakata Commons-Beanutils's BeanComparator class, you can dynamically change the columns, which is better than your own Compare () interface.

Collections.Sort (Result, New BeanComparator ("CODE")); for "ORDER BY Lastname, Firstname", you can also write this, where 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 3. i i i 报 报 报 快 设计 设计

2. element, finally do not have a form line

Ireports finally came from 0.41, support JasperReports 0.64.

JasperReports 0.64 is so important? Because from 0.63, JasperReports supported the Box attributes from the thousand calls. You can set the four weeks of the table. And before, although everyone said the fairy , But I think of a hand-crossed line, but also accurately leave 1 pixel between the two Field, the scalp is numb.

In addition, the new BOX is also good under Excel, no longer need to make two different template files for HTML and Excel.

I don't understand anything, facing the full screen of the elements, the traction is aligned, and I can't use it.

2.1 Quickly organize, align the elements within the relevant table to demonstrate the faster way:

1. Click on the elements you want to align, the corresponding Report session in the left document structure list will open

2. Use the Shift key to select all the elements in the session

3.ctrl shift l, Ctrl Shift up arrow

It turns out that there is a messy element.

After adding or deleting an element in the report, use this method quickly reorganizes the page.

The first and second step is to select all the elements for fast batch, you can also select it with a mouse, or press the Shift key to select one.

Step 3 is to align the left alignment and upper alignment, and will be in line with the first elements.

2.2 Batch Modify Element Properties Like a lot of web editors, you can select multiple elements at the same time then select the properties batch modification.

2.3 IREPORT, UltraEdit Dual Double Tuning Reports Report Variables When the variable is more than the variable, use UltraEdit directly Copy Paste to increase FileD and Varible faster than Ireport. But use UltraEdit to pay attention to the following two questions:

The leftmost "

4. Export an Excel file

The Excel file that started to be exported was incomparably disappointed, and an Excel file that gave out should be at least: 1. All Field wants to stand the height of the BAND, do not leave a gap 2. All objects are selected transparent transparent 3. Setting parameters

Exporter.SetParameter.is_remove_empty_space_betWeen_Rows, Boolean.true; Exporter.SetParameter.is_white_porterparameter.is_white_page_background, boolean.false;

5. JasperReports Some supplements

5.1 Output GBK Character Set

Exporter.SetParameter (JrexporterParameter.Character_Encoding, "GBK");

5

.2 Define the Pattern displayed by FileD

For example, after the number of small numbers, financial count (one, numbers), etc., if the Pattern provided by Ireport is not cool, you can also directly change the XML file 5.3 Expresstion uses a three-purpose operator to achieve simple IF-ELSE to choose some cases such as The denominator cannot be determined by 0, and it can be done with a three-mean operator. Don't be able to work with VO. 5.4 Batch Compile Document Ireport's Plug-in bulk in batches of all files in all files in Jiangnan white

转载请注明原文地址:https://www.9cbs.com/read-60129.html

New Post(0)