Using Jasperreport+ireport for Web report development

Source: Internet
Author: User

Web Report Development with Jasperreport+ireport

Preface in very many practical projects, the report is a very important part of it, such as the query results in the form of a report. The report mentioned here is not a simple two-dimensional table, but has a complex header, multidimensional, can be active in the execution period from the database to read data, self-active paging, rich page elements (pictures, hyperlinks, etc.), support groups and crosstab, support printing, preferably exported to Excel or Word ...... (KhanL)。 However, it is obvious that the more powerful the report, the more services it provides, the more complexity it has, so it is not enough to generate reports by hand in the Stone Age. Fortunately, several of the reporting tools we know today are powerful enough to have a very handy report generation tool. Each of them is: Jasperreport (+ireport), BIRT (+eclipse), Crystal Report (+eclipse,jbuiler, etc.). The three reporting tools are mentioned first because they are open source (CrystalReportForEclipse1.0 is already open source). Since we don't have to consider the cost, which one do we choose in our project? For Crystal Reports, although they are in the. NET platform is very eye-catching, but on the Java platform, most of the implementation is charged (for example, for JBuilder version), and its Eclipse plug-in resource consumption is very alarming (my machine is configured to P4 3.0+512ram, using "eclipse3.2+ Crystal Report Plug-in "will not run at all." So I chose the Pure Java Report Tool Jasperreport and ireport combination. However, about the Jasperreport document is relatively scarce, its official documents are still charged, I would like to use this article to show how to use this powerful combination of Web-based report development, I hope that those who suffer from the report of the colleagues to solve some practical problems. This article will focus on how to configure and use the Jasperreport report and report export function in the Web environment, because I have written in the past blog how to design ordinary reports, the here will not repeat. For those major operations are left to the reader to understand, I believe that with the help of ireport, get started very quickly.(Note: This article has been "program ape" included, without consent may not be reproduced)

1 jasperreport Brief introduction 2 Web Report Development 2.1 environment settings 2.2 Report preview Framework 2.3 using JNLP technology for client preview 3 Concluding remarks ... -

1 jasperreport Brief Introduction Jasperreport is a powerful, flexible report generation tool that can display rich page content and convert it to Pdf,html,xml,excel (implemented via POI or JEXCELAPI) and RTF (via POI) format. The library is written entirely in Java and can be used to generate dynamic content in a variety of Java applications, including J2ee,web applications. Its main purpose is to assist in the generation of page oriented, ready for the printed document. Jasperreport is organized by data that is defined in the XML document by the report design. This data may come from a different data source, including a relational database, an array of Collections,java objects. By implementing a simple interface, the user is able to insert the report library into a custom-made data source. The steps for report development with Jasperreport are as follows (version=1.0):Now jasperreport the latest version number is 1.2.7, able to download its entire project and code to the sourceforg site. The demo subfolder under its Project Files folder includes a very well-defined sample that enables a variety of functions. Given its documentation fees, we can only use these demos as a learning material if we want to learn about using Jasperreport. But the tedious XML markup and feature APIs also bring extra complexity at the same time to provide robust dynamic and extensible development, and it is extremely unwise to manually write the XML files needed for the report design without free documentation. Just as we write Swinggui with JBuilder (or other visual development tools), we are able to use ireport to visualize the report design to avoid dealing with scary XML files and implementation details. While it is possible to lose some of the flexibility of generating reports dynamically, in most cases we only need static design frameworks and dynamic loading data and very few dynamic reporting frameworks, so these small losses are negligible compared to the convenience we get. Of course, if you really need to, and see the following things you do not faint words, you can really get the necessary flexibility. The verticalfilling and horizontalfilling indicate the order in which the data is loaded. From what we can clearly see, a report design is mainly composed of PageHeader and report content, report content is composed of columns, content can be a column can also be multi-column, but also can be group. The detailed real ratio is as follows: What exactly are these elements defined in the XML design file of the Jaserreport? I don't want to care, because it's all ireport responsible, and we just need to make it easy to use ireport as a building block to add a variety of visual elements. I believe you will love ireport, just like me. As a practical necessity, I will provide you with a simple dynamic form generation framework for your reference. 2 Web Report Development Today's environment is the web's big line, a tool hypothesis can not be integrated into the Web functionality can not be based. Jasperreport's developers are clearly aware of this early on, so JasperReport1.0 has increased the ability to support servlet/jsp. In other words, we can use servlet/jsp to export generated reports into HTML (or Pdf/rtf/excel) format for previewing or exporting. The only drawback, however, is that Jasperreport does not provide the ability to print directly on the client, and we cannot directly display the Jrviewer preview form in client except using applets, how can we solve these problems? 2. 1 environment SettingsThe use of Jasperreport in servlet/jsp does not require many other settings, just the jar package that Jasperreport uses is placed under the Web-inf/lib folder in project. During the program execution period, servlet/jsp only need to be able to load the report file correctly, reload the data and generate the Jasperpring object, use the export framework given below to make a slight change to generate a html/pdf/rtf/ The Excel export feature, as well as the functional module that enables HTML previews to be paginated. 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= "*"%><%jasperprint jasperprint = (jasperprint) 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 (Jrhtmlexporterparameter.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 part of the code is used to export the Jasperreport object generated by the servlet into HTML format, exporting the servlet used for Jasperreport Imageservlet. It is important to note that I add the color part of the code, that is, you must put a Jasperprint object in the session variable, its keyword is "Imageservlet.default_jasper_print_session_attribute"So that Imageservlet can get it and export the report on its own initiative. <tr><td> &gt;&gt;<%=pagetitle%></td></tr></table> <table width= "98%" cellpadding= "0" cellspacing= "0" border= "0" height= "a" ><tr><td><div class= "menu" ><a href= "Pdfservlet" ></a></d Iv><div class= "menu" ><a href= "Rtfservlet" ></a></ Div><div class= "menu" ><a href= "Xlsservlet" ></a>< /div>        <div class= "menu" ><a href= "" >&nbsp;&nbsp;</a></div><div><%if (PageIndex > 0)          {%><a href= "This page? pageindex=0 "></a><a href= "This page?pageindex=<%=pageindex-1%> "></a><%          }Else          {%><%          } if (PageIndex < Lastpageindex)          {%><a href= "This page? Pageindex=<%=pageindex + 1%> "></a><a href= "This page?pageindex=<%=lastpageindex%> "></a><%          }Else          {%><%            }%></div></td></tr></table>This code will be exported as aHTMLthe report is paginated. <table width= "98%" cellpadding= "0" cellspacing= "0" border= "0" ><tr> <td width= "50%" >&nbsp;</ Td> <td align= "left" > <%=sbuffer.tostring ()%> </td> <td width= "50%" >&nbsp;</td> </tr></table></body>With this framework we can easily implement our own active paging function and export the report to the format we want: Pdf,word,excel, etc. Confined to space, here I can not show every detail and process of report development, but I have tried to extract the approximate process of web report development, and focus on the data source of the system, the design of the cross-table, and the Web preview framework These people believe that every Web report will encounter problems and solutions, In the case of Jasperreport's high-end use of documents, I hope my efforts will bring you a little help.

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: 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.