Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge cells in same column, apply rowspan?

In jrxml design for my report, I have a column in my detail section that is printing same thing for a specific group. For Example, if i have a country column USA and the next column contains its states. Country USA repeats for each entry . So i need to merge the country cells has USA value. I need to merge all the cells of column has same value and should display it as a single cell and it should align its text vertically and horizontally center. Anybody help me how can i achieve this by using jrxml?

Everything is in the Detail band.

I am working to get a report like in the below picture

enter image description here


UPDATED:

From Peter's answer I have one doubt, if my xml is like:

<report>
   <row1>
    <country>INDIA</country>
    <state>Haryana</state>
   </row1>
   <row2>
    <country>INDIA</country>
    <state>Punjab</state>
   </row2>
   <row3>
     <country>INDIA</country>
     <state>Maharashtra</state>
   </row3>
</report>

How can I create the report if my xml is like above?

like image 970
Tinoy Jameson Avatar asked Dec 09 '15 06:12

Tinoy Jameson


1 Answers

You can fairly easy achieve text vertically aligned at top by using isPrintRepeatedValues="false", setting borders correctly (only top, using empty cell with only left, adding line to columnFooter).

Top alignment result

To achieve "text vertically aligned at center" use a subreport for the other columns and set stretchType="RelativeToBandHeight" on your rowspan column.

Note you need in this case change your datasource (main report, country by country, subreport all states relative to country)

Align center

EDIT: Comment: This not works in detail band. Petter – @Tinoy Malayil.

I include a runnable example for text vertically aligned at center:

Datasource xml:

<report>
    <country>
        <name>INDIA</name>
        <states>
            <state>Haryana</state>
            <state>Punjab</state>
            <state>Maharashtra</state>
            <state>Karnataka</state>
            <state>TamilNadu</state>
        </states>
    </country>
    <country>
        <name>USA</name>
        <states>
            <state>Alabama</state>
            <state>Washington</state>
            <state>Alaska</state>
            <state>Texas</state>
        </states>
    </country>
</report>

Main report:, country.jrxml

<?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="Country" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="30" bottomMargin="30" uuid="dbc44bea-4f8e-4072-9c94-8442f3093aa0">
<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
    <defaultValueExpression><![CDATA["C:\\fullPath\\to\\Your\\subreport\\"]]></defaultValueExpression>
</parameter>
<queryString language="xPath">
    <![CDATA[report/country]]>
</queryString>
<field name="name" class="java.lang.String">
    <fieldDescription><![CDATA[name]]></fieldDescription>
</field>
<background>
    <band/>
</background>
<columnHeader>
    <band height="20">
        <staticText>
            <reportElement x="0" y="0" width="177" height="20" uuid="d4eb7868-2f74-4713-abca-a176c47927e1"/>
            <box>
                <pen lineWidth="0.25"/>
                <topPen lineWidth="0.25"/>
                <leftPen lineWidth="0.25"/>
                <bottomPen lineWidth="0.25"/>
                <rightPen lineWidth="0.25"/>
            </box>
            <textElement textAlignment="Center" verticalAlignment="Middle"/>
            <text><![CDATA[COUNTRY]]></text>
        </staticText>
        <staticText>
            <reportElement x="177" y="0" width="200" height="20" uuid="98cbcff7-6b24-43bd-a2df-39cc07e56487"/>
            <box>
                <pen lineWidth="0.25"/>
                <topPen lineWidth="0.25"/>
                <leftPen lineWidth="0.25"/>
                <bottomPen lineWidth="0.25"/>
                <rightPen lineWidth="0.25"/>
            </box>
            <textElement textAlignment="Center" verticalAlignment="Middle"/>
            <text><![CDATA[STATE]]></text>
        </staticText>
    </band>
</columnHeader>
<detail>
    <band height="20">
        <textField>
            <reportElement stretchType="RelativeToBandHeight" x="0" y="0" width="177" height="20" uuid="1bbab3e7-f8a3-48c9-b28e-2a6d2a68b755"/>
            <box topPadding="0" leftPadding="0">
                <pen lineWidth="0.25"/>
                <topPen lineWidth="0.25"/>
                <leftPen lineWidth="0.25"/>
                <bottomPen lineWidth="0.25"/>
                <rightPen lineWidth="0.25"/>
            </box>
            <textElement textAlignment="Center" verticalAlignment="Middle"/>
            <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
        </textField>
        <subreport>
            <reportElement x="177" y="0" width="200" height="20" uuid="6314908a-006d-4a5b-9137-a056eb205529"/>
            <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("/country/states/state")]]></dataSourceExpression>
            <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "country_subreport.jasper"]]></subreportExpression>
        </subreport>
    </band>
  </detail>
</jasperReport>

subreport, country_subreport.jrxml

<?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="country_subreport" language="java" pageWidth="200" pageHeight="500" columnWidth="200" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="c9795fb7-39e0-4aa6-8926-2f019c4af84e">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<queryString language="xPath">
    <![CDATA[/report/country/states/state]]>
</queryString>
<field name="state" class="java.lang.String">
    <fieldDescription><![CDATA[child::text()]]></fieldDescription>
</field>
<detail>
    <band height="20" splitType="Stretch">
        <textField>
            <reportElement x="0" y="0" width="200" height="20" uuid="dc0a9dda-b940-4752-ad91-31420c4ce729"/>
            <box topPadding="2" leftPadding="2">
                <pen lineWidth="0.25"/>
                <topPen lineWidth="0.25"/>
                <leftPen lineWidth="0.25"/>
                <bottomPen lineWidth="0.25"/>
                <rightPen lineWidth="0.25"/>
            </box>
            <textElement verticalAlignment="Middle"/>
            <textFieldExpression><![CDATA[$F{state}]]></textFieldExpression>
        </textField>
    </band>
</detail>

like image 106
Petter Friberg Avatar answered Nov 15 '22 11:11

Petter Friberg