Overview
This tutorial shows how to create a simple JasperReports® Book that uses the basic features in Jaspersoft® Studio. The sample book contains the orders for each country as a separate report and a table of contents that shows the page number where each report part begins. You do this in three main steps:
- Create a report book with a query that groups records by country.
- Create a report with the information we want with a parameterized query for country.
- Add the report to the Content (detail) section of the report book and set the parameter appropriately.
Introduction
A report book is a single .jrxml that bundles multiple reports into a single object. A report book has sections, similar to bands. The possible sections are: detail, group header, and group footer sections. Like bands, you can have multiple instances of a section. Inside each section, you can have one or more reports or report books. You cannot have other element types in a report book.
The individual reports inside a report book are called "report parts". They are similar to subreports, but each report part is included as a whole report, not embedded as data. This means each report part starts on a separate page and different report parts can have different page layouts.
A report book has a dataset that determines how many times each part is instantiated. For example a part placed on the detail section is printed once for each record from the dataset. Dummy groups (that is, groups that have constant group expressions) can be used for parts that should only be included once in the generated document.
The group header or group footer section can be used to create a table of contents that lists the titles of the report parts.
Creating a Report Book
As mentioned above, this example creates a report that contains the orders for each country as a separate report, along with a table of contents that shows the page number where each report part begins. The first step is to create a new report book using the New Report Wizard:
-
Right-click on a project in the project explorer and select New > Jasper Report or click
on the Jaspersoft Studio toolbar.
- In the New Report Wizard, select Report Books in the Categories pane on the left and choose one of the available templates. For this example, choose the Abstract Book template, which includes all the components for a book with a cover, table of contents, detail pages, and back cover. Then click Next.
-
Enter a name, Orders_Book, and location for the book, then click Next.
- Select a data adapter for the book. For this example, use the Sample DB data adapter to use the sample database shipped with Jaspersoft Studio. Then click Next.
-
Set up a query that groups orders by country. In the SQL query editor, enter the following query:
SELECT SHIPCOUNTRY FROM orders GROUP BY SHIPCOUNTRY
Then click Next. -
Leave the default selection on the sections to be created. As mentioned you want a cover, table of contents, and back cover for the final book sample. Click Finish.
NOTE: In this template, the Cover and Table of Contents section is a group header section, the Content section is a detail section, and the Backcover section is a group footer section.
The report book and report parts are created and the JasperBook editor opens showing the book structure.
Modifying the Report Parts In the Template
The template includes some preformatted report parts, which you can easily modify. To edit the book cover report part and change the title and subtitle:
-
Edit the title and subtitle to match your report.
The table of contents part is already correct and you can use it as it is. No need to change anything.
Creating a Report Part for the Detail Section
Next, create a new report part to generate the information for each country inside the content section. To do this:
-
Create a new report using a blank template, using the Sample DB and leave the sql query blank for the moment, click on finish;
-
Define a new String parameter for the report named country
-
Add this query:
select * from orders where shipcountry = $P{country} order by shipcity
This extracts all details for the orders for a specific country. Clidk on read Fields button -
To connect the link in the table of contents to a meaningful bookmark for each country, set an Anchor Name Expression for the text field in the Title band. Set the bookmark level to 1. Finally save the report7. In the Project Explorer, right-click CountryOrders.jrxml and select Build project. The resulting file, CountryOrders.jasper, appears in the Project Explorer.
Adding a Report Part to the Report Book
You have only a couple of things left to do in the (main report) book.
-
Just select the report compiled created in the previous section (for example, CountryOrders.jasper) in the project explorer and drop it in the Content section of the report book. A dialog pops up to let you configure the connection for this part.
Conclusions
All is set and you can run the report preview, using the Sample DB as default data adapter, of course.
Give it a try, exploring the book and navigating through the different sections from the table of contents, which contains numbered references to the pages with details for each country.
If you want to explore a more complex example, look at the JasperReports sample project, JasperReportsSamples. You can build this in Jaspersoft Studio via File > New > Project > JasperReports Samples. The report book sample in the book subfolder showcases a more detailed table of contents along with a chart in each report part for more appealing results.
References
Recommended Comments
There are no comments to display.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now