传递 ArrayList 报告显示每个项目的逗号 [英] Passing an ArrayList report shows comma every item

查看:17
本文介绍了传递 ArrayList 报告显示每个项目的逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过参数将字符串列表传递给 JasperReports 报告.

String jasperFileName = "C:\\TestReportProcess.jasper";映射<字符串,对象>params = new HashMap();params.put("List", getMyListOfString());JasperPrint jprint = (JasperPrint) asperFillManager.fillReport(jasperFileName, params, new JREmptyDataSource());

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

item 1,第 2 项,第 3 项,第 4 项,等等等等

如何避免?

我的碧玉报告xml

 <细节><band height="280" splitType="Stretch"><textField isStretchWithOverflow="true" pattern=""><reportElement x="0" y="13" width="550" height="45" uuid="f907894e-e9f1-418b-9ab8-1db276b8482e"/><文本元素><font fontName="Antique Olive Compact"/></textElement><textFieldExpression><![CDATA[$P{List}]]></textFieldExpression></textField></细节></jasperReport>

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

解决方案

你可以通过多种方式传递List:

  1. 传递List作为参数并在subDataset中使用这个参数(在dataSourceExpression中)莉>
  2. I'm passing an List of String to JasperReports report via parameter.

    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());
    

    when report starting it shows comma every item

    item 1,
    item 2,
    item 3,
    item 4,
    etc etc 
    

    How can avoid it?

    My jasper report 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>
    

    This is my simple xml report, there is just a parameter as java.util.Arraylist

    解决方案

    You can pass the List<String> in several ways:

    1. Passing List<String> as parameter and use this parameter in subDataset (in dataSourceExpression)
    2. Passing List<String> as JRDataSource with help of JRBeanCollectionDataSource
    3. Converting List<String> to the String object and replace comma into carriage return (\n) or just remove comma with help of String.replace method.

    Example

    The example shows both approaches

    Java code

    We populating listOfItems parameter with List<String> and fill report with 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);
    

    Report template

    The datasource contains 6 items (elements) for showing in Detail band (main dataset).

    Parameter listOfItems contains the list of 3 elements to show in Title band with help of subDataset of Table component.

    The Summary band is used to show how to show data from List<String> (listOfItems parameter) with just one textField element.

    The fieldDescription help us to get the field's value. With help of _THIS keyword we getting the String value.

    <?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>
    

    The usage of List.toString method gives result like this: [val1, val2] - values separated with comma and enclosed in square brackets. The usage of String.replace method (several serial calling of this method) give us nice results.

    Output result

    The generated pdf file with help of JRPdfExporter looks like:

    这篇关于传递 ArrayList 报告显示每个项目的逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