使用JasperReports API创建子报告

时间:2014-05-09 04:06:17

标签: java jasper-reports

我可以使用 JasperReports API 创建主报告。但是,使用 JasperReports API 设计子报告并将其添加到主报表设计中时会遇到困难。

主报告JRXML

    <band height="250" splitType="Stretch">
        <subreport>
        <reportElement isPrintRepeatedValues="false" x="0" y="0" width="550" height="233" isRemoveLineWhenBlank="true" backcolor="#000000">
        </reportElement>
        <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{time})]]>   
</dataSourceExpression>
        <subreportExpression><![CDATA["subreport.jasper"]]></subreportExpression>
    </subreport>
   </band>

子报告JRXML

<field name="count" class="java.lang.String"/>
<field name="timeStamp" class="java.lang.String"/>
<group name="ProductGroup">
    <groupExpression><![CDATA[$F{count}]]></groupExpression>
    <groupHeader>
        <band height="250">
            <timeSeriesChart>
                <chart evaluationTime="Report">
                    <reportElement x="0" y="25" width="550" height="175"/>
                    <chartTitle/>
                    <chartSubtitle/>
                    <chartLegend/>
                </chart>
                <timeSeriesDataset timePeriod="Minute">
                    <dataset incrementType="None"/>
                    <timeSeries>
                        <seriesExpression><![CDATA["Count"]]></seriesExpression>
                        <timePeriodExpression><![CDATA[new Date(Long.valueOf($F{timeStamp})*1000)]]></timePeriodExpression>
                        <valueExpression><![CDATA[Integer.valueOf($F{count})]]></valueExpression>
                    </timeSeries>
                </timeSeriesDataset>
                <timeSeriesPlot isShowLines="true" isShowShapes="false">
                    <plot/>
                    <timeAxisFormat>
                        <axisFormat>
                            <labelFont/>
                            <tickLabelFont/>
                        </axisFormat>
                    </timeAxisFormat>
                    <valueAxisFormat>
                        <axisFormat>
                            <labelFont/>
                            <tickLabelFont/>
                        </axisFormat>
                    </valueAxisFormat>
                </timeSeriesPlot>
            </timeSeriesChart> 
        </band>
    </groupHeader>
</group>

这些独立的XML正在编译并将生成报告。

但是我想使用Jasper Report API创建这些。 我刚刚开始。但没有任何可用的例子就卡住了。

JRDesignSubreport jSubreport = new JRDesignSubreport(jasperDesign); 
jSubreport.setUsingCache(false); 
jSubreport.setRemoveLineWhenBlank(true);

JRDesignExpression subReportDataSourceExpr = new JRDesignExpression();
subReportDataSourceExpr.addResourceChunk("");
//How to set List Data Source?
JRDesignSubreportParameter subReportDataSource = new JRDesignSubreportParameter();
subReportDataSource.setExpression(subReportDataSourceExpr);
jSubreport.setDataSourceExpression(subReportDataSourceExpr);

//How to Add <subreportExpression><![CDATA["subreport.jasper"]]></subreportExpression>

1 个答案:

答案 0 :(得分:3)

这可以如下完成 将子报告放在一个乐队中,然后将乐队附加到主报告

band = new JRDesignBand();
band.setHeight(250);
band.setSplitType(SplitTypeEnum.STRETCH);

JRDesignSubreport jSubreport = new JRDesignSubreport(jasperDesign); 
jSubreport.setUsingCache(false); 
jSubreport.setRemoveLineWhenBlank(true); 
jSubreport.setPrintRepeatedValues(false);

JRDesignExpression expression = new JRDesignExpression();
expression.setText("new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{time})");
jSubreport.setDataSourceExpression(expression);

expression = new JRDesignExpression(); 
expression.setText("\"/path/to/sub.jasper\"");

jSubreport.setExpression(expression);
band.addElement(jSubreport);