Search code examples
jasper-reportscrosstab

How to print the values from a table into one by one value horizontally in jasper report?


I have data from DB as below.

Subject                         Mark   Grade
---------
COMPUTER SCIENCE               88.00 A2   
TAMIL                          88.00 A2   
HINDI                          55.00 C1   
MATHS                          54.00 C1   
SCIENCE                        77.00 B1   
ENGLISH                        45.00 C2   

Using Jasper Report I have to print it like this:

Subject     COMPUTER SCIENCE   Tamil    HINDI    MATHS   SCIENCE   ENGLISH
GRADE           A2              A2       C1       C1       B1        C2

I put everything in detail band, but it prints like this

Subject     COMPUTER SCIENCE   COMPUTER SCIENCE     COMPUTER SCIENCE  
GRADE           A2                    A2                  A2                              

Same repeated for all the subject, hence it prints 6 times into separate detail band for all subject, but I want it to be

Subject     COMPUTER SCIENCE     TAMIL    HINDI                                    
GRADE           A2                 A2     C1

Please do some suggestions? I have tried with column count, report count, expression, but with no result


Solution

  • This is achieved using crosstab, in your example there is no rowGroup so we need to generate a dummy rowGroup on fixed String "Grade", the columnGroup is on $F{Subject}

    Example

    <?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="grades" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="407ecd26-3880-4351-b7f4-4422fa5adc32">
        <style name="Crosstab Data Text" hAlign="Center"/>
        <queryString>
            <![CDATA[]]>
        </queryString>
        <field name="Subject" class="java.lang.String"/>
        <field name="Mark" class="java.lang.String"/>
        <field name="Grade" class="java.lang.String"/>
        <summary>
            <band height="50">
                <crosstab>
                    <reportElement x="0" y="0" width="555" height="50" uuid="c15de662-be8e-4db4-acef-1b71c3201725"/>
                    <crosstabHeaderCell>
                        <cellContents>
                            <staticText>
                                <reportElement style="Crosstab Data Text" x="0" y="0" width="70" height="30" uuid="07cab0ef-f928-4681-86ff-0c38656cdd31"/>
                                <box>
                                    <pen lineWidth="0.5"/>
                                    <topPen lineWidth="0.5"/>
                                    <leftPen lineWidth="0.5"/>
                                    <bottomPen lineWidth="0.5"/>
                                    <rightPen lineWidth="0.5"/>
                                </box>
                                <textElement verticalAlignment="Middle"/>
                                <text><![CDATA[Subject]]></text>
                            </staticText>
                        </cellContents>
                    </crosstabHeaderCell>
                    <rowGroup name="dummy" width="70">
                        <bucket class="java.lang.String">
                            <bucketExpression><![CDATA["GRADE"]]></bucketExpression>
                        </bucket>
                        <crosstabRowHeader>
                            <cellContents backcolor="#F0F8FF" mode="Opaque">
                                <box>
                                    <pen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                                </box>
                                <textField>
                                    <reportElement style="Crosstab Data Text" x="0" y="0" width="70" height="25" uuid="0ec80ead-fa81-4933-b56b-445e14578f6b"/>
                                    <textElement verticalAlignment="Middle"/>
                                    <textFieldExpression><![CDATA[$V{dummy}]]></textFieldExpression>
                                </textField>
                            </cellContents>
                        </crosstabRowHeader>
                        <crosstabTotalRowHeader>
                            <cellContents/>
                        </crosstabTotalRowHeader>
                    </rowGroup>
                    <columnGroup name="Subject" height="30">
                        <bucket class="java.lang.String">
                            <bucketExpression><![CDATA[$F{Subject}]]></bucketExpression>
                        </bucket>
                        <crosstabColumnHeader>
                            <cellContents backcolor="#F0F8FF" mode="Opaque">
                                <box>
                                    <pen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                                </box>
                                <textField>
                                    <reportElement style="Crosstab Data Text" x="0" y="0" width="62" height="30" uuid="304f31c2-426d-4177-aa32-bb7f6694cd6c"/>
                                    <textElement verticalAlignment="Middle"/>
                                    <textFieldExpression><![CDATA[$V{Subject}]]></textFieldExpression>
                                </textField>
                            </cellContents>
                        </crosstabColumnHeader>
                        <crosstabTotalColumnHeader>
                            <cellContents/>
                        </crosstabTotalColumnHeader>
                    </columnGroup>
                    <measure name="GradeMeasure" class="java.lang.String">
                        <measureExpression><![CDATA[$F{Grade}]]></measureExpression>
                    </measure>
                    <crosstabCell width="62" height="25">
                        <cellContents>
                            <box>
                                <pen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                            </box>
                            <textField>
                                <reportElement style="Crosstab Data Text" x="0" y="0" width="62" height="25" uuid="b10eac33-96fb-4d5d-8c3f-212c72192a3c"/>
                                <textElement verticalAlignment="Middle"/>
                                <textFieldExpression><![CDATA[$V{GradeMeasure}]]></textFieldExpression>
                            </textField>
                        </cellContents>
                    </crosstabCell>
                    <crosstabCell height="25" rowTotalGroup="dummy">
                        <cellContents backcolor="#BFE1FF" mode="Opaque">
                            <box>
                                <pen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                            </box>
                            <textField>
                                <reportElement style="Crosstab Data Text" x="0" y="0" width="50" height="25" uuid="26fc7a85-7738-45e8-81ff-e076bb51d3fd"/>
                                <textFieldExpression><![CDATA[$V{GradeMeasure}]]></textFieldExpression>
                            </textField>
                        </cellContents>
                    </crosstabCell>
                    <crosstabCell width="50" columnTotalGroup="Subject">
                        <cellContents backcolor="#BFE1FF" mode="Opaque">
                            <box>
                                <pen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                            </box>
                            <textField>
                                <reportElement style="Crosstab Data Text" x="0" y="0" width="50" height="25" uuid="82d05499-1fe2-4a97-ad82-23fa974759c5"/>
                                <textFieldExpression><![CDATA[$V{GradeMeasure}]]></textFieldExpression>
                            </textField>
                        </cellContents>
                    </crosstabCell>
                    <crosstabCell rowTotalGroup="dummy" columnTotalGroup="Subject">
                        <cellContents backcolor="#BFE1FF" mode="Opaque">
                            <box>
                                <pen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                            </box>
                            <textField>
                                <reportElement style="Crosstab Data Text" x="0" y="0" width="50" height="25" uuid="c6e4a9d8-6c74-4dcc-8617-18ad14e60890"/>
                                <textFieldExpression><![CDATA[$V{GradeMeasure}]]></textFieldExpression>
                            </textField>
                        </cellContents>
                    </crosstabCell>
                </crosstab>
            </band>
        </summary>
    </jasperReport>
    

    Result

    Output