您可以使用此数据源表达式将java.util.List(通过参数)传递给子报表:
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{seznamPriloh})]]></dataSourceExpression>
工作样本,主报告:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport ...>
<parameter name="listParam" class="java.util.List"/>
<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
<defaultValueExpression><![CDATA["<subreport_dir>"]]></defaultValueExpression>
</parameter>
<queryString>
<![CDATA[SELECT id, street, city FROM address]]>
</queryString>
<field name="ID" class="java.lang.Integer"/>
<field name="STREET" class="java.lang.String"/>
<field name="CITY" class="java.lang.String"/>
<detail>
<band height="57" splitType="Stretch">
<frame>
<reportElement x="0" y="0" width="539" height="57"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<subreport>
<reportElement x="0" y="32" width="523" height="17"/>
<subreportParameter name="cityParam">
<subreportParameterExpression><![CDATA[$F{CITY}]]></subreportParameterExpression>
</subreportParameter>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{listParam})]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "subreport_list_as_param.jasper"]]></subreportExpression>
</subreport>
<textField>
<reportElement x="300" y="0" width="208" height="20"/>
<box leftPadding="10"/>
<textElement>
<font isBold="true"/>
</textElement>
<textFieldExpression><![CDATA["City: " + $F{CITY}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="100" y="0" width="200" height="20"/>
<box leftPadding="10"/>
<textElement>
<font isBold="true"/>
</textElement>
<textFieldExpression><![CDATA["Street: " + $F{STREET}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="0" y="0" width="100" height="20"/>
<box leftPadding="10"/>
<textElement>
<font isBold="true"/>
</textElement>
<textFieldExpression><![CDATA["Id: " + $F{ID}]]></textFieldExpression>
</textField>
</frame>
</band>
</detail>
</jasperReport>
子报告:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport ...>
<parameter name="cityParam" class="java.lang.String"/>
<field name="id" class="java.lang.Integer"/>
<field name="station" class="java.lang.String"/>
<field name="city" class="java.lang.String"/>
<filterExpression><![CDATA[$F{city}.equals($P{cityParam})]]></filterExpression>
<title>
<band height="39">
<textField>
<reportElement x="220" y="14" width="161" height="20"/>
<box leftPadding="10"/>
<textElement>
<font isBold="true" isItalic="true"/>
</textElement>
<textFieldExpression><![CDATA["City param: " + $P{cityParam}]]></textFieldExpression>
</textField>
</band>
</title>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="100" height="20"/>
<box leftPadding="10"/>
<textElement/>
<textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="100" y="0" width="100" height="20"/>
<box leftPadding="10"/>
<textElement/>
<textFieldExpression><![CDATA[$F{station}]]></textFieldExpression>
</textField>
</band>
</detail>
<noData>
<band height="50">
<textField>
<reportElement x="220" y="17" width="161" height="20"/>
<box leftPadding="10"/>
<textElement>
<font isBold="true" isItalic="true"/>
</textElement>
<textFieldExpression><![CDATA["No data for city param: " + $P{cityParam}]]></textFieldExpression>
</textField>
</band>
</noData>
</jasperReport>
用于传递List的 Java 代码:
Map<String, Object> params = new HashMap<String, Object>();
List<TestBean> beansList = new ArrayList<TestBean>();
// The TestBean class constructor is:
//public TestBean(String city, Integer id, String station)
TestBean bean = new TestBean("Dallas", 10, "Central park st.");
beansList.add(bean);
bean = new TestBean("Dallas", 11, "Railway st.");
beansList.add(bean);
bean = new TestBean("Dallas", 12, "Market st.");
beansList.add(bean);
bean = new TestBean("Lyon", 20, "Airport st.");
beansList.add(bean);
params.put("listParam", beansList);
JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, getDemoHsqldbConnection());
JasperExportManager.exportReportToPdfFile(jasperPrint, outputFileName);
结果将是(生成的 PDF 文件的视图):
您可以查看net.sf.jasperreports.engine.JRDataSource的实现。最适合您的情况是:JRBeanCollectionDataSource和JRBeanArrayDataSource。如您所见,它们都是基于 Bean 的。
我认为您可以轻松地将您的转换List<String>
为List<StringBean>
.
或者您可以实现自己的JRDataSource。