Jasper报告中的空白子报告

时间:2011-10-31 14:00:58

标签: jasper-reports subreport

我在空白子报告中看到了很多问题,但没有一个提供我需要的信息。我有一份主报告:

<?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="sample_report"  pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" 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="reportTitle" class="java.lang.String"/>
<parameter name="reportSubTitle" class="java.lang.String"/>
<background>
    <band splitType="Stretch"/>
</background>
<title>
    <band height="65" splitType="Stretch">
        <textField>
            <reportElement x="0" y="0" width="555" height="20"/>
            <textElement textAlignment="Center"/>
            <textFieldExpression><![CDATA[$P{reportTitle}]]></textFieldExpression>
        </textField>
        <textField>
            <reportElement x="434" y="40" width="80" height="20"/>
            <textElement textAlignment="Right"/>
            <textFieldExpression><![CDATA["Page "+$V{PAGE_NUMBER}+" of"]]></textFieldExpression>
        </textField>
        <textField evaluationTime="Report">
            <reportElement x="514" y="40" width="40" height="20"/>
            <textElement/>
            <textFieldExpression><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression>
        </textField>
        <textField>
            <reportElement x="0" y="20" width="555" height="20"/>
            <textElement textAlignment="Center"/>
            <textFieldExpression><![CDATA[$P{reportSubTitle}]]></textFieldExpression>
        </textField>
    </band>
</title>
<pageHeader>
    <band splitType="Stretch"/>
</pageHeader>
<columnHeader>
    <band splitType="Stretch"/>
</columnHeader>
<detail>
    <band height="109" splitType="Stretch">
        <subreport isUsingCache="false">
            <reportElement x="0" y="0" width="555" height="100" isPrintWhenDetailOverflows="true"/>
            <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
            <subreportExpression><![CDATA["sample_report_activity_report.jasper"]]></subreportExpression>
        </subreport>
    </band>
</detail>
<columnFooter>
    <band splitType="Stretch"/>
</columnFooter>
<pageFooter>
    <band splitType="Stretch"/>
</pageFooter>
<summary>
    <band splitType="Stretch"/>
</summary>
</jasperReport>

和子报告:

<?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="sample_report_activity_report" pageWidth="802" pageHeight="555" orientation="Landscape" whenNoDataType="AllSectionsNoDetail" columnWidth="802" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<queryString language="SQL">
    <![CDATA[SELECT ActivityType, Count, Amount, ActivityDate FROM Activity WHERE ARIID = 1]]>
</queryString>
<field name="ActivityType" class="java.lang.String"/>
<field name="Count" class="java.lang.String"/>
<field name="Amount" class="java.lang.String"/>
<field name="ActivityDate" class="java.lang.String"/>
<background>
    <band splitType="Stretch"/>
</background>
<title>
    <band height="24" splitType="Stretch">
        <staticText>
            <reportElement x="0" y="0" width="802" height="20"/>
            <textElement textAlignment="Center"/>
            <text><![CDATA[Activity Report]]></text>
        </staticText>
    </band>
</title>
<pageHeader>
    <band splitType="Stretch"/>
</pageHeader>
<columnHeader>
    <band height="25" splitType="Stretch">
        <staticText>
            <reportElement x="12" y="0" width="100" height="20"/>
            <textElement/>
            <text><![CDATA[ActivityType]]></text>
        </staticText>
        <staticText>
            <reportElement x="112" y="0" width="100" height="20"/>
            <textElement/>
            <text><![CDATA[Count]]></text>
        </staticText>
        <staticText>
            <reportElement x="215" y="0" width="100" height="20"/>
            <textElement/>
            <text><![CDATA[Amount]]></text>
        </staticText>
        <staticText>
            <reportElement x="315" y="0" width="100" height="20"/>
            <textElement/>
            <text><![CDATA[ActivityDate]]></text>
        </staticText>
    </band>
</columnHeader>
<detail>
    <band height="22" splitType="Stretch">
        <textField>
            <reportElement x="12" y="0" width="100" height="20"/>
            <textElement/>
            <textFieldExpression><![CDATA[$F{ActivityType}]]></textFieldExpression>
        </textField>
        <textField>
            <reportElement x="115" y="0" width="100" height="20"/>
            <textElement/>
            <textFieldExpression><![CDATA[$F{Count}]]></textFieldExpression>
        </textField>
        <textField>
            <reportElement x="215" y="0" width="100" height="20"/>
            <textElement/>
            <textFieldExpression><![CDATA[$F{Amount}]]></textFieldExpression>
        </textField>
        <textField>
            <reportElement x="315" y="2" width="100" height="20"/>
            <textElement/>
            <textFieldExpression><![CDATA[$F{ActivityDate}]]></textFieldExpression>
        </textField>
    </band>
</detail>
<columnFooter>
    <band splitType="Stretch"/>
</columnFooter>
<pageFooter>
    <band splitType="Stretch"/>
</pageFooter>
<summary>
    <band height="42" splitType="Stretch">
        <staticText>
            <reportElement x="0" y="0" width="802" height="20"/>
            <textElement textAlignment="Center"/>
            <text><![CDATA[--- End of Activity Report ---]]></text>
        </staticText>
    </band>
</summary>
</jasperReport>

我有以下Java代码:

Map parameters = new HashMap();
parameters.put("reportTitle", "Report");
parameters.put("reportSubTitle", "Activity Date 10/25/2011");

JasperReport jasperReportMain = JasperCompileManager.compileReport("c:\\tempfilepath\\sample_report.jrxml");

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReportMain, parameters, con);
JasperExportManager.exportReportToPdfFile(jasperPrint, "c:\\tempfilepath\\SampleReport.pdf");

据我所知,我已经按照之前的所有建议进行操作,但在运行主报表时,我的子报表中仍然没有显示任何数据。我做错了什么?

3 个答案:

答案 0 :(得分:4)

可能是子报表中的查询没有返回任何行。您可以尝试在子报表中添加No Data个频段,在其中添加一些Static Text,并将(子)报表属性When No Data设置为No Data Section

答案 1 :(得分:0)

我没有注意到您的XML JR代码存在任何异常。 首先,您确定您的数据源(数据库结果集)是否返回任何结果并且设置正确吗?

我还建议为Jasper Reports安装IREPORT Designer工具。我认为最新版本是4.1.2。这将极大地帮助您进行设计和错误/异常处理。

答案 2 :(得分:0)

我可能错了,因为我不太了解jrxml作为代码,因为我一直使用iReport开发报告。你是否将参数从主报告传递到子报告?如果是,请通过右键单击子报表来检查是否已传递子报表的参数。