如何将参数从主报表传递到子报表?

How to pass a parameter from main report to sub report?

我想将我的参数值从主报表传递到子报表。

我试过很多方法。我也尝试过 link 中的选项 passing-parameters-from-main-report-to-subreport-to-subreport-in-jasper

主报告

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.2.0.final using JasperReports Library version 6.2.0  -->
<!-- 2016-01-03T00:04:36 -->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="parademo" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="4949f116-97b5-453c-8581-61ccb1b3159e">
    <parameter name="TITLEPARA" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA["hi title"]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch">
            <textField>
                <reportElement x="180" y="36" width="220" height="30" uuid="4ac55979-2248-45d2-a001-3c82b5fec56b"/>
                <textFieldExpression><![CDATA[$P{TITLEPARA}]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="61" splitType="Stretch"/>
    </columnHeader>
    <detail>
        <band height="286" splitType="Stretch">
            <subreport runToBottom="false">
                <reportElement x="0" y="86" width="560" height="74" uuid="eca8556b-2d65-462f-a3d3-ac95ea6e3ebf"/>
                <subreportParameter name="TITLEPARA">
                    <subreportParameterExpression><![CDATA[$P{TITLEPARA}]]></subreportParameterExpression>
                </subreportParameter>
                <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                <subreportExpression><![CDATA["file:/home/rectrix/workspace/jasper/subparam.jrxml"]]></subreportExpression>
            </subreport>
        </band>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>

子报告

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.2.0.final using JasperReports Library version 6.2.0  -->
<!-- 2016-01-03T00:04:17 -->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="subparam" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="b4c019e3-0dc2-425c-b449-908c4fd21bde">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="MainReportDataSet"/>
    <parameter name="TITLEPARA" class="java.lang.String" isForPrompting="false"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch">
            <textField>
                <reportElement x="140" y="45" width="100" height="30" uuid="6b9d5038-4163-47ed-a0cd-812f07b27ff0"/>
                <textFieldExpression><![CDATA[$P{TITLEPARA}]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="61" splitType="Stretch"/>
    </columnHeader>
    <detail>
        <band height="125" splitType="Stretch"/>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>

我可以使用主报表 title 带中的参数打印 "HI TITLE"。

我想用参数把这个值传给子报表,打印在子报表的title带上

我只想使用参数传递这个值,因为我想知道参数是如何工作的,所以我可以将它用于我的项目,通过参数将值传递给子报表。

正确传递了参数,这不是您的问题。

使用您的示例文件时遇到这些问题。

  1. 如果数据源为空,您需要 jasperReport 标记上的属性 whenNoDataType="AllSectionsNoDetail" 来显示某些内容(在主报表和子报表中)。

  2. 子报表不能在 detail 带中(因为你的数据源是空的),将它移到另一个带示例 title 带中。

  3. 您不能在 subreportExpression 中引用 jrxml,您需要引用 .jasper 文件(jrxml 当它被遵守时)。注意:路径需要是文件的绝对路径(你通常为此使用一个参数,以便你可以在运行时传递它)

有关子报表路径的详细信息,请参阅此内容

为了测试,我建议您在子报表中放置另一个静态文本元素,以便您了解子报表是否显示。