How to add subreport in jasper report using iReport - BOOKC -->

How to add subreport in jasper report using iReport

Tuesday, November 28, 2017

In this tutorial I will show you how to add sub reports in jasper report using iReport. Before adding subreport in our main report we need to discuss about subreport that what they are.

What is Subreport

A subreport is a report within our main report. Basically a subreport is nothing but a simple report which we want to show in our main report.

Why we use subreport

A subreprot is used when we have more then one set of data and we want to display that data or information in a single report. That's why we need to create such kind of report. Or we can say that to display multiple data on multiple details band we need subreports.

Steps to create Sub Reports

1- First of all create your main report
To create main report follow my previous article How to create a report in iReport. Before creating your main report you should need to know which type of data should be included in your report. In my case in my parent report or main report I want to show faculty name and the qualification details of that faculty should be comes from subreport. For that purpose I have created my main report named TestB.jrxml. In this report template I have three bands the first one is for report title and the other two is detail band. In detail band 1 I want to show name of the faculty and in detail band 2 I want to show qualification detail of that faculty and this detail or data comes from subreport.

2- Create your subreport template.Creating a subreport is not a rocket science, its just a simple report as we have created in step number 1.

3- Add parameter to the subreport.
To connect our subreport to the main report we need to create parameter in our subreport. The purpose of creating parameter in subreport is that subreport report query will receive the guide from the main report via this parameter which we are going to create in this step. And we will link our main report to subreport via this parameter.

To create parameter go to Report Inspector tab and right click on Parameter tree and choose Add parameter.

Now go to properties of your created parameter and set name and type (parameter class) of parameter.

4- Now define query for your subreport.
Now that we have created our main report, subreport and parameter for subreport, Its time to create report query for subreport which we have created in step number 2. To fetch data from that query we use parameter in where clause to limit items in our subreport. To create query for that subreport open report query window add your query and in where clause use parameter which we have created in step number 3. In this process we are going to tell subreport that fetch only those data that are linked to our main report. Now click on OK button.

5- Add subreport to you main report
Now that our main report and subreport have been ready. Now its time to link both of the reports. Go to your main report and drag Subreport in your desired detail band of your main report. If you are unable to view Palette window than press Ctrl+Shif+8. After draging subreport you will see subreport wizard from that wizard select "use an existing report" and browse your subreport which we have created in step number 2 and click on Next button.

Now select "Use the same connection used to fill the master report" and click on Next button.

Now select "Store the directory name in a parameter" and click on Finish button.

Now you have successfully add subreport in your main report, adjust its width and height according to your need.

You are now ready to run your main report. Running (i.e. previewing) the main report will automatically run the subreport(s) as well, and the data from the main as well as from the subreport(s) will be included in the report.