我在 JasperReports 中生成子报表时遇到了一些问题。我有 2 份在 iReport 中制作的报告。
主要报告:
<?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代码:
Main方法中的代码:
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;
}
}
我不知道我做错了什么,但我无法生成子报告。它总是空的。当子报表仅包含静态文本时,我遇到了同样的问题。