使用XML数据源使用jasper报表获取空白子报表

时间:2018-11-28 05:17:13

标签: xml jasper-reports subreport

我正在尝试使用xml数据源在主报表中显示子报表,但是我得到了空白的子报表。

我尝试在子报告的报告元素中添加isRemoveLineWhenBlank =“ true”,但仍然无法显示子报告。

如何解决这个问题。

这是我的DemoMajorReport.jrxml

     <?xml version="1.0" encoding="UTF-8"?>
<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="demoMajorReport" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="166fe611-df51-441b-a4b3-c830a3e38170">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA["C:\\Users\\Avinash Mahadeva\\Documents\\"]]></defaultValueExpression>
    </parameter>
    <queryString language="xPath">
        <![CDATA[/catalog/book]]>
    </queryString>
    <field name="Booking_Id" class="java.lang.String">
        <fieldDescription><![CDATA[Booking_Id]]></fieldDescription>
    </field>
    <field name="Car_Model" class="java.lang.String">
        <fieldDescription><![CDATA[Car_Model]]></fieldDescription>
    </field>
    <field name="Start_Date" class="java.lang.String">
        <fieldDescription><![CDATA[Start_Date]]></fieldDescription>
    </field>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch">
            <staticText>
                <reportElement x="197" y="42" width="100" height="20" uuid="6e286d17-497e-48ed-bb26-865010354dbb"/>
                <textElement>
                    <font size="12" isBold="true"/>
                </textElement>
                <text><![CDATA[Major Report]]></text>
            </staticText>
        </band>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="23" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="185" height="23" uuid="d70c3e82-79be-4402-b3ff-30b21972898d"/>
                <text><![CDATA[Booking_Id]]></text>
            </staticText>
            <staticText>
                <reportElement x="185" y="0" width="185" height="23" uuid="88794d2c-4ecc-4db3-a305-0be2f03e1952"/>
                <text><![CDATA[Car_Model]]></text>
            </staticText>
            <staticText>
                <reportElement x="370" y="0" width="185" height="23" uuid="2882bb1c-15e2-4532-b778-98868b30cb27"/>
                <text><![CDATA[Start_Date]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="26" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="185" height="26" uuid="0bd87bfc-8589-457b-aa65-5f7335aaf534"/>
                <textFieldExpression><![CDATA[$F{Booking_Id}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="185" y="0" width="185" height="26" uuid="bce91095-8a3d-484c-ba7f-bdae9ff5d9dc"/>
                <textFieldExpression><![CDATA[$F{Car_Model}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="370" y="0" width="185" height="26" uuid="0c32d03f-025a-4164-8b8f-28d0c024fe67"/>
                <textFieldExpression><![CDATA[$F{Start_Date}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="160" splitType="Stretch">
            <subreport>
                <reportElement x="20" y="21" width="513" height="100" uuid="58b75c1b-012a-46a0-b983-f4842cb37512"/>
                <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "demosub.jasper"]]></subreportExpression>
            </subreport>
        </band>
    </summary>
</jasperReport>

这是我的demosub.jrxml

<?xml version="1.0" encoding="UTF-8"?>
<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="demodub" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="a545d58f-357d-4eda-b9c1-3abed8902f6a">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <queryString language="xPath">
        <![CDATA[/catalog/book]]>
    </queryString>
    <field name="User" class="java.lang.String">
        <fieldDescription><![CDATA[User]]></fieldDescription>
    </field>
    <field name="Mobile" class="java.lang.String">
        <fieldDescription><![CDATA[Mobile]]></fieldDescription>
    </field>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch">
            <staticText>
                <reportElement x="234" y="35" width="100" height="20" uuid="b0d5a0c2-2049-4270-8ea4-85b19ee00a43"/>
                <textElement>
                    <font size="14" isBold="true"/>
                </textElement>
                <text><![CDATA[Sub report]]></text>
            </staticText>
        </band>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="61" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="277" height="20" uuid="7c5b3912-6208-4173-a538-abff1b7ed41b"/>
                <text><![CDATA[User]]></text>
            </staticText>
            <staticText>
                <reportElement x="277" y="0" width="277" height="20" uuid="7be408f4-1c94-41bc-b781-ba897a4ee603"/>
                <text><![CDATA[Mobile]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="125" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="277" height="20" uuid="6379a05e-a3cb-499b-95fc-27da08f139fe"/>
                <textFieldExpression><![CDATA[$F{User}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="277" y="0" width="277" height="20" uuid="3eef5b2a-64fb-4b75-a829-979e142f1cdb"/>
                <textFieldExpression><![CDATA[$F{Mobile}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>

这是我的sample.xml

<?xml version="1.0"?>
<catalog>
   <book >
      <Booking_Id>1</Booking_Id>
      <Car_Model>Acura</Car_Model>
      <Start_Date>15/11/2018</Start_Date>
      <End_Date>28/11/2018</End_Date>
      <User>User</User>
      <Mobile>9916452185</Mobile>
      <Email>roger@gmail.com</Email>
      <Tariff_Rs_day>3000</Tariff_Rs_day>
   </book>
 <book >
      <Booking_Id>2</Booking_Id>
      <Car_Model>BMW</Car_Model>
      <Start_Date>21/11/2018</Start_Date>
      <End_Date>27/11/2018</End_Date>
      <User>Andy</User>
      <Mobile>5514184532</Mobile>
      <Email>Andy@ymail.com</Email>
      <Tariff_Rs_day>5000</Tariff_Rs_day>
   </book>
</catalog>

1 个答案:

答案 0 :(得分:0)

您在主报表中的子报表声明需要一个数据源表达式,如下所示:

<subreport>
    <reportElement x="20" y="21" width="513" height="100" uuid="58b75c1b-012a-46a0-b983-f4842cb37512"/>
    <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).dataSource("/catalog/book")]]></dataSourceExpression>
    <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "demosub.jasper"]]></subreportExpression>
</subreport>

$P{REPORT_CONNECTION}仅适用于JDBC数据源。