您可以通过多种方式传递List<String>:
- 传递
List<String>作为参数并在subDataset中使用这个参数(在dataSourceExpression中)
- 在 JRBeanCollectionDataSource 的帮助下将
List<String> 传递为 JRDataSource
- 将
List<String> 转换为 String 对象并将逗号替换为回车符 (\n) 或在 String.replace 方法。
示例
这个例子展示了这两种方法
Java 代码
我们用 List<String> 填充 listOfItems 参数,并用 JRBeanCollectionDataSource 填充报告。
JRDataSource dataSource = new JRBeanCollectionDataSource(Arrays.asList("item 1", "item 2", "item 3", "item 4", "item 5", "item 6"));
Map<String, Object> params = new HashMap<>();
params.put("listOfItems", Arrays.asList("Title 1", "Title 2", "Title 3"));
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource);
报告模板
数据源包含 6 个项目(元素),用于显示在 详细信息 带(主数据集)中。
参数 listOfItems 包含 3 个元素的列表,这些元素在 subDataset 的 subDataset 的帮助下显示在 Title 带中em>表格组件。
Summary 带用于显示如何仅使用一个 textField 显示来自 List<String>(listOfItems 参数)的数据 元素。
fieldDescription 帮助我们获取字段的值。在 _THIS 关键字的帮助下,我们得到了 String 值。
<?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="Passing List of String" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<subDataset name="listDataset">
<field name="name" class="java.lang.String">
<fieldDescription><![CDATA[_THIS]]></fieldDescription>
</field>
</subDataset>
<parameter name="listOfItems" class="java.util.List"/>
<field name="item" class="java.lang.String">
<fieldDescription><![CDATA[_THIS]]></fieldDescription>
</field>
<title>
<band height="27">
<componentElement>
<reportElement x="340" y="0" width="200" height="15">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
</reportElement>
<jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
<datasetRun subDataset="listDataset">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{listOfItems})]]></dataSourceExpression>
</datasetRun>
<jr:column width="200">
<jr:detailCell height="15">
<textField>
<reportElement x="0" y="0" width="200" height="15"/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</band>
</title>
<detail>
<band height="15" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="100" height="15"/>
<textFieldExpression><![CDATA[$F{item}]]></textFieldExpression>
</textField>
</band>
</detail>
<summary>
<band height="60" splitType="Stretch">
<textField>
<reportElement x="0" y="15" width="100" height="15" />
<textFieldExpression><![CDATA[$P{listOfItems}.toString().replace(",", " ").replace("[", "").replace("]", "")]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement x="300" y="40" width="100" height="15"/>
<textFieldExpression><![CDATA[$P{listOfItems}.toString().replace(", ", "\n").replace("[", "").replace("]", "")]]></textFieldExpression>
</textField>
</band>
</summary>
</jasperReport>
List.toString 方法的使用给出如下结果:[val1, val2] - 值用逗号分隔并括在方括号中。 String.replace 方法的使用(这个方法的多次串行调用)给了我们很好的结果。
输出结果
在 JRPdfExporter 的帮助下生成的 pdf 文件如下所示: