Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing an ArrayList report shows comma every item

Tags:

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

like image 839
Massimo Avatar asked Oct 27 '16 09:10

Massimo


1 Answers

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:

Generated pdf

like image 162
Alex K Avatar answered Sep 23 '22 17:09

Alex K