为什么只显示我的第一个子报表?

时间:2019-08-19 13:15:59

标签: jasper-reports subreport

我试图在一个Jasper报告中显示多个表,并且为此功能使用子报告。但是我一直坚持显示几个子报告。实际上,仅显示其中一个(第一个)。

这是我的主要 report.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="multitable" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="1c6dc1c9-67af-43d5-bf1e-9243557c8048">
<property name="net.sf.jasperreports.json.source" value="C:/jdd/projects/StackTrace/jasper/multitable.json"/>
    <parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
    <defaultValueExpression><![CDATA["reports/hardware/"]]></defaultValueExpression></parameter>
<parameter name="title" class="java.lang.String"/>
<parameter name="period" class="java.lang.String"/>
<parameter name="DS1" class="net.sf.jasperreports.engine.data.JsonDataSource"/>
<parameter name="DS2" class="net.sf.jasperreports.engine.data.JsonDataSource"/>
<queryString language="json">
    <![CDATA[]]>
</queryString>
<detail>
    <band height="120" splitType="Stretch">
        <textField>
            <reportElement x="0" y="0" width="100" height="50" uuid="91249d6e-57f7-4b6d-8d2a-05059efa2a47"/>
            <textElement textAlignment="Center" verticalAlignment="Middle">
                <font size="16"/>
            </textElement>
            <textFieldExpression><![CDATA[$P{title}]]></textFieldExpression>
        </textField>
        <textField>
            <reportElement x="652" y="0" width="100" height="50" uuid="67e1ca07-588a-4b2b-ae93-ecbdb544b01b"/>
            <textElement textAlignment="Center" verticalAlignment="Middle">
                <font size="16"/>
            </textElement>
            <textFieldExpression><![CDATA[$P{period}]]></textFieldExpression>
        </textField>
        <subreport>
            <reportElement x="0" y="50" width="555" height="20" uuid="48a7bbe4-b8ce-4a0d-a6e1-6ddd288e5602"/>
            <dataSourceExpression><![CDATA[$P{DS1}]]></dataSourceExpression>
            <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "printersSubreport.jasper"]]></subreportExpression>
        </subreport>
    </band>
</detail>
<summary>
    <band height="305" splitType="Stretch">
        <textField>
            <reportElement x="0" y="0" width="100" height="50" uuid="91249d6e-57f7-4b6d-8d2a-05059efa2a47"/>
            <textElement textAlignment="Center" verticalAlignment="Middle">
                <font size="16"/>
            </textElement>
            <textFieldExpression><![CDATA[$P{title}]]></textFieldExpression>
        </textField>
        <subreport>
            <reportElement x="0" y="50" width="555" height="32" uuid="780a1f25-065e-494a-ba1d-86a33b464343"/>
            <dataSourceExpression><![CDATA[$P{DS2}]]></dataSourceExpression>
            <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "subjectSubreport.jasper"]]></subreportExpression>
        </subreport>
    </band>
</summary>
</jasperReport>

这是 subjectSubreport.jrxml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.9.0.final using JasperReports Library version 6.9.0-cb8f9004be492ccc537180b49c026951f4220bf3  -->
<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="multitable_summary" pageWidth="555" pageHeight="802" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="2a869c20-1e7d-4baa-92d7-c8d555f1f344">
    <queryString language="json">
        <![CDATA[]]>
    </queryString>
    <field name="subject" class="java.lang.String"/>
    <field name="zipCode" class="java.lang.Integer"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="23">
            <staticText>
                <reportElement x="0" y="0" width="100" height="20" uuid="a9730039-8418-4e58-a2eb-0babe8d2a380"/>
                <text><![CDATA[Subject]]></text>
            </staticText>
        </band>
    </title>
    <columnHeader>
        <band height="20">
            <staticText>
                <reportElement mode="Opaque" x="0" y="0" width="100" height="20" backcolor="#CCCCCC" uuid="a49b18ef-63c3-42bb-ae81-8d7d72ac9086"/>
                <box leftPadding="0">
                    <pen lineWidth="0.25"/>
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement verticalAlignment="Middle"/>
                <text><![CDATA[Subject]]></text>
            </staticText>
            <staticText>
                <reportElement mode="Opaque" x="100" y="0" width="100" height="20" backcolor="#CCCCCC" uuid="c1c70fed-d887-429f-a3b0-ed645517e41f"/>
                <box leftPadding="0">
                    <pen lineWidth="0.25"/>
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <text><![CDATA[ZipCode]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="100" height="20" uuid="401436b3-d9a1-40bd-8a1d-dfa3f162f842"/>
                <box leftPadding="0">
                    <pen lineWidth="0.25"/>
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{subject}]]></textFieldExpression>
            </textField>
            <textField pattern="###0">
                <reportElement x="100" y="0" width="100" height="20" uuid="4447d3b0-6d3e-4795-8b5f-6a09d620448b"/>
                <box leftPadding="0">
                    <pen lineWidth="0.25"/>
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{zipCode}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

