javajasper-reportssubreport

Jasper Report with multiple Subreport


I'm new with Jasper Reports, and I'm creating a Java program that will export reports to a .pdf file. This report has multiple subreports and was created in JasperSoft studio.

This is one of the subreport-parts in the main report:

<subreport>
    <reportElement positionType="Float" x="18" y="1" width="522" height="65" uuid="f3063ed3-775f-47df-8306-a939aecc9d89"/>
    <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
    <subreportExpression><![CDATA["ClaimHistory.jasper"]]></subreportExpression>
</subreport>

This is another subreport:

<subreport>
    <reportElement positionType="Float" x="18" y="2" width="522" height="65" uuid="a70d75e0-b9d0-4f52-bb84-090993c2e90b"/>
    <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
    <subreportExpression><![CDATA["ClaimDetail.jasper"]]></subreportExpression>
</subreport>

Each subreport uses JRBeanCollectionDataSource(list) to get its data and below is the Java code doing all the work:

String jasperFile = "E:/jasper/jasperFile.jrxml";
FileInputStream inputStream = new FileInputStream(jasperFile);
JasperReport jasperReport = JasperCompileManager.compileReport(inputStream);
List<JasperPrint> jasperPrintList = new ArrayList<JasperPrint>();
JasperPrint print = JasperFillManager.fillReport(jasperReport, params, new JREmptyDataSource());
jasperPrintList.add(print);
JRPdfExporter exporter = new JRPdfExporter();
exporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrintList)); //Set as export input my list with JasperPrint s
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput("E:/output.pdf")); //or any other out streaam
exporter.exportReport();

My current skills don't allow me to properly inject any data into the subreports using JRBeanCollectionDataSource(...).
How can I achieve injecting data into the subreports?


Solution

  • firstly defined the sub report's data sources are as fields in the main report (main.jrxml)

    <field name="claimHistoryDataSource" class="java.util.ArrayList">
        <fieldDescription><![CDATA[claimHistoryList}]]></fieldDescription>
    </field>
    
    <field name="claimDetailDataSource" class="java.util.ArrayList">
        <fieldDescription><![CDATA[claimDetailList}]]></fieldDescription>
    </field>
    

    claimHistoryList and claimDetailList are ArrayLists defined in the java class which used as a data source of the main report.

    then pass it to the sub report with a dataSourceExpression

    <subreport>
        <reportElement positionType="Float" x="18" y="1" width="522" height="65" uuid="f3063ed3-775f-47df-8306-a939aecc9d89"/>
        <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{claimHistoryDataSource})]]></dataSourceExpression>
        <subreportExpression><![CDATA["ClaimHistory.jasper"]]></subreportExpression>
    </subreport>
    
    <subreport>
        <reportElement positionType="Float" x="18" y="1" width="522" height="65" uuid="f3063ed3-775f-47df-8306-a939aecc9d89"/>
        <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{claimDetailDataSource})]]></dataSourceExpression>
        <subreportExpression><![CDATA["ClaimDetail.jasper"]]></subreportExpression>
    </subreport>