Use JasperReport + iReport for Web report development

Source: Internet
Author: User

Use JasperReport + iReport for Web report development

In many practical projects, a report is a very important component. For example, the query result is presented in the form of a report. The report mentioned here is not a simple two‑dimensional table, instead, it has complex headers, multi-dimensional data that can be automatically read from the database at runtime, automatic paging, and rich page elements (images, hyper-connections, etc), supports grouping and cross tabulation, supports printing, it is best to export to Excel or Word ...... (Khan L ). However, the more powerful the report function is, the more services it provides, and the more complex it is. Therefore, manual report generation by stone age alone cannot meet the requirements. Fortunately, several well-known report tools are powerful enough and are provided with easy-to-use report generation tools. They are JasperReport (+ iReport), BIRT (+ eclipse), and crystal report (+ eclipse, JBuiler, etc ). These three reporting tools are all open-source (CrystalReportForEclipse1.0 is already open-source ). Since we don't need to consider the cost, which one should we choose in our project? For the crystal report, although it is in. net platform is very eye-catching, but on Java platform, most implementations are charged (such as For JBuilder ), in addition, the resource consumption of its Eclipse plug-in is amazing (my machine is configured with P4 3.0 + 512RAM, and the "Eclipse3.2 + crystal report plug-in" is not enough ). So I chose the combination of the pure Java report tool JasperReport and iReport. However, the document about JasperReport is relatively scarce, and the official document is still charged. I hope to use this article to show how to use this powerful combination for Web-based report development, I hope to solve some practical problems for my colleagues who suffer from reports. This article focuses on how to configure and use the JasperReport report and report export functions in the Web environment. Since I have written a previous Blog about how to design common reports, here we will not go into detail. These basic operations are left to the reader's own experience. I believe that with the help of iReport, you will get started quickly. (Note: This article has been included in "programmer" and cannot be reproduced without permission)


1 JasperReport Introduction 2 Web report development 2.1 environment setting 2.2 report preview framework 2.3 client preview using JNLP Technology 3 conclusion... 24


1 JasperReport IntroductionJasperReport is a powerful and flexible report generation tool that can display rich page content and convert it into PDF, HTML, XML, and Excel (implemented through POI or JExcelAPI) and the Rtf (implemented by POI) format. This library is fully written in Java and can be used to generate dynamic content in various Java applications, including J2EE and Web applications. Its main purpose is to generate page-oriented (page oriented) and prepare printed documents. JasperReport organizes data by defining the report design in the XML document. These data may come from different data sources, including relational databases, collections, and java object arrays. By implementing simple interfaces, you can insert the report library into the customized data source. The process of developing reports using JasperReport is as follows (Version = 1.0): the latest Version of JasperReport is 1.2.7. You can download the entire project and code from the Sourceforg website. The demo sub-directory in the project file directory contains many well-defined examples to implement various functions. In view of its documentation charges, we can only use these demos as learning materials if we want to learn how to use JasperReport. However, the tedious XML tag and function APIs provide powerful dynamic and scalable development while also bringing about ultra-high complexity. Without free documentation, it is unwise to manually write the XML file required for report design. However, just as we use JBuilder (or other visual development tools) to write SwingGUI, we can use iReport for visual report design to avoid dealing with terrible XML files and implementation details. Although it may lose some flexibility to dynamically generate reports, in most cases, we only need a static design framework and dynamic data loading, but seldom need a dynamic report framework, therefore, these small losses are negligible compared with the convenience we have obtained. Of course, if you really need and see the following things, you can gain the required flexibility by yourself. VerticalFilling and HorizontalFilling indicate the order of data loading. We can clearly see that the design of a report is mainly composed of PageHeader and report content. The report content is composed of columns, which can be either one column or multiple columns, it can also be a Group. The specific example is as follows: I don't want to care about the definition of these elements in the XML design file of JaserReport, because these elements are all taken care of by iReport, we only need to use iReport to add various visualization elements just like building blocks. I believe that you will never give up on iReport after using it, just like me. For actual needs, I will provide a simple dynamic form generation framework for your reference. 2. Web report developmentToday's environment is widely used in the Web field. A tool cannot be used if it cannot be integrated with Web functions. The JasperReport developer was aware of this early on, so he added support for Servlet/JSP before JasperReport1.0. That is to say, we can use Servlet/JSP to export the generated report to HTML (or PDF/RTF/EXCEL) format for preview or export. However, the only drawback is that JasperReport does not provide direct printing on the client. In addition to using the Applet, we cannot directly display a preview window like JRViewer on the client. How can we solve these problems? 2.1 Environment SettingsUsing JasperReport in Servlet/JSP requires no more settings, just put the jar package used by JasperReport under the WEB-INF/lib directory in the project. During the program running, Servlet/JSP only needs to be able to correctly load the report file, load data, and generate the JasperPring object, you can use the export framework provided below to generate a function module with HTML/PDF/RTF/EXCEL export function and pagination of HTML preview. 2.2 report preview framework<% @ Page contentType = "text/html; charset = UTF-8" %> <% @ page import = "javax. servlet. * "%> <% @ page import =" net. sf. jasperreports. engine. * "%> <% @ page import =" net. sf. jasperreports. engine. util. * "%> <% @ page import =" net. sf. jasperreports. engine. export. * "%> <% @ page import =" net. sf. jasperreports. j2ee. servlets. * "%> <% @ page import =" java. util. * "%> <% @ page import =" java. io. * "%> <% JasperPrint jasperPrint = (JasperPr Int) session. getAttribute ("JasperPrint"); session. setAttribute (ImageServlet. DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint); String pageTitle = (String) session. getAttribute ("pageTitle"); JRHtmlExporter exporter = new JRHtmlExporter (); int pageIndex = 0; int lastPageIndex = 0; if (jasperPrint. getPages ()! = Null) {lastPageIndex = jasperPrint. getPages (). size ()-1;} String pageStr = request. getParameter ("pageIndex"); try {if (pageStr! = Null) pageIndex = Integer. parseInt (pageStr);} catch (Exception e) {// e. printStackTrace ();} if (pageIndex <0) {pageIndex = 0;} if (pageIndex> lastPageIndex) {pageIndex = lastPageIndex;} StringBuffer sbuffer = new StringBuffer (); exporter. setParameter (JRExporterParameter. JASPER_PRINT, jasperPrint); exporter. setParameter (JRExporterParameter. OUTPUT_STRING_BUFFER, sbuffer); exporter. setParameter (J RHtmlExporterParameter. IMAGES_URI, "ImageServlet? Image = "); exporter. setParameter (JRExporterParameter. PAGE_INDEX, new Integer (pageIndex); exporter. setParameter (JRHtmlExporterParameter. HTML_HEADER, ""); exporter. setParameter (JRHtmlExporterParameter. BETWEEN_PAGES_HTML, ""); exporter. setParameter (JRHtmlExporterParameter. HTML_FOOTER, ""); try {exporter. exportReport ();} catch (Exception e) {e. printStackTrace () ;}%> this code is used to export the JasperReport object generated by the Servlet to HTM. L format. The exported Servlet is the ImageServlet that comes with JasperReport. Note that I have added the color part of the code, that is, you must put a JasperPrint object into the Session variable. The keyword is "ImageServlet. DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE ", so that ImageServlet can obtain and automatically export the report. <Html>

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.