这是 printersSubreport.jrxml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.9.0.final using JasperReports Library version 6.9.0-cb8f9004be492ccc537180b49c026951f4220bf3  -->
<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="multitable_subreport" pageWidth="555" pageHeight="802" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="5f89d251-66b1-4658-9af5-c057de138fb6">
    <field name="statusDesc" class="java.lang.String"/>
    <field name="lastUpdateTime" class="java.lang.Integer"/>
    <field name="criticalCount" class="java.lang.String"/>
    <columnHeader>
        <band height="20" splitType="Stretch">
            <staticText>
                <reportElement mode="Opaque" x="0" y="0" width="100" height="20" backcolor="#CCCCCC" uuid="bbb8857c-471c-4aca-a8fc-336d2748d7b9"/>
                <box>
                    <pen lineWidth="0.25"/>
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement verticalAlignment="Middle"/>
                <text><![CDATA[Description]]></text>
            </staticText>
            <staticText>
                <reportElement mode="Opaque" x="100" y="0" width="100" height="20" backcolor="#CCCCCC" uuid="3fde89c9-d9a3-4972-8e3b-b2e619a51601"/>
                <box>
                    <pen lineWidth="0.25"/>
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <text><![CDATA[UpdateTime]]></text>
            </staticText>
            <staticText>
                <reportElement mode="Opaque" x="200" y="0" width="100" height="20" backcolor="#CCCCCC" uuid="e82ece60-c92e-4d0b-8881-29d7d0d78e81"/>
                <box>
                    <pen lineWidth="0.25"/>
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <text><![CDATA[Count]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="100" height="20" uuid="cdec861a-8b3b-413f-8b99-74d0e18ee19d"/>
                <box>
                    <pen lineWidth="0.25"/>
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{statusDesc}]]></textFieldExpression>
            </textField>
            <textField pattern="###0.##">
                <reportElement x="100" y="0" width="100" height="20" uuid="9dfb9f78-74e5-4562-84fd-ebb87c3203ce"/>
                <box>
                    <pen lineWidth="0.25"/>
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{lastUpdateTime}]]></textFieldExpression>
            </textField>
            <textField pattern="¤ #,##0.00">
                <reportElement x="200" y="0" width="100" height="20" uuid="923ff62d-ea1f-410e-94d3-fa7c574c001b"/>
                <box>
                    <pen lineWidth="0.25"/>
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{criticalCount}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

当我切换子报告时,它们中的第一个正确显示,而另一个消失。在JasperSoft Studio中,一切都很好,当我将它们导出到pdf / xls时问题就开始了。

1 个答案:

答案 0 :(得分:1)

  

实际上,仅显示其中一个(第一个)。当我切换子报告时,它们中的第一个正确显示,而另一个消失了

您要声明:

<parameter name="DS1" class="net.sf.jasperreports.engine.data.JsonDataSource"/>
<parameter name="DS2" class="net.sf.jasperreports.engine.data.JsonDataSource"/>

JRDatasource是可消耗的,这意味着如果使用它来填充一个报告,它将被消耗,因此,对于在同一数据源上调用next()的下一个报告,它将返回false。

如果您为DS1DS2传递相同(相同对象)数据源,将会遇到的情况是DS1将被消耗,以填充第一个子报表然后由于在填充第二个子报表时DS1 == DS2 DS2已被使用,因此第二个报表将没有记录(空)。

解决方案

您需要制作一个克隆DS1,创建一个新的Object并将其作为DS2传递。 JsonDataSource从理论上讲也是JRRewindableDataSource,因此您可以在填充第二个子报表之前调用moveFirst(),但这在jrxml文件中并不是很容易,因此我不建议尝试使用此方法方法。

相关问题