26,26 €
JasperReports is the world's most popular embeddable Java open source reporting library, providing Java developers with the power to easily create rich print and web reports. While such reports are pivotal in managing business information more effectively, creating and customizing them can get tedious.
This book will give you recipes to solve common JasperReports problems to make your life easier. It will take you through complex examples related to JasperReports with step-by-step instructions on how to solve them.
The author's experience in creating reports enables him to share over 50 recipes to develop crystal-clear business reports using the capabilities of JasperReports and the amazing features provided by its visual report designer tool: iReport.
This cookbook will arm you to develop user-friendly reports by using multiple types of data in the same report, thereby saving your time in sorting, arranging, or extracting data and making the whole process effortless. First you will learn to do basic things such as creating, sizing, positioning, and enhancing the titles, headers, footers of the report. You will move on to working with the body and footer of the report, including using parameters to filter records during report processing and implementing nested hierarchy. Second, you will learn to enhance the look and feel of your report by deploying and reusing styles, and by using designs, textual effects, background images, and watermarks. You will also learn to use mathematical and logical expressions such as counting the number of records with a particular field value, grouping records, applying styles on your data based on logical or mathematical conditions, and so on.
Then you will be able to do advanced things such as working with a variety of data sources: relational data, XML data, model beans of Java applications, and also multiple relational databases at once. You will learn to build a cover page and table of contents for multi-page reports, display multi-dimensional page numbering, and much more. You will be able to design simple and multi-level summary reports, cross-tabs with dynamic rows and columns, create bar charts, and display data trend graphs in your reports. Lastly and very importantly, you will be able to generate reports from Java Swing applications after you learn how to use JavaWrappers for your JasperReport.
Create and enhance your business reports using the various functionalities of JasperReports and iReport
The entire book is a comprehensive collection of recipes meant to tutor you on making and enhancing reports. Each recipe provides you with easy step-by-step descriptions of the actions necessary to accomplish a specific task with explanations to help you to understand how and why each action was undertaken. Example values and code samples are used throughout the recipes, which makes adaptation for individual needs easy. Each recipe uses a concrete example for easy understanding.
This book is for Java developers who want to use JasperReports to create user-friendly business reports. It is for those who may be familiar with JasperReports but want to dive into advanced JasperReports activities.
If you know how to use Microsoft Word you can also learn how to design business reports using iReport by following the recipes of this cookbook.
Although the primary audience of this cookbook are report designers, however Java and XML developers will find this book useful in understanding how Jasper's XML code works and how to wrap JasperReports functionality in your Java Swing or Web applications.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 297
Veröffentlichungsjahr: 2010
Copyright © 2010 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: June 2010
Production Reference: 1220610
Published by Packt Publishing Ltd. 32 Lincoln Road Olton Birmingham, B27 6PA, UK.
ISBN 978-1-849510-76-9
www.packtpub.com
Cover Image by Vinayak Chittar (<[email protected]> )
Author
Bilal Siddiqui
Reviewers
Nadeem Ghafoor Chaudhry
Sagara Gunathunga
Lloyd H. Meinholz
Bhavani P. Polimetla
Acquisition Editor
Dilip Venkatesh
Development Editor
Mayuri Kokate
Technical Editors
Ishita Dhabalia
Vinodhan Nair
Indexer
Monica Ajmera Mehta
Editorial Team Leader
Gagandeep Singh
Project Team Leader
Priya Mukherji
Project Coordinator
Zainab Bagasrawala
Proofreader
Sandra Hopper
Graphics
Geetanjali Sawant
Production Coordinators
Aparna Bhagat
Arvindkumar Gupta
Cover Work
Aparna Bhagat
Bilal Siddiqui is an Electronics Engineer, an XML consultant, and the founder of XML4Java.com, a company focused on simplifying e-business. After graduating in 1995 with a degree in electronics engineering from the University of Engineering and Technology, Lahore, he began designing software solutions for industrial control systems. Later, he turned to XML and used his programming experience in C++ to build web- and WAP-based XML processing tools, server-side parsing solutions, and service applications. Bilal is a technology evangelist and a frequently published technical author.
Bilal has been focusing exclusively on Java and XML-based open source tools and solutions since 2006. He has extensively used popular open source products such as JasperReports, ADempiere, Openbravo, and Eclipse. Bilal is a strong advocate of open source tools and is engaged not only in designing solutions based on open source tools but also collaborating with local universities in Lahore to train software and IT personnel in using open source technologies.
Special thanks to my wife's patience, which was often put to the test while I was working on this book. I'm also thankful for the support of Rainer Maier—my friend and business partner, who was quite convinced that I should just work on this book and sleep instead of working on our mutual projects. Finally, I am also thankful to my colleagues, Tariq and Zia, for their ideas to make this book more interesting and useful.
Nadeem Ghafoor Chaudhry received his B.S. and M.S. in Computer Science from the University of Massachusetts, Lowell, U.S.A. He worked in the software industry in the U.S. for about four years. Then he switched to academia in Pakistan and taught both undergraduate and graduate courses at different institutes of higher learning in Pakistan. Currently, he is engaged as Assistant Professor in the department of Computer Science of COMSATS Institute of Information Technology, Lahore, Pakistan.
COMSATS Institute of Information Technology (CIIT) is a renowned institute for higher learning in Pakistan. Currently, the CIIT is offering 47 different degree programs divided into 20 undergraduate and 27 graduate programs in the fields of Information and Communication Technology, Management Sciences, Electrical Engineering, Chemical Engineering, Mathematics, Physics, Bio-sciences, Development Studies, Environmental Science, Meteorology, and Architecture and Design. The present student strength is around 20,000 with faculty corpus of more than 1,600. The CIIT has already produced approximately 10,000 graduates.
Sagara Gunathunga holds a special degree in Computer Science (B.Sc.) from the University of Peradeniya, Sri Lanka and a degree in Information Technology (BIT) from the University of Colombo, Sri Lanka. He is an Apache Axis PMC member, an Apache committer for several projects that include Apache Web Service and Apache Woden. His research interest focuses on SOA, web services, distributed systems, and modularity systems.
He also has a sound industry experience in J2EE-related technologies, including ORM tools, dependency injection frameworks, and reporting. Currently, he is playing a Tech-Lead role in a U.S.-based software services company named Aeturnum Inc.
Sagara also maintains his blog at http://ssagara.blogspot.com/, and you can get in touch with him at<[email protected]>.
All my thanks go to my mother for supporting me.
Lloyd H. Meinholz works as an IT professional in the Washington D.C. area. He has more than 20 years of experience in developing software in a variety of programming languages and production environments. Lloyd has focused on Java, Linux, and other open source technologies for the past 12 years. He has been employed by start-up companies as well as Fortune 500 companies and worked in both the public and private sectors. Lloyd has found JasperReports and iReport to be a great reporting solution for many of his employers.
Bhavani P. Polimetla has been learning and working in the IT Industry since 1990. He graduated as a Bachelor of Computer Science and Master of Computer Applications from Andhra University, India. He worked on standalone Swing applications to Grid computing and N-tire architecture. He has worked with the world's top clients including three from Fortune 50 companies. At present, he is working as an independent Java consultant in Atlanta, Georgia, U.S.A.
To demonstrate his skills, he completed 25+ certifications in the spectrum of J2EE, database, and project management subjects. He also achieved many awards for many of his projects. He reads and writes poetry in his free time. More information is available at his website www.polimetla.com.
JasperReports is the world's most popular embeddable Java open source reporting library, providing Java developers with the power to easily create rich print and web reports. While such reports are pivotal in managing business information more effectively, creating and customizing them can get tedious.
This book will give you recipes to solve common JasperReports problems to make your life easier. It will take you through complex examples related to JasperReports with step-by-step instructions on how to solve them.
The author's experience in creating reports enables him to share over 50 recipes to develop crystal-clear business reports using the capabilities of JasperReports and the amazing features provided by its visual report designer tool: iReport.
Create and enhance your business reports using the various functionalities of JasperReports and iReport.
Chapter 1, Creating Static and Dynamic Titles and Headers, focuses on basic things such as creating, sizing, positioning, and enhancing the titles and headers of the report.
Chapter 2, Working with the Body and Footer of your Report, covers working with the body and footer of the report, including using parameters to filter records during report processing and implementing nested hierarchy.
Chapter 3, Enhancing the Look and Feel of your Report, shows you how to enhance the look and feel of your report by deploying and reusing styles, and by using designs, textual effects, background images, and watermarks.
Chapter 4, Working with a Variety of Data Sources, focuses on advanced things such as working with a variety of data sources: relational data, XML data, model beans of Java applications, and also multiple relational databases at once.
Chapter 5, Multi-page Reports, teaches you to build a cover page and table of contents for multi-page reports, conditional reset page numbering, display multi-dimensional page numbering, design multi-page reports using sub-reports, create separate header and footer for each sub-report in your Master report, and much more.
Chapter 6, Multi-column Reports, covers topics such as dividing the body of a report in multiple columns, grouping records with common value in a report column, filling data in report columns horizontally and vertically, inserting column break in your report, controlling the size of each column independently, and using sub-reports to design multi-column reports.
Chapter 7, Summary Report, Crosstabs, and Graphs, covers designing simple and multi-level summary reports, cross-tabs with dynamic rows and columns, creating bar charts, embedding a bar inside a tabular view, and displaying data trend graphs in your reports.
Chapter 8, Java Wrappers for your JasperReports, covers generating a report from a Java Swing application, generating a report from your web application, printing a hard copy of a report on the fly from within your Java code, viewing your report as a PDF, or a web page, or an XLS sheet, and so on.
Chapter 9, Using Mathematical and Logical Expressions, covers mathematical and logical expressions such as counting the number of records with a particular field value, grouping records, applying styles to your data based on logical or mathematical conditions, and so on.
Chapter 9, Using Mathematical and Logical Expressions, is not present in the book but is available as a free download from: http://www.packtpub.com/sites/default/files/downloads/0769os-chapter-9.zip
The following software products are used in this JasperReports 3.6 Development Cookbook:
This book is for Java developers who want to use JasperReports to create user-friendly business reports. It is for those who may be familiar with JasperReports but want to dive into advanced JasperReports activities.
If you know how to use Microsoft Word, you can also learn how to design business reports using iReport by following the recipes of this cookbook.
Although the primary audiences of this cookbook are report designers, Java and XML developers will find this book useful in understanding how Jasper's XML code works and how to wrap JasperReports functionality in your Java Swing or web applications.
In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.
Code words in text are shown as follows: Open the Pattern.jrxml file from the Task5 folder in the source code for this chapter.
A block of code is set as follows:
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
Any command-line input or output is written as follows:
New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Select Currency from the Category column and click the OK button."
Warnings or important notes appear in a box like this.
Tips and tricks appear like this.
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.
To send us general feedback, simply send an email to<[email protected]>, and mention the book title via the subject of your message.
If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or e-mail<[email protected]>.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book on, see our author guide on www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
Downloading the example code for the book
You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration, and help us to improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the let us know link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata added to any list of existing errata. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.
Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or web site name immediately so that we can pursue a remedy.
Please contact us at<[email protected]> with a link to the suspected pirated material.
We appreciate your help in protecting our authors, and our ability to bring you valuable content.
You can contact us at<[email protected]> if you are having a problem with any aspect of the book, and we will do our best to address it.
In this chapter, you will learn:
This chapter is about the titles and headers of your reports.
Imagine you are looking for a particular recipe in a cookbook. Perhaps the quickest way of doing this is to go through the table of contents, which is just a list of the titles of all the recipes contained in this cookbook. So it is important that each title says what the recipe teaches.
Similarly, the title of your report will help identify the report among other reports. The title perhaps decides the objective of designing a report.
A title normally appears on just the first page of your report. The header appears on every page. While a title helps to identify the report among other reports, a header distinguishes the report among other copies of the same report. You can understand this with an example.
Consider you are designing a report showing the data for all invoices issued for a particular customer during a specific month. "Monthly Customer Invoices" can be an appropriate title for this report. Once you have designed the monthly customer invoices report, you will periodically generate copies of this report for a specific customer and for a specific month (that is, each copy of the report will be specific for these two parameters). Therefore, it is a good idea to include these two parameters in the header of your report.
This means that a report header represents the data on which a report is generated.
iReport is a powerful visual tool for JasperReports to graphically design reports. It is much easier to generate JASPER code using the graphical features of iReport as compared to writing the JASPER code by hand.
In order to create reports using JasperReports, you will only need to download iReport from the iReport project website hosted at SourceForge.net ( http://sourceforge.net/projects/ireport ).
As the JasperReports library comes bundled with iReport, you don't need to separately download and install JasperReports.
This recipe just shows you how to download, install, and run iReport.
The five steps given above show you how to download and install iReport for Microsoft Windows. iReport also supports other operating systems such as Linux, Mac OS X, and Solaris. The following is a short guideline to download and install iReport for these operating systems.
You can download iReport for Linux, Mac OS X, or Solaris by following the steps listed below:
Visit http://www.linuxhelp.net/newbies/#unzip to learn more about unzipping tar.gz files.
If you need to download an older version of iReport, click on the View all files button to the right of the Download Now! button on the iReport project home page. A page containing a list of available versions will open. You will find the latest downloads for all operating systems in the Newest Files section and older versions in the All Files section of the download page.
Note that I have used iReport version 3.6.0 in this cookbook. iReport versions dated before 2008 are referred to as iReport classic, which might behave differently from the recipes of this cookbook.
This recipe is a quick starter for someone who is using iReport for the first time. It shows you what views of your report the tool offers and what type of report components are available. This recipe also teaches you how to quickly make a simple "Hello World" report.
The following ten steps will show you how to make your first "Hello World" report.
JasperReports has defined its own XML-based markup language called JasperReports XML, or JRXML for short. JRXML code contains all the information that is required to specify everything about your report. Therefore, designing a report using JasperReports means you have to perform the following steps:
You normally need to write Java code to compile, fill, and export your JasperReport.
iReport is a JRXML authoring, compiling, filling, and exporting tool. This means that if you can author JRXML manually and write Java code to perform these tasks (compiling, filling, and exporting), you don't need iReport.
However, it is really troublesome trying to write JRXML manually. The graphical tools and components in iReport make it very easy and quick to author the JRXML according to the design requirements of your reports. iReport performs the following tasks for you to help you generate your report:
iReport provides three integrated views to make your work easier: the designer view (where you design your report graphically), the preview view (where you can see the effects of your design), and the XML view (where you can view and edit the actual XML of a JRXML file).
To see the XML view of iReport, switch to the XML tab and you will find the following JRXML code for the Blank A4 report:
You can see from this code that there is a JRXML tag for every section of your report. For example, there is a<title> tag which wraps the title of your report. Similarly, you can see a<pageHeader> tag, which wraps the header.
Each JRXML tag for a section has a<band> child, which specifies two things about the section: the height of the section through a height attribute and the splitting behavior of the section.
In steps 8, 9, and 10 of this recipe, you played a little with the Designer, XML, and Preview tabs, respectively. You will need these tabs in almost every report you prepare using iReport.
The Designer tab is perhaps where you will work most of the time. It shows a window in which you will design your report by using different components such as static text, text field, and so on. These components are available in a palette of components on the right of your main iReport window.
In step 7 of this recipe, you played with the palette of components to quickly have an idea of how components are used in your report. The Palette window is split into two sections named Report Elements and Tools. The former contains several graphical components (for example, Static Text, Text Field, Frame, and so on), whereas the latter contains tools such as Current date and Page number. You will learn how to use these tools in Chapter 2,Working with the Body and Footer of your Report.
A self-explanatory title for your report helps identify the report among other reports. A popular style of designing self-explanatory titles is to have a main title and a subtitle. The main title is normally brief and the subtitle is a more elaborate form of the main title.
In this recipe, you will learn how to provide a main title and a subtitle for your business report. We are going to create, size, position, and color the title.
There is no database connection required in this recipe.
If you have not made any database connection so far in your iReport installation, you will see an Empty datasource selected in a drop-down list just below the main iReport menu. This is the requirement to run this recipe. If any other option (for example, some database connection) is previously selected, then change it to Empty datasource from the drop-down list, as follows:
the following steps are very simple, and demonstrate how you can design a basic title for your report:
Switch to the XML tab and you will see the following JRXML code:
I have shown only the<title> tag in the code and omitted other JRXML tags that are not relevant to this discussion.
The<title>
