Youcef Laidani Youcef Laidani - 1 year ago 130
Java Question

How to send datasource to subreport?

I work at a report that need a subreport, so I want to pass datasource from a Java method to subreport, but I don't arrive at the correct way. Usually I use the followin piece of code to create my report. What is the correct way to send datasource to subreport?

Map<String, Object> param = new HashMap<>();
param.put("rapportNom", "Module");
param.put("PAR_IMAGE_BACKGROUND", "/dz/suivi/reporting/sources/BackgroundElit.png");
param.put("PAR_IMAGE_HEADER", "/dz/suivi/reporting/sources/headerElit.png");
param.put("SUBREPORT_DIR", "/dz/suivi/reporting/sources/moduleAllDetail_subreport1.jasper");

return Reporting.printEtat(getClass().getResourceAsStream("/dz/suivi/reporting/sources/moduleAllDetail.jasper"),
param, new JRBeanCollectionDataSource(reportingModuleViewFacade.findById(module)));

Answer Source

If you have your datasource as a List<MyBean> as simple method is:

  1. Pass it in the parameter map

    param.put("SUBREPORT_DATA", myBeanList);
  2. Define the parameter in jrxml

    <parameter name="SUBREPORT_DATA" class="java.util.List"/>
  3. Create a JRBeanCollectionDataSource using your parameter

        <reportElement x="0" y="0" width="200" height="100" uuid="406c9014-e74b-42d9-b9e1-4af106bfb779"/>
        <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "theSubreport.jasper"]]></subreportExpression>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download