Using Jasperreport+ireport for Web report development

Source: Internet
Author: User

Using Jasperreport+ireport for Web report development

The preface is a very important part of the actual project, and the result is presented in the form of the 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 (picture. hyperlinks, etc.), support for grouping and cross-tables, support printing, and best export 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 Reports (+eclipse,jbuiler, etc.). The three reporting tools are mentioned first because they are open source (CrystalReportForEclipse1.0 is already open source). Since the cost is not to be considered. 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, there is a relative lack of documentation about Jasperreport, and its official documents are still charged. I want 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's own understanding, believe in the help of ireport. It will be very quick to get started. (Note: This article has been "program ape" included, without consent may not be reproduced )


1 jasperreport Brief introduction 2

Instancename=ctl00_contentplaceholder1_entryeditor1_richtexteditor_richtexteditor&toolbar=default#_ Toc153974710 ">       Web Report Development 2

Instancename=ctl00_contentplaceholder1_entryeditor1_richtexteditor_richtexteditor&toolbar=default#_ Toc153974711 ">.1        environment settings 2.2       Report preview framework 2

Instancename=ctl00_contentplaceholder1_entryeditor1_richtexteditor_richtexteditor&toolbar=default#_ Toc153974713 ">.3 using JNLP technology to achieve client preview 3 Conclusion ... -


1 jasperreport Brief Introduction Jasperreport is a powerful and flexible report generation tool that can showcase rich page content. and convert it into a PDF. Html,xml,excel (implemented by POI or JEXCELAPI) and RTF (implemented by POI). The library is written entirely in Java. can be used to generate dynamic content in a variety of Java applications, including J2ee,web applications. Its main purpose is to assist in generating page oriented. Prepare the document for printing. 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. Users will be 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. Be 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 there may be some loss of flexibility in generating reports dynamically. But most of the 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, the assumption is indeed required. And if you don't faint when you see the following things, you can really get the flexibility you need.

The verticalfilling and horizontalfilling indicate the order in which the data is loaded.

From the way we can see clearly. A report design is mainly composed of PageHeader and report content, report content is composed of columns, content can be a column can be more than columns, but also can be group. The detailed real proportions are as follows: What exactly is the definition of these elements in the XML design file of Jaserreport? I don't want to care, because this is ireport responsible for worry. We just need to be able to use ireport to add a variety of visual elements just as easily as building blocks. 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, a tool assumes that it cannot be integrated into the Web functionality can not be based.

Jasperreport's developers are clearly aware of this early on. So in JasperReport1.0 once 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= "java.io.*"%><%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. Pay special attention to the code that I added the color section to. Be sure to 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= "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, and so on. Due to space limitations, here I can't show every detail of the report and process development, but I've tried the web to extract the statements from the development process and have built a system of key data sources. Cross-table design, and the Web preview framework believe that all of these web people's reports will encounter problems and solutions that are relatively scarce in the case of Jasperreport high file usage, and I hope that my efforts will help you a little.

Using Jasperreport+ireport for Web report development

Related Article

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.