我通过参数将String of List传递给JasperReports报告。
String jasperFileName = "C:\\TestReportProcess.jasper";
Map<String, Object> params = new HashMap<String, Object>();
params.put("List", getMyListOfString());
JasperPrint jprint = (JasperPrint) asperFillManager.fillReport(jasperFileName, params, new JREmptyDataSource());
报告开始时显示逗号每个项目
item 1,
item 2,
item 3,
item 4,
etc etc
如何避免它?
我的jasper报告xml
<parameter name="List" class="java.util.ArrayList" isForPrompting="false"/>
<detail>
<band height="280" splitType="Stretch">
<textField isStretchWithOverflow="true" pattern="">
<reportElement x="0" y="13" width="550" height="45" uuid="f907894e-e9f1-418b-9ab8-1db276b8482e"/>
<textElement>
<font fontName="Antique Olive Compact"/>
</textElement>
<textFieldExpression><![CDATA[$P{List}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
这是我的简单xml报告,只有一个参数java.util.Arraylist
答案 0 :(得分:2)
您可以通过多种方式传递 List<String>
:
List<String>
作为参数传递,并在 subDataset 中使用此参数(在 dataSourceExpression 中)List<String>
作为 JRDataSource 传递
List<String>
转换为 String 对象,并将逗号替换为回车符( \ n )或只是删除逗号帮助 String.replace 方法。该示例显示了两种方法
我们使用 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 项目(元素),用于在 Detail band(主数据集)中显示。
参数 listOfItems 包含 3 元素的列表,这些元素将在 subDataset 的标题带中显示em>表组件。
摘要频段用于显示如何仅使用一个 List<String>
( listOfItems 参数)的数据> textField 元素。
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 文件如下所示: