JasperReports:子报表呈现多次,导致重复的页面

时间:2012-08-06 22:33:30

标签: jasper-reports duplicates subreport

我正在使用 JasperReports 使用两个单独的查询为Web应用程序生成图表和表格。我的主 JRXML 生成图表,并在 Detail 区域中包含子报表以生成表格。但是,当它呈现时,子报表似乎被多次渲染(确切地说是12),这导致生成的报表在12页上是相同的图形和表格(彼此完全相同)。这通过在线应用程序和 iReport 发生。

我的主要 JRXML ,子报告 JRXML 和报告生成代码如下(由于NDA原因,已删除了一些详细信息)。

对问题的任何见解或想法都会非常有帮助。

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="report" language="groovy" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
        <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["<<PATH_TO_SUBREPORT>>"]]></defaultValueExpression>
        </parameter>
        <parameter name="param" class="java.util.Collection" />     
     <queryString language="SQL">
            <![CDATA[<<DATABASE_QUERY>>]]>
        </queryString>
        <field name="FIELD1" class="java.lang.Integer"/>
        <field name="FIELD2" class="java.sql.Date"/>
        <field name="FIELD3" class="java.math.BigDecimal"/>
        <field name="FIELD4" class="java.math.BigDecimal"/>
        <field name="FIELD5" class="java.math.BigDecimal"/>
        <background>
            <band splitType="Stretch"/>
        </background>
        <title>
            <band height="10" splitType="Stretch"/>
        </title>
        <pageHeader>
            <band height="5" splitType="Stretch"/>
        </pageHeader>
        <columnHeader>
            <band height="5" splitType="Stretch"/>
        </columnHeader>
        <detail>
            <band height="513" splitType="Stretch">
                <lineChart>
                    <chart>
                        <reportElement positionType="Float" x="51" y="0" width="680" height="400"/>
                        <chartTitle/>
                        <chartSubtitle/>
                        <chartLegend/>
                    </chart>
                    <categoryDataset>
                        <categorySeries>
                            <seriesExpression><![CDATA["FIELD1"]]></seriesExpression>
                            <categoryExpression><![CDATA[$F{f1}]]></categoryExpression>
                            <valueExpression><![CDATA[$F{f2}]]></valueExpression>
                        </categorySeries>
                        <categorySeries>
                            <seriesExpression><![CDATA["FIELD2"]]></seriesExpression>
                            <categoryExpression><![CDATA[$F{f1}]]></categoryExpression>
                            <valueExpression><![CDATA[$F{f2}]]></valueExpression>
                        </categorySeries>
                        <categorySeries>
                            <seriesExpression><![CDATA["FIELD3"]]></seriesExpression>
                            <categoryExpression><![CDATA[$F{f1}]]></categoryExpression>
                            <valueExpression><![CDATA[$F{f2}]]></valueExpression>
                        </categorySeries>
                    </categoryDataset>
                    <linePlot>
                        <plot labelRotation="-90.0"/>
                        <categoryAxisFormat labelRotation="-90.0">
                            <axisFormat>
                                <tickLabelFont>
                                    <font size="12"/>
                                </tickLabelFont>
                            </axisFormat>
                        </categoryAxisFormat>
                        <rangeAxisMinValueExpression><![CDATA[0]]></rangeAxisMinValueExpression>
                        <rangeAxisMaxValueExpression><![CDATA[101]]></rangeAxisMaxValueExpression>
                    </linePlot>
                </lineChart>
                <subreport>
                    <reportElement x="377" y="413" width="425" height="100"/>
                    <subreportParameter name="param">
                        <subreportParameterExpression><![CDATA[$P{param}]]></subreportParameterExpression>
                    </subreportParameter>
                    <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                    <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "testReport.jasper"]]></subreportExpression>
                </subreport>
            </band>
        </detail>
        <columnFooter>
            <band height="5" splitType="Stretch"/>
        </columnFooter>
        <pageFooter>
            <band height="15" splitType="Stretch">
                <textField>
                    <reportElement mode="Opaque" x="3" y="2" width="759" height="13" backcolor="#E6E6E6"/>
                    <textElement textAlignment="Right"/>
                    <textFieldExpression><![CDATA["Page "+$V{PAGE_NUMBER}+" of"]]></textFieldExpression>
                </textField>
                <textField evaluationTime="Report">
                    <reportElement mode="Opaque" x="762" y="2" width="40" height="13" backcolor="#E6E6E6"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression>
                </textField>
                <textField pattern="EEEEE dd MMMMM yyyy">
                    <reportElement x="3" y="0" width="100" height="13"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[new SimpleDateFormat("MM-dd-yyyy").format(
    new java.util.Date())]]></textFieldExpression>
                </textField>
            </band>
        </pageFooter>
        <summary>
            <band height="10" splitType="Stretch"/>
        </summary>
    </jasperReport>

