无法从Java代码生成JasperReports中的子报表

时间:2011-09-30 11:15:40

标签: java jasper-reports subreports

我在JasperReports中生成子报表时遇到了一些问题。 我在iReport中制作了2份报告。

主要报告:

<?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="raporcik" language="groovy" pageWidth="595" pageHeight="842" 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="autor" class="java.lang.String"/>
<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
    <defaultValueExpression><![CDATA["C:\\Documents and Settings\\user\\workspace\\Jasper\\report\\"]]></defaultValueExpression>
</parameter>
<field name="name" class="java.lang.String"/>
<background>
    <band splitType="Stretch"/>
</background>
<title>
    <band height="79" splitType="Stretch">
        <staticText>
            <reportElement x="242" y="31" width="115" height="28"/>
            <textElement>
                <font size="18"/>
            </textElement>
            <text><![CDATA[Something]]></text>
        </staticText>
        <textField>
            <reportElement x="439" y="59" width="100" height="20"/>
            <textElement/>
            <textFieldExpression class="java.lang.String"><![CDATA[$P{autor}]]></textFieldExpression>
        </textField>
    </band>
</title>
<pageHeader>
    <band height="35" splitType="Stretch"/>
</pageHeader>
<detail>
    <band height="28" splitType="Stretch"/>
    <band height="50">
        <subreport>
            <reportElement x="46" y="0" width="200" height="50"/>
            <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
            <subreportExpression class="java.lang.String"><![CDATA[$P{SUBREPORT_DIR} + "info.jasper"]]></subreportExpression>
        </subreport>
    </band>
</detail>
<summary>
    <band height="42" splitType="Stretch"/>
</summary>

第二份报告(子报告):

<?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="info" language="groovy" pageWidth="555" pageHeight="802" columnWidth="555" 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"/>
<field name="name" class="java.lang.String"/>
<background>
    <band splitType="Stretch"/>
</background>
<detail>
    <band height="52" splitType="Stretch">
        <textField>
            <reportElement x="37" y="12" width="100" height="20"/>
            <textElement/>
            <textFieldExpression class="java.lang.String"><![CDATA[$F{name}]]></textFieldExpression>
        </textField>
    </band>
</detail>
</jasperReport>

和Java代码:

主要方法中的代码:

    JasperDesign jsp = JRXmlLoader.load("C:/Documents and Settings/user/workspace/Jasper/report/raporcik.jrxml");       
    JasperCompileManager.compileReportToFile("C:/Documents and Settings/user/workspace/Jasper/report/info.jrxml");      
    JasperReport jasperReport = JasperCompileManager.compileReport(jsp);        

    Map<String, Object> parametros = new HashMap<String, Object>();
    parametros.put("autor", "Jasper W.");
    parametros.put("SUBREPORT_DIR","C:/Documents and Settings/user/workspace/Jasper/report/");
    List lista = new ArrayList();
    lista.add(new Name("something1"));
    lista.add(new Name("something2"));

    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parametros, new JRBeanCollectionDataSource(lista));    


    JRExporter exporter = new JRPdfExporter();
    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
    exporter.setParameter(JRExporterParameter.OUTPUT_FILE, new java.io.File("C:/Documents and Settings/user/workspace/Jasper/report/hello.pdf"));
    exporter.exportReport();    

和姓名类:

 public class Name {

String name;

public Name(){

}

public Name(String _name){
    this.name = _name;      
}   

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

}

我不知道我做错了什么,但我无法生成子报告。它总是空的。当子报表只包含静态文本时,我遇到同样的问题。

1 个答案:

答案 0 :(得分:0)

您的子报表可能是空的,可能是因为您的子报表或主报表的JRXML中没有数据源或查询。您可以在this answer中找到更多信息。它具有您只需要使用静态字段的子报表所需的内容。

所以你的问题不完全是子报告,而是Jasper Reports环境生成报告的方式。