Sub Reports

Hi to all,

           I dont  know how to use sub reports in the main report.i want help from u who are having knowledge on sub reports.And i want how to pass parameter from main report data to the sub report.Please help me .

pnvswamy's picture
884
Joined: Oct 13 2008 - 5:15am
Last seen: 14 years 5 months ago

27 Answers:

subreports are used where you want to represent data that is child to the data in the parent report. You could show data that is a reference data which may not any relation. You can have as many levels of subreports. You can pass parameters to the subreport jrxml from the subreport parameter tab in the subreport element. The same parameter name is to be specified in the subreport jrxml in order that the value is passed sucessfully.
shrije's picture
2237
Joined: Apr 5 2008 - 8:59am
Last seen: 9 years 1 day ago

I understood the usage of subreports.I developed the main report and sub report using  IREPORT tool.In i report i am directly connecting the database.So both reports are working fine.

My problem came when i am using the application.

In the application i am using JRBeanCollectionDatasource for the Report data as a ArrayList of BeanClasses.

Could you please help me how to pass parameter to sub report from the main report in the application.

pnvswamy's picture
884
Joined: Oct 13 2008 - 5:15am
Last seen: 14 years 5 months ago

You can pass parameters to the subreport jrxml from the subreport parameter tab in the subreport element. The same parameter name is to be specified in the subreport jrxml in order that the value is passed sucessfully.

shrije's picture
2237
Joined: Apr 5 2008 - 8:59am
Last seen: 9 years 1 day ago

hi

I am generating jasper reports with out writing any query string in .jrxml files.I am using JRBeanCollectionDataSource.

I am facing problems in sub reports.

main report having the fields like empCode,Name only.

These fields are getting throguh EmpBeans as an arrylist to passing a parameter to JRBeanCollectionDataSource.

Based on empcode i am getting the employee projects done have to shown using sub reports.

These projects are under another ProjectBean.

How can i fill the sub report based on the empCode.Please help me.

If i use query strings in jrxml there is no problem in ireport.

pnvswamy's picture
884
Joined: Oct 13 2008 - 5:15am
Last seen: 14 years 5 months ago

Class MainReport{

List<EmpDetail>

List <ProjectDetail>

}

Class EmpDetail{

empCode,Name

}

Class ProjectDetail{

empCode, projectDetails

}

Your main class should look like this.

You have beancollection of MainReport only.

The mainReport will have two fields automatically created as List. Once you done that change the subreport datasource as JavaBeancollectionSource(NameofListField). This way you can achieve very complex reports. Your subreport just needs to have the fields from the respective beans. It is difficult to write all in message but i have written enough for your understanding.

 

 

shrije's picture
2237
Joined: Apr 5 2008 - 8:59am
Last seen: 9 years 1 day ago

In this employeeList is the arryList i am fiiling the main report.In that it is having EmpDetail bean class.For each it has empCode ,for ecah code i want Project details to fill as arrayList.In Main report i am passing parameter as $P{empCode}

where i have to check the condition and where i ahave to fill the sub report as arryList based on EmpCode.

 

Thank you,

My problem is for the main report i am using EmpDetail bean class

and for sub report i am using ProjectDetails bean class.

first i am filling the main report like this  :

---------------------------------------------------------------------new File(getServlet().getServletContext().getRealPath("reports/PaEmployeeDetailReport.jrxml"));null; new java.util.HashMap();//parameters.put("subReport",jasperReport1);

JRBeanCollectionDataSource ds =

JRBeanCollectionDataSource(employeeList);

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, ds );

new

-----------------------------------------

File fileSrc =

File destFile =

JasperDesign jasperDesign = JRXmlLoader.load(fileSrc);

JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);

 

java.util.Map parameters =

 

 

pnvswamy's picture
884
Joined: Oct 13 2008 - 5:15am
Last seen: 14 years 5 months ago

You might have to change the implementation slightly.

You dont have to populate sub report as arryList based on EmpCode. All the data will be populated right before you generate the report. So you have to organise your data in the following form.

Class EmpDetail {

String EmpCode

List ProjectDetailsClass

}

So your first main report JRBeanCollectionDataSource is JrBeanCollectionDataSource(List<EmpDetail>)

Rest of the population is just iReport Configuration of subreport. You subreport should have fields for ProjectDetailsClass.

Change the subreport element properties to following:-

