Split a field on the basis of delimiter and print all the strings

0

Suppose I have got a dataset from query as-

ColumnA ColumnB
ColumnC  
ColumnD
A1 B1 C1

D11,D21,D31

A2 B2 C2

D21,D22,D32

 

I am trying to split it like following-

ColumnA ColumnB ColumnC ColumnD
A1 B1 C1 D11
A1 B1 C1 D21
A1 B1 C1 D31
A2 B2 C2 D21
A2 B2 C2 D22
A2 B2 C2 D32

Can anyone suggest anything.I have tried $F{ColumnD}.split(",") but that returns an array of string ,which I dont know how to traverse in Jasper.RThe length of ColumnD is dynamic.

sneha.sinha_1's picture
Joined: Jun 6 2019 - 2:58am
Last seen: 1 month 2 days ago

2 Answers:

0

What database are you using ?

Can't you do this in the SQL statement ? Most probably SQL and your database will be faster then JasperSoft and Java (or Groovy).

Filip C.'s picture
Joined: May 15 2011 - 11:51pm
Last seen: 2 weeks 2 days ago
0

You could 1. create an Array of Strings based on your Field:

$F{ColumnD}.split(",")

and 2. create an ArrayList of that:

Arrays.asList($F{ColumnD}.split(",")

and 3. use this list for an ListElement or even a SubReport as a JRBeanCollectionDataSource using finally this as DataSourceExpression:

new JRBeanCollectionDataSource(Arrays.asList($F{ColumnD}.split(",") ))

in your List or SubReport you must create a Field named "_THIS" of type String that you can place within the Listelement or the detail of your SubReport.

just tried it out with following parameter in a new empty (main) record:

<parameter name="MyStrings" class="java.util.List" nestedType="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA[Arrays.asList("Record1", "2. Record","and the third one")]]></defaultValueExpression>
    </parameter>

and this for SubReport expression:

<subreport>
                <reportElement x="0" y="10" width="545" height="60" uuid="7f7e06fb-1788-4fdc-a1a3-3f5dd3745533"/>
                <dataSourceExpression><![CDATA[new JRBeanCollectionDataSource($P{MyStrings})]]></dataSourceExpression>
                <subreportExpression><![CDATA["StringListAsDataSource_Sub.jasper"]]></subreportExpression>
            </subreport>

and this as whole SubReport:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.5.1  -->
<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="StringListAsDataSource_Sub" pageWidth="595" pageHeight="842" columnWidth="595" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="9d9ea5fd-fadc-4890-827f-369ee64374e3">
    <property name="com.jaspersoft.studio.unit." value="pixel"/>
    <property name="com.jaspersoft.studio.unit.pageHeight" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.pageWidth" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.topMargin" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.bottomMargin" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.leftMargin" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.rightMargin" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.columnWidth" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.columnSpacing" value="pixel"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="_THIS" class="java.lang.String"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <detail>
        <band height="30" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="440" height="30" uuid="2beea897-e04d-4bc2-aa50-1238ad9aef85">
                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="837e5d94-d9e1-4f14-af77-3dc9369ce311"/>
                </reportElement>
                <textFieldExpression><![CDATA[$F{_THIS}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

hth + regards

C-Box

C-Box's picture
1622
Joined: Jul 19 2006 - 5:58pm
Last seen: 4 days 9 hours ago
Feedback
randomness