子报告 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="evcas_sub" language="groovy" pageWidth="450" pageHeight="150" columnWidth="10" leftMargin="10" rightMargin="10" topMargin="10" bottomMargin="10">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <style name="table">
        <box>
            <pen lineWidth="1.0" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_TH" mode="Opaque" backcolor="#0000FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_CH" mode="Opaque" backcolor="#BFBFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
        <conditionalStyle>
            <conditionExpression><![CDATA[new Boolean($V{REPORT_COUNT}.intValue()%2==0)]]></conditionExpression>
            <style backcolor="#EFEFFF"/>
        </conditionalStyle>
    </style>
    <style name="Crosstab Data Text" hAlign="Center"/>
    <parameter name="param" class="java.util.Collection" />
    <queryString>
        <![CDATA[<<DATABASE_QUERY>>]]>
    </queryString>
    <field name="FIELD1" class="java.lang.String"/>
    <field name="FIELD2" class="java.math.BigDecimal"/>
    <field name="FIELD3" class="java.math.BigDecimal"/>
    <field name="FIELD4" class="java.math.BigDecimal"/>
    <field name="FIELD5" class="java.math.BigDecimal"/>
    <field name="FIELD6" class="java.math.BigDecimal"/>
    <field name="FIELD7" class="java.math.BigDecimal"/>
    <field name="FIELD8" class="java.math.BigDecimal"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="5" splitType="Stretch"/>
    </title>
    <pageHeader>
        <band height="5"/>
    </pageHeader>
    <columnHeader>
        <band height="12">
            <textField>
                <reportElement style="table_TH" x="0" y="0" width="50" height="12" backcolor="#FFFFFF"/>
                <textElement>
                    <font size="9"/>
                    <paragraph leftIndent="2"/>
                </textElement>
                <textFieldExpression><![CDATA["FIELD1"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement style="table_TH" x="50" y="0" width="50" height="12" backcolor="#FFFFFF"/>
                <textElement>
                    <font size="9"/>
                    <paragraph leftIndent="2"/>
                </textElement>
                <textFieldExpression><![CDATA["FIELD2"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement style="table_TH" x="100" y="0" width="50" height="12" backcolor="#FFFFFF"/>
                <textElement>
                    <font size="9"/>
                    <paragraph leftIndent="2"/>
                </textElement>
                <textFieldExpression><![CDATA["FIELD3"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement style="table_TH" x="150" y="0" width="50" height="12" backcolor="#FFFFFF"/>
                <textElement>
                    <font size="9"/>
                    <paragraph leftIndent="2"/>
                </textElement>
                <textFieldExpression><![CDATA["FIELD4"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement style="table_TH" x="200" y="0" width="50" height="12" backcolor="#FFFFFF"/>
                <textElement>
                    <font size="9"/>
                    <paragraph leftIndent="2"/>
                </textElement>
                <textFieldExpression><![CDATA["FIELD5"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement style="table_TH" x="250" y="0" width="50" height="12" backcolor="#FFFFFF"/>
                <textElement>
                    <font size="9"/>
                    <paragraph leftIndent="2"/>
                </textElement>
                <textFieldExpression><![CDATA["FIELD6"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement style="table_TH" x="300" y="0" width="50" height="12" backcolor="#FFFFFF"/>
                <textElement>
                    <font size="9"/>
                    <paragraph leftIndent="2"/>
                </textElement>
                <textFieldExpression><![CDATA["FIELD7"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement style="table_TH" x="350" y="0" width="50" height="12" backcolor="#FFFFFF"/>
                <textElement>
                    <font size="9"/>
                    <paragraph leftIndent="2"/>
                </textElement>
                <textFieldExpression><![CDATA["FIELD8"]]></textFieldExpression>
            </textField>
        </band>
    </columnHeader>
    <detail>
        <band height="12">
            <textField>
                <reportElement style="table_CH" x="0" y="0" width="50" height="12" backcolor="#FFFFFF"/>
                <textElement>
                    <font size="8"/>
                    <paragraph leftIndent="2"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{f1}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement style="table_CH" x="50" y="0" width="50" height="12" backcolor="#FFFFFF"/>
                <textElement>
                    <font size="8"/>
                    <paragraph leftIndent="2"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{f2}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement style="table_CH" x="100" y="0" width="50" height="12" backcolor="#FFFFFF"/>
                <textElement>
                    <font size="8"/>
                    <paragraph leftIndent="2"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{f3}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement style="table_CH" x="150" y="0" width="50" height="12" backcolor="#FFFFFF"/>
                <textElement>
                    <font size="8"/>
                    <paragraph leftIndent="2"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{f4}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement style="table_CH" x="200" y="0" width="50" height="12" backcolor="#FFFFFF"/>
                <textElement>
                    <font size="8"/>
                    <paragraph leftIndent="2"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{f5}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement style="table_CH" x="250" y="0" width="50" height="12" backcolor="#FFFFFF"/>
                <textElement>
                    <font size="8"/>
                    <paragraph leftIndent="2"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{f6}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement style="table_CH" x="300" y="0" width="50" height="12" backcolor="#FFFFFF"/>
                <textElement>
                    <font size="8"/>
                    <paragraph leftIndent="2"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{f7}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement style="table_CH" x="350" y="0" width="50" height="12" backcolor="#FFFFFF"/>
                <textElement>
                    <font size="8"/>
                    <paragraph leftIndent="2"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{f8}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band height="5" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="5" splitType="Stretch"/>
    </pageFooter>
</jasperReport>

渲染代码:

String strFileName = "<<FILENAME>>";
File jrxml = new File(strFileName);
InputStream iStream = new FileInputStream(jrxml);
JasperReport objJReport = JasperCompileManager.compileReport(iStream);
JasperPrint objJPrint = JasperFillManager.fillReport(objJReport, hashMap, datasource.getConnection());
JasperExportManager.exportReportToPdfStream(objJPrint, os);

1 个答案:

答案 0 :(得分:2)

您的数据集中的每条记录都会打印一个详细信息带。因此,如果您的数据库查询返回12行,您将获得12个详细信息区域的副本。每个波段都使用单行数据,但这对您来说无关紧要,因为您使用图表和子报表来显示数据。

为避免这种重复,只需将图表和子报告放入不同的波段即可。我会选择列标题或页脚,但除了详细信息之外的任何内容都可以使用。