2012-08-06 176 views
2

我正在使用JasperReports使用兩個單獨的查詢爲Web應用程序生成圖形和表格。我的主要JRXML生成圖表,並且我在詳細帶子中包含一個子報表以生成表格。但是,在呈現時,子報表似乎會多次呈現(精確爲12),這會導致生成的報表在12個頁面上呈現相同的圖形和表格(相互重複)。這通過在線應用程序和iReport發生。JasperReports:子報表多次渲染,導致重複頁面

我的主要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> 

Sub報表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

一個細節帶每個記錄印在你的數據集。所以如果你的數據庫查詢返回12行,你會得到12個細節帶的副本。每個樂隊都使用單行數據,但這對您而言無關緊要,因爲您正在使用圖表和子報表來顯示數據。

爲了避免這種重複,只需將您的圖表和子報表放入不同的樂隊。我會去列標題或頁腳,但除了細節以外的任何內容都可以工作。

+0

我的數據庫查詢返回任意數量的行 - 無論它返回多少,它總是有12個重複項。另外,由於它是任意數量的行,所以如果我將它放在列標題/頁腳中,則會出現溢出錯誤,說如果查詢返回的數量大於指定的大小,則該band不可擴展。 – 2012-08-07 13:06:52