At the subreport tab select "use DataSource Expression"

Here enter "new JrBeanCollectionDataSource(List<ProjectDetail>)" as the datasource expression.

I hope i am clear now.

 

 

shrije's picture
2237
Joined: Apr 5 2008 - 8:59am
Last seen: 9 years 1 day ago

I am filling the main report like this:
----------------------------------------
File fileSrc = new File(getServlet().getServletContext().getRealPath("reports/PaEmployeeDetailReport.jrxml"));
File destFile = null;
JasperDesign jasperDesign = JRXmlLoader.load(fileSrc);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
  
java.util.Map parameters = new java.util.HashMap();
  
/*employeeList is the EmpDetail List*/
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(employeeList);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, ds );
------------------------------------------------------------------------------
i want to fill sub report also
Please help me......

pnvswamy's picture
884
Joined: Oct 13 2008 - 5:15am
Last seen: 14 years 5 months ago

I am filling the main report like this:
----------------------------------------
File fileSrc = new File(getServlet().getServletContext().getRealPath("reports/PaEmployeeDetailReport.jrxml"));
File destFile = null;
JasperDesign jasperDesign = JRXmlLoader.load(fileSrc);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
  
java.util.Map parameters = new java.util.HashMap();
  
/*employeeList is the EmpDetail List*/
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(employeeList);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, ds );
------------------------------------------------------------------------------
i want to fill sub report also
Please help me......

 

Please see the files:

MainReport : PaEmployeeDetailReport.jrxml

sub report  :  Employee_subreport1.jrxml

Method in java class is :

generateReport()

pnvswamy's picture
884
Joined: Oct 13 2008 - 5:15am
Last seen: 14 years 5 months ago

generateMethod looks like this:new PaReportsHelper();"reports/Employee_subreport1.jrxml");"reports/Employee_subreport1.jasper");out.println("List Size:"+employeeList.size());if(employeeList.size() == 0 ){new ActionMessage("PA.PaEmpReportsByStatusAction.generateReport.noData");"noData", noData);return mapping.findForward("noRecords");try{

// JasperCompileManager.compileReportToFile(getServlet().getServletContext().getRealPath("reports/PaEmployeeDetailReport.jrxml"), getServlet().getServletContext().getRealPath("reports/PaEmployeeDetailReport.jasper"));

JasperDesign jasperDesign1 = JRXmlLoader.load(subFileSrc);

JasperReport jasperReport1 = JasperCompileManager.compileReport(jasperDesign1);

JasperCompileManager.compileReportToFile(subFileSrc,subFileDest );

List projects = reportsHelper.employeeProjectDetails(

 

File fileSrc =

File destFile =

JasperDesign jasperDesign = JRXmlLoader.load(fileSrc);

JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);

 

java.util.Map parameters =

 

parameters.put(

parameters.put(

JRBeanCollectionDataSource ds =

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, ds );

JRExporter jrExporter =

new Integer(5));new File(getServlet().getServletContext().getRealPath("reports/PaEmployeeDetailReport.jrxml"));null; new java.util.HashMap();"subReport",jasperReport1);"subds",new JRBeanCollectionDataSource(projects));new JRBeanCollectionDataSource(employeeList);null;

 

PaReportsHelper reportsHelper =

String subFileSrc =getServlet().getServletContext().getRealPath(

String subFileDest =getServlet().getServletContext().getRealPath(

List employeeList = reportsHelper.getEmployeeDetails123(department,employee,status,type,loginUnitId,subFileSrc,subFileDest);

System.

 

 

ActionMessage noData =

messages.add(

saveMessages(request, messages);

 

}

 

 

 

pnvswamy's picture
884
Joined: Oct 13 2008 - 5:15am
Last seen: 14 years 5 months ago

the generateReport() method looks like this{

PaReportsHelper reportsHelper =

new PaReportsHelper();"reports/Employee_subreport1.jrxml");"reports/Employee_subreport1.jasper");out.println("List Size:"+employeeList.size());if(employeeList.size() == 0 ){new ActionMessage("PA.PaEmpReportsByStatusAction.generateReport.noData");"noData", noData);return mapping.findForward("noRecords");try{

// JasperCompileManager.compileReportToFile(getServlet().getServletContext().getRealPath("reports/PaEmployeeDetailReport.jrxml"), getServlet().getServletContext().getRealPath("reports/PaEmployeeDetailReport.jasper"));

JasperDesign jasperDesign1 = JRXmlLoader.load(subFileSrc);

JasperReport jasperReport1 = JasperCompileManager.compileReport(jasperDesign1);

JasperCompileManager.compileReportToFile(subFileSrc,subFileDest );

List projects = reportsHelper.employeeProjectDetails(

 

File fileSrc =

File destFile =

JasperDesign jasperDesign = JRXmlLoader.load(fileSrc);

JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);

 

java.util.Map parameters =

 

parameters.put(

parameters.put(

JRBeanCollectionDataSource ds =

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, ds );

new Integer(5));new File(getServlet().getServletContext().getRealPath("reports/PaEmployeeDetailReport.jrxml"));null; new java.util.HashMap();"subReport",jasperReport1);"subds",new JRBeanCollectionDataSource(projects));new JRBeanCollectionDataSource(employeeList);

}

