Hi folks,
In this tip, you would see how to convert sql rows into report columns.
By default when you put text fields in detail band, you could see the report prints the query output in vertical order(row by row). There are some situations when your query results definite number of rows in output and need them to be displayed as horizontal view.
In this case, usually one might think of cross tab implementation. This is fine upto some extent but working with cross tab is not always developer friendly hence instead of using it one can split the report into N number columns in reports properties and provide desired width for columns and then pull your fields one below to other. In the report advanced properties change Print Order property from Vertical to Horizontal and this would result converting rows output into columns that would look like a cross tab implementation.
Design:
Query format/Sample query output
Sample output:
JRXML: (Developed using 6.4.2 professional - it should also works in community[not tested])
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.4.2.final using JasperReports Library version 6.4.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="columns" columnCount="5" printOrder="Horizontal" pageWidth="650" pageHeight="842" columnWidth="120" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" whenResourceMissingType="Error" uuid="adca597a-f8d3-4f9f-ae19-21b3272acdb9">
<property name="com.jaspersoft.studio.unit." value="pixel"/>
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="foodmart"/>
<property name="com.jaspersoft.studio.data.sql.tables" value=""/>
<queryString>
<![CDATA[(SELECT 100 customerID, 'Sadakar' CustomerName, 'Hyd' CustomerCity FROM customer limit 1)
UNION ALL
(SELECT 101 customerID, 'Hasini' CustomerName, 'MNCL' CustomerCity FROM customer limit 1)
UNION ALL
(SELECT 102 customerID, 'Venkat' CustomerName, 'Banglore' CustomerCity FROM customer limit 1)
UNION ALL
(SELECT 103 customerID, 'Robert' CustomerName, 'Chenni' CustomerCity FROM customer limit 1)
UNION ALL
(SELECT 104 customerID, 'Soujanya' CustomerName, 'MMR' CustomerCity FROM customer limit 1)]]>
</queryString>
<field name="customerid" class="java.lang.Integer"/>
<field name="customername" class="java.lang.String"/>
<field name="customercity" class="java.lang.String"/>
<pageHeader>
<band height="46" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="610" height="46" uuid="217244d6-5976-4298-bf74-d5e93099b5c7"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="16"/>
</textElement>
<textFieldExpression><![CDATA["Horizontal display of definite number of rows output like a cross tab view"]]></textFieldExpression>
</textField>
</band>
</pageHeader>
<detail>
<band height="93" splitType="Stretch">
<textField>
<reportElement x="0" y="3" width="100" height="30" uuid="160c2caa-87e8-4757-8b6d-7ff9daad46e8"/>
<textFieldExpression><![CDATA[$F{customerid}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="0" y="33" width="100" height="30" uuid="91ef78c5-4eae-43a4-9f64-8a6848961018"/>
<textFieldExpression><![CDATA[$F{customername}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="0" y="63" width="100" height="30" uuid="f3c5ce61-e141-409e-8b21-86d4cdbfd7e5"/>
<textFieldExpression><![CDATA[$F{customercity}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
No comments:
Post a Comment