【问题标题】:Passing an ArrayList report shows comma every item传递 ArrayList 报告显示逗号每个项目
【发布时间】:2016-10-27 09:45:51
【问题描述】:

我通过参数将字符串列表传递给 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 

如何避免?

我的碧玉报告 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

【问题讨论】:

标签: java arraylist jasper-reports


【解决方案1】:

您可以通过多种方式传递List&lt;String&gt;

  1. 传递List&lt;String&gt;作为参数并在subDataset中使用这个参数(在dataSourceExpression中)
  2. JRBeanCollectionDataSource 的帮助下将 List&lt;String&gt; 传递为 JRDataSource
  3. List&lt;String&gt; 转换为 String 对象并将逗号替换为回车符 (\n) 或在 String.replace 方法。

示例

这个例子展示了这两种方法

Java 代码

我们用 List&lt;String&gt; 填充 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 个元素的列表,这些元素在 subDatasetsubDataset 的帮助下显示在 Title 带中em>表格组件。

Summary 带用于显示如何仅使用一个 textField 显示来自 List&lt;String&gt;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 文件如下所示:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-08
    相关资源
    最近更新 更多