String subFileSrc =getServlet().getServletContext().getRealPath(

String subFileDest =getServlet().getServletContext().getRealPath(

List employeeList = reportsHelper.getEmployeeDetails123(department,employee,status,type,loginUnitId,subFileSrc,subFileDest);

System.

 

 

ActionMessage noData =

messages.add(

saveMessages(request, messages);

 

}

 

 

 

pnvswamy's picture
884
Joined: Oct 13 2008 - 5:15am
Last seen: 14 years 5 months ago

hi,

I am having the field type ArrayList in employee Bean.

I want to show that list in Report.

Ex : i am setting the projectDetails as a ArrayList in Bean.

And i configured the <field name="projectDetails" class="java.util.ArrayList"/>

then i am getting the exception like --->

Could you please help me how to represent arraylist type Data in .jrxml file.

Error retrieving field value from bean : projectDetails

pnvswamy's picture
884
Joined: Oct 13 2008 - 5:15am
Last seen: 14 years 5 months ago

Put it as java.util.List. Also change the subreport properties as i described earlier.

shrije's picture
2237
Joined: Apr 5 2008 - 8:59am
Last seen: 9 years 1 day ago

i am not using any sub report.Only single report .And i want the ArrayList data to be printed in the report.

i just put the filed like <filed name="projectDeatils" class="java.util.ArrayList"/>

i dont know how to print the ArrayList data.

 

pnvswamy's picture
884
Joined: Oct 13 2008 - 5:15am
Last seen: 14 years 5 months ago

You need to use subreport for this to work.

shrije's picture
2237
Joined: Apr 5 2008 - 8:59am
Last seen: 9 years 1 day ago

First of all Thanks for ur reply.

ok i am using sub report.If i given the <field name="projectDetails" class="java.util.ArrayList"/>

then it is giving the

 

Error retrieving field value from bean : projectDetails

pnvswamy's picture
884
Joined: Oct 13 2008 - 5:15am
Last seen: 14 years 5 months ago

Hi,

i given like this in .jrxml file

<

 

field name="projDetails" class="java.util.List"

/>

and in sub report part

<subreport>
 <reportElement x="9" y="665" width="502" height="20"/>
 <dataSourceExpression><![CDATA[new JRBeanCollectionDataSource($F{projDetails})]]></dataSourceExpression>
 <subreportExpression class="net.sf.jasperreports.engine.JasperReport"><![CDATA[$P{subReport}]]></subreportExpression>
    
</subreport>

then it was giving the error like

Errors were encountered when compiling report expressions class file:

1. JRBeanCollectionDataSource cannot be resolved or is not a type

value = (net.sf.jasperreports.engine.JRDataSource)(new JRBeanCollectionDataSource(((java.util.List)field_projDetails.getValue())));/*$JR_EXPR_ID=39$*/

<------------------------>

2. JRBeanCollectionDataSource cannot be resolved or is not a type

value = (net.sf.jasperreports.engine.JRDataSource)(new JRBeanCollectionDataSource(((java.util.List)field_projDetails.getOldValue())));/*$JR_EXPR_ID=39$*/

<------------------------>

3. JRBeanCollectionDataSource cannot be resolved or is not a type

value = (net.sf.jasperreports.engine.JRDataSource)(new JRBeanCollectionDataSource(((java.util.List)field_projDetails.getValue())));/*$JR_EXPR_ID=39$*/

<------------------------>

3 errors

 

Please help me 

pnvswamy's picture
884
Joined: Oct 13 2008 - 5:15am
Last seen: 14 years 5 months ago

Thank you for your help,and spending time for my problems.

Now i got the report.

Keep in touch.Very very Thanks to you.

pnvswamy's picture
884
Joined: Oct 13 2008 - 5:15am
Last seen: 14 years 5 months ago

one doubt i am having,Please rectify me.

What my doubt is In the main report , i am giving the width and height for the sub report.Is there any way to give width and height dynamically for the sub report depending on the Progects size.

 

pnvswamy's picture
884
Joined: Oct 13 2008 - 5:15am
Last seen: 14 years 5 months ago

Subreports resize accordingly. You dont have to worry. Good that the JRBeanCollectionDataSource worked. i believe u missed the package name.

shrije's picture
2237
Joined: Apr 5 2008 - 8:59am
Last seen: 9 years 1 day ago

Hi,

I am using IReport 3.0.1 and i want to represent my report in a Table format.How can i use could u please help me.In my IReport tool there is no Table field.Please help me.

pnvswamy's picture
884
Joined: Oct 13 2008 - 5:15am
Last seen: 14 years 5 months ago

And I have one more doubt.Is there any way to execute the group in jrxml file depending on the Parameter.

Eg: In java i have parameters like groupByEmp,GroupByDept like this

Depending on the Java Parameter i have to execute the Group of that parameter and remaining group will not execute.

Is it possible.

pnvswamy's picture
884
Joined: Oct 13 2008 - 5:15am
Last seen: 14 years 5 months ago

I am getting the

Exception like Message Org.xml.sax.SAXParseException: Element type "pen" must be declared.

Why it was happening.Please help me to resolve.

 

pnvswamy's picture
884
Joined: Oct 13 2008 - 5:15am
Last seen: 14 years 5 months ago

Hello,

I don't understand the solution gave to solve the problem :

Errors were encountered when compiling report expressions class file:

1. JRBeanCollectionDataSource cannot be resolved or is not a type

value = (net.sf.jasperreports.engine.JRDataSource)(new JRBeanCollectionDataSource(((java.util.List)field_projDetails.getValue())));/*$JR_EXPR_ID=39$*/

<------------------------>

2. JRBeanCollectionDataSource cannot be resolved or is not a type

value = (net.sf.jasperreports.engine.JRDataSource)(new JRBeanCollectionDataSource(((java.util.List)field_projDetails.getOldValue())));/*$JR_EXPR_ID=39$*/

<------------------------>

3. JRBeanCollectionDataSource cannot be resolved or is not a type

value = (net.sf.jasperreports.engine.JRDataSource)(new JRBeanCollectionDataSource(((java.util.List)field_projDetails.getValue())));/*$JR_EXPR_ID=39$*/

<------------------------>

 

I have the same problem on a subreport and I don't manage to solve it.

Could you help me please ?

 

 

xavixxxx's picture
Joined: Oct 12 2009 - 1:38am
Last seen: 13 years 5 months ago

xavixxxx
Wrote:

Errors were encountered when compiling report expressions class file:

1. JRBeanCollectionDataSource cannot be resolved or is not a type

Use the qualified class name, i.e. net.sf.jasperreports.engine.data.JRBeanCollectionDataSource.

HTH,

Lucian

lucianc's picture
72648
Joined: Jul 17 2006 - 1:10am
Last seen: 1 hour 39 min ago
 

i am also having some problem with subreport. i used netbeans for development tool and i execute a report which got a subreport from the application and it worked fine. The problem it when i pass a parameter for the report and no data is being fetched. Can anyone help me with this.

Thanx

echo74's picture
61
Joined: Feb 15 2010 - 3:19am
Last seen: 13 years 1 month ago

Another old thread that is incomplete, so I'll try to complete it.

The answer was almost given above, but a few details were not quite correct, which might explain why the original poster did not recognize the solution.

First, your main collection class should look similar to this:

Class EmpDetail {

String empCode;

String empName;

List ProjectDetails empDetails;

}

Notice the list/collection in your main class which will contain the source of data for the sub-report.

Then, in the main report where you define your subreport, set the Data Source Expression property to the following:

new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{empDetails})

The reference to field empDetails was key to understanding the solution and was not presented before.

e-sarge's picture
94
Joined: Jun 21 2010 - 11:42am
Last seen: 12 years 9 months ago
Feedback