Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

java - Passing an ArrayList report shows comma every item

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 ( ) 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(", ", "
").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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...