传递 ArrayList 报告显示每个项目的逗号

Mas*_*imo 5 java arraylist jasper-reports

我通过参数将字符串列表传递给 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());
Run Code Online (Sandbox Code Playgroud)

当报告开始时,它显示每个项目的逗号

item 1,
item 2,
item 3,
item 4,
etc etc 
Run Code Online (Sandbox Code Playgroud)

怎样才能避免呢?

我的碧玉报告 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>
Run Code Online (Sandbox Code Playgroud)

这是我的简单 xml 报告,只有一个参数 java.util.Arraylist

Ale*_*x K 4

List<String>您可以通过多种方式传递:

  1. List<String>作为参数传递并在 subDataset 中使用此参数(dataSourceExpression
  2. 在JRBeanCollectionDataSource的帮助下List<String>作为JRDataSource传递
  3. 转换List<String>String对象并将逗号替换为回车符 ( \n ) 或借助String.replace方法删除逗号。

例子

该示例显示了两种方法

Java代码

我们使用JRBeanCollectionDataSource填充listOfItems参数List<String>并填充报告。

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);
Run Code Online (Sandbox Code Playgroud)

报告模板

数据源包含6 个项目(元素),用于在详细信息带(主数据集)中显示。

参数listOfItems包含在Table组件的subDataset的帮助下在标题带中显示的3 个元素的列表。

摘要区用于展示如何仅使用一个textFieldList<String>元素显示来自 (listOfItems参数)的数据。

fieldDescription帮助我们获取字段的值在_THIS关键字的帮助下,我们获取了字符串值。

<?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>
Run Code Online (Sandbox Code Playgroud)

List.toString方法的使用给出如下结果:[val1, val2]- 用逗号分隔并括在方括号中的值。使用String.replace方法(多次连续调用该方法)给我们带来了很好的结果。

输出结果

在JRPdfExporter的帮助下生成的pdf文件如下所示:

生成的pdf