Search code examples
javajasper-reportsireport

iReport: preview report with subreport informed the parameter


How to preview in iReport to get a report that has a subreport, and this is sent in subreport parameter?

Example Subreport in JRXML:

<subreportExpression class="java.io.InputStream"><![CDATA[$P{SUBREPORT}]]>/subreportExpression>

Example parameter in java:

InputStream subreport = new ByteArrayInputStream(reportInBytes);
parameters.put("SUBREPORT", subreport);

Solution

  • Any error message, stack trace or something?

    subreportExpression tag is the relative path or full path to where the subreport is located. Something like this is valid

    <subreportExpression  class="java.lang.String"><![CDATA["..\jasper\subReportInvoice.jasper"]]></subreportExpression>
    

    or

    <subreportExpression  class="java.lang.String"><![CDATA["home\josh\jasper\subReportInvoice.jasper"]]></subreportExpression>
    

    When you want to pass arguments or parameters to your subreport it has to be first declared in the main report (that is, the report calling the subreport) just before the queryString tag.

    <!-- This is declared before my queryString tag tag -->
    <parameter name="AGENCY_ID" isForPrompting="true" class="java.lang.Long"/>
    <parameter name="TREATPROC_SUBTYPE_ID" isForPrompting="true" class="java.lang.Long"/>
    <parameter name="PRODUCT_TYPE_CATEGORY_ID" isForPrompting="true" class="java.lang.Long"/>
    
    <!-- This goes into my subreport tag -->
    <subreport  isUsingCache="true">
                    <reportElement
                        mode="Opaque"
                        x="0"
                        y="20"
                        width="900"
                        height="20"
                        forecolor="#000000"
                        backcolor="#FFFFFF"
                        key="subreport-2"
                        stretchType="NoStretch"
                        positionType="FixRelativeToTop"
                        isPrintRepeatedValues="true"
                        isRemoveLineWhenBlank="false"
                        isPrintInFirstWholeBand="false"
                        isPrintWhenDetailOverflows="false"/>
                    <subreportParameter  name="AGENCY_ID">
                        <subreportParameterExpression><![CDATA[$P{AGENCY_ID}]]></subreportParameterExpression>
                    </subreportParameter>
                    <subreportParameter  name="PRODUCT_TYPE_CATEGORY_ID">
                        <subreportParameterExpression><![CDATA[$P{PRODUCT_TYPE_CATEGORY_ID}]]></subreportParameterExpression>
                    </subreportParameter>
                    <subreportParameter  name="TREATPROC_SUBTYPE_ID">
                        <subreportParameterExpression><![CDATA[$P{TREATPROC_SUBTYPE_ID}]]></subreportParameterExpression>
                    </subreportParameter>
                    <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                    <subreportExpression  class="java.lang.String"><![CDATA["..\jasper\subReportInvoice.jasper"]]></subreportExpression>
                </subreport>
    

    All the parameters required by your subreport query must be passed using subreportParameter and subreportParameterExpression tags as shown in my example.

    I hope this helps.