Tool used: ireport
What is subreport?
Before going into details, let me tell you what is subreport and what is the relation between a main report and subreport. A subreport is a report which is used inside another report. This functionality helps us during complex designs where the data comes either from different portions of a single document or from different datasources.
Make data easy with Helical Insight. Helical Insight is world's best open source business intelligence tool.
Click Here to Free Download
In some cases, there are some values which needs to be used both on the main report and the subreport. The values can be either the parameters used to call the report, variables or can be the fields generated from the query. In those cases we need to pass those values as parameters to the subreport which in turn can be used in the subreport. The main things that we need to take care of are the datatype and the naming convention.
How to pass values to subreport?
The following are the steps:
- First thing is to create the subreport and link the subreport with the main report. For example: Let the name of the main report be Main.jrxml and the subreport be Subrpt.jrxml.
You now have to link the subreport element that you just added to the main report, to your subreport file (the one you created in step 1) In order to do that, select the subreport element in the main report, and modify its parameters as follows:
- Subreport Expression: “repo:/public/MainReport/Subrpt.jrxml” à Subreport path
- Expression Class: java.lang.String
- Connection Type: Use a connection expression
Connection Expression: $P{REPORT_CONNECTION}
2. Now the link has been established between the main report and the subreport. We will get these parameters by passing them from the main report to the subreport. The rule is,for every field(be it either a variable/a query field/ a parameter) we want to pass to a subreport, a similar parameter (with another name) has to exist in the subreport, so that we can make the mapping. So we have to create a parameter on the subreport side which will have the same name as the name of the field/variable on the main report and the datatype should be the same also. For example: If the name of main report variable is X with datatype java.lang.Integer then the subreport should contain the same parameter of X with datatype java.lang.Integer. This is how Jasper understands and therefore allows the data to flow into the subreport.
Make data easy with Helical Insight. Helical Insight is world's best open source business intelligence tool.
Lets Start Free Trail
3. Go to the main report and click on the subreport band. In the properties section on the right hand side corner of the report, go to parameters option(2nd last option in ireport). Click on the expression tab and a pop up window named Subreport Parameters will open. See below screenshot for better understanding.
4.While passing the parameter to the report, the Name section is the section where the name of the parameter present in the subreport is specified and in the expression section is the value you want to pass from the main report. In the above screenshot, I am just passing the parameter of the main report as my passing parameter to the subreport.
Note: It is advisable to keep the name of the parameter in subreport and the parameter’s name in the main report same. However if you want to change the name of the subreport parameter then we have to first mention over here and then mention the same name in the subreport. But we can’t change the data type of the parameter unless we are changing the type in the connection expression.
Hence, this is how we pass values(parameters/variables/fields) of main report as parameters to the subreport. Now we use that parameter anywhere in the subreport.
Thanks, Nitish
Best Open Source Business Intelligence Software Helical Insight is Here
Could you help me how to pass fields from main report to subreport. If I do it as a parameter, only pass one value, not all values.
As example in main report I have product name, and there should be 3 products, but in the subreport only display the last product name.