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 .
27 Answers:
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.
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.
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.
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 );
-----------------------------------------
File fileSrc =
File destFile =
JasperDesign jasperDesign = JRXmlLoader.load(fileSrc);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
java.util.Map parameters =
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.
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......
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()
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 =
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);
}
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 );
}
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);
}
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
Hi,
i given like this in .jrxml file
< />
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
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.
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 ?
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
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.