你可以试试这个样本。
POJO (豆):
public class BeanWithList {
private List<String> m_cities;
private Integer m_id;
public BeanWithList(List<String> cities, Integer id) {
m_cities = cities;
m_id = id;
}
public List<String> getCities() {
return m_cities;
}
public Integer getId() {
return m_id;
}
}
填写报告的代码:
public static void testBuildPdf() {
try {
Map<String, Object> params = new HashMap<String, Object>();
params.put("BeanSubreport", JasperCompileManager.compileReport(subreportSource));
JasperReport jasperReport = JasperCompileManager.compileReport(masterSource);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, getDataSource());
JasperExportManager.exportReportToPdfFile(jasperPrint, outputFileName);
} catch (Exception e) {
e.printStackTrace();
}
}
private static JRDataSource getDataSource() {
Collection<BeanWithList> coll = new ArrayList<BeanWithList>();
BeanWithList bean = new BeanWithList(Arrays.asList("London", "Paris"), 1);
coll.add(bean);
bean = new BeanWithList(Arrays.asList("London", "Madrid", "Moscow"), 2);
coll.add(bean);
bean = new BeanWithList(Arrays.asList("Rome"), 3);
coll.add(bean);
return new JRBeanCollectionDataSource(coll);
}
子报表jrxml:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport .. leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0">
<field name="city" class="java.lang.String">
<fieldDescription><![CDATA[_THIS]]></fieldDescription>
</field>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement positionType="Float" x="0" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{city}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
主jrxml:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport ...>
<parameter name="BeanSubreport" class="net.sf.jasperreports.engine.JasperReport"/>
<field name="id" class="java.lang.Integer"/>
<field name="cities" class="java.util.Collection"/>
<title>
<band height="103" splitType="Stretch">
<staticText>
<reportElement x="138" y="28" width="258" height="20"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[Bean with List sample]]></text>
</staticText>
</band>
</title>
<columnHeader>
<band height="20">
<staticText>
<reportElement x="0" y="0" width="100" height="20"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true" isUnderline="false"/>
</textElement>
<text><![CDATA[Id]]></text>
</staticText>
<staticText>
<reportElement x="100" y="0" width="100" height="20"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true" isUnderline="false"/>
</textElement>
<text><![CDATA[City name]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement stretchType="RelativeToTallestObject" x="0" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
</textField>
<subreport>
<reportElement positionType="Float" stretchType="RelativeToTallestObject" x="100" y="0" width="100" height="20" isPrintWhenDetailOverflows="true"/>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{cities})]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{BeanSubreport}]]></subreportExpression>
</subreport>
</band>
</detail>
</jasperReport>
结果将是:
该示例的主要技巧是使用_THIS
“变量”。
有关更多信息,您可以阅读JavaBean 数据源文章。