Converting jrxml file to csv file: Leading zeros getting truncated - jasper-reports

I have a conversion from jrxml to pdf. I am sending the data as 001 in the DTO but while displaying the same in the Excel it is becoming as 1. Below is the code that is been used for displaying the data.
Code:
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement style="table 5_CH" stretchType="RelativeToTallestObject" x="0" y="0" width="71" height="26" backcolor="#FFFFFF">
<property name="class" value="sortable"/>
</reportElement>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font pdfFontName="Helvetica"/>
<paragraph lineSpacing="Single" firstLineIndent="3" spacingAfter="5"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$F{reference}]]>
</textFieldExpression>
</textField>
I need to show the leading zeros also. So pls let me know how to fix this issue.

You are using the MS Excel application for viewing csv file. The csv is just a plain-text format, it does not contain information "how to show data". The view of this sort of data depends on your viewer only. Therefore you can try to use JRXlsExporter or JExcelApiExporter or JRXlsxExporter exporters - for constructing report in Excel format (xls or xlsx).
Let's look at an example. We take csv file as datasource.
The csv file data (excel_data.csv):
number_as_string,date,number_as_int
001,2013-09-23,1
002,2013-09-22,3
3,2013-09-21,4
004,2013-09-20,5
The configuration of datasource in iReport will be:
We will use the net.sf.jasperreports.export.xls.pattern and net.sf.jasperreports.export.xls.detect.cell.type properties in sample for getting information in format we want.
Our template will be:
<?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="test_excel_formatting" language="groovy" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="802" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="952b781d-520d-4a76-aa59-f635b8759fbe">
<property name="net.sf.jasperreports.export.xls.detect.cell.type" value="true"/>
<queryString>
<![CDATA[]]>
</queryString>
<field name="number_as_string" class="java.lang.String"/>
<field name="date" class="java.util.Date"/>
<field name="number_as_int" class="java.lang.Integer"/>
<columnHeader>
<band height="42" splitType="Stretch">
<staticText>
<reportElement uuid="667b6ca6-bf50-404e-8ef1-c654456ed8e4" stretchType="RelativeToTallestObject" mode="Opaque" x="0" y="0" width="88" height="42" backcolor="#FF99CC"/>
<box>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Number as String, without pattern]]></text>
</staticText>
<staticText>
<reportElement uuid="080a5cc0-fa66-4b11-a3d3-f0b8a4e69204" stretchType="RelativeToTallestObject" mode="Opaque" x="88" y="0" width="88" height="42" backcolor="#9999FF"/>
<box>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Number as Int, without pattern]]></text>
</staticText>
<staticText>
<reportElement uuid="d1fc6219-2e33-4cc9-b4fe-4ac2b5365102" stretchType="RelativeToTallestObject" mode="Opaque" x="176" y="0" width="104" height="42" backcolor="#99FFCC"/>
<box>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Date without pattern]]></text>
</staticText>
<staticText>
<reportElement uuid="991837d7-a1ab-44ed-8177-756cfae9d369" stretchType="RelativeToTallestObject" mode="Opaque" x="280" y="0" width="104" height="42" backcolor="#9999FF"/>
<box>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Number as Int with common pattern]]></text>
</staticText>
<staticText>
<reportElement uuid="d38154af-2017-4e10-a176-4d66e5cc9f96" stretchType="RelativeToTallestObject" mode="Opaque" x="384" y="0" width="104" height="42" backcolor="#9999FF"/>
<box>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Number as Int with xls pattern]]></text>
</staticText>
<staticText>
<reportElement uuid="17f57019-8691-4643-abdb-ab15afe63f36" stretchType="RelativeToTallestObject" mode="Opaque" x="488" y="0" width="104" height="42" backcolor="#9999FF"/>
<box>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Number as Int with common and xls pattern]]></text>
</staticText>
<staticText>
<reportElement uuid="f4f506bf-9511-4e1e-805b-8752a98e91ea" stretchType="RelativeToTallestObject" mode="Opaque" x="592" y="0" width="104" height="42" backcolor="#99FFCC"/>
<box>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Date with common pattern]]></text>
</staticText>
<staticText>
<reportElement uuid="dffea96e-1dfa-47e0-8551-dd417c25ecbd" stretchType="RelativeToTallestObject" mode="Opaque" x="696" y="0" width="104" height="42" backcolor="#99FFCC"/>
<box>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Date with common pattern]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement uuid="3d11d568-6e2c-4373-af7d-fa79b99a2908" mode="Opaque" x="0" y="0" width="88" height="20" backcolor="#FF99CC"/>
<box>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{number_as_string}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="8b8f968e-ca6d-44d0-bc2e-50a4773d0a6f" mode="Opaque" x="88" y="0" width="88" height="20" backcolor="#9999FF"/>
<box>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{number_as_int}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="943a468b-491d-4955-b0c0-58d550536234" mode="Opaque" x="176" y="0" width="104" height="20" backcolor="#99FFCC"/>
<box>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{date}]]></textFieldExpression>
</textField>
<textField pattern="000000">
<reportElement uuid="52a26b66-1ef5-4f77-a4a2-900a606a1171" mode="Opaque" x="280" y="0" width="104" height="20" backcolor="#9999FF"/>
<box>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{number_as_int}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="c26be152-fae4-4a85-ae55-c70e27739a87" mode="Opaque" x="384" y="0" width="104" height="20" backcolor="#9999FF">
<property name="net.sf.jasperreports.export.xls.pattern" value="00000000000000"/>
</reportElement>
<box>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{number_as_int}]]></textFieldExpression>
</textField>
<textField pattern="00000">
<reportElement uuid="fab0fba1-1718-479d-841b-6cbaf1963a0b" mode="Opaque" x="488" y="0" width="104" height="20" backcolor="#9999FF">
<property name="net.sf.jasperreports.export.xls.pattern" value="00000000000000"/>
</reportElement>
<box>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{number_as_int}]]></textFieldExpression>
</textField>
<textField pattern="dd-MMM-yyyy">
<reportElement uuid="07552f94-56c8-413d-8dd8-5b5c667c81cf" mode="Opaque" x="592" y="0" width="104" height="20" backcolor="#99FFCC"/>
<box>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{date}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="844449f9-1578-4729-b911-18bdd17f8cd4" mode="Opaque" x="696" y="0" width="104" height="20" backcolor="#99FFCC">
<property name="net.sf.jasperreports.export.xls.pattern" value="dd mmmm yyyy"/>
</reportElement>
<box>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{date}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
The report's design in iReport looks like this:
In example I've set report's property net.sf.jasperreports.export.xls.detect.cell.type as true.
I've put the number_as_string field without any patterns into the first column (pink color).
I've put the number_as_int field into the columns number 2, 4, 5, 6 (blue color).
The column #2 does not contain any pattern.
The column #4 does contains common textFields element's pattern attribute. I've used the 000000 value.
For column #5 I've set net.sf.jasperreports.export.xls.pattern property. I've set 00000000000000 value for it.
And the column #6 is a mixed variant - I've set net.sf.jasperreports.export.xls.pattern property and the textFields element's pattern.
I've put the date field into the columns number 3, 7, 8 (green color).
The column #3 does not contain any pattern. The default pattern will be used.
The column #7 does contains common textFields element's pattern attribute. I've used the dd-MMM-yyyy value.
For column #8 I've set net.sf.jasperreports.export.xls.pattern property. I've set dd mmmm yyyy value for it.
The result (generated file in Excel format) will be:
Notes:
As you can see the first column contains data in original format.
The column #6 contains data in format set with help of net.sf.jasperreports.export.xls.pattern property. This property is "stronger" than the textFields element's pattern attribute.
The Excel file was generated with help of iReport 5.2.0 with XLS Preview, XLS Preview (Using JExcel API) and Excel 2007 (XLSX) Preview options - the result was the same.

I figured the answer after lot of googling. I am sending the data as String but still it is truncating the leading zeroes.
The problem is, excel is taking this input as a number and truncating the leading zeros.
In order to stop excel from doing this, prefix the data with '=' and append our data in double quotes.
i.e, the input which I am sending is 001 which should be converted as "=" + "\"" + "001" + "\"" so it becomes ="001" then in excel it gets to know that the given one is a text and it is not going to truncate the leading zeroes.
Addendum:
I even had trouble while displaying the date. The requirement is, date should be displayed as 04 Apr 2013 but if I send the same to excel it is converting it to 04-Apr-2013 which is not acceptable in my case.
The same logic above holds good here too,
just append '=' to the input data. Say "=" + "\"" + 04 Apr 2013 + "\"" so it becomes
="04 Apr 2013". Now the date is taken as a textand is displayed as it is.
Hoping this will help someone .. :)

Related

Sum columns based on custom group (Jasper Reports) [duplicate]

First of all, the records are shown in the table by table component but not in the report one.
The results looks like this:
YEARS MONTHS SUMMONTH SUMQUARTER
----- ------ -------- ----------
2009 Jan 130984 432041
Feb 146503
Mar 154554
Apr 147917 435150
May 131822
Jun 155411
Jul 144000 424806
Aug 130369
Sep 150437
Oct 112137 400114
Nov 152057
Dec 135920
=====================================
Jan-Dec 1692111
=====================================
2010 Jan 139927 417564
Feb 154940
Mar 122697
Apr 163257 413305
May 124999
Jun 125049
Jul 145127 427612
Aug 138804
Sep 143681
Oct 143398 406381
Nov 125351
Dec 137632
=====================================
Jan-Dec 1664862
=====================================
The sumquarter column shows the sum of each quarter in year.
They are not printed when it repeated the value of the field.
The question is how to group the column of sumquarter, so that the first printed repeated value in each row to join the next repeated value to become a single cell until it meets the non-repeated value?
You can simply see it in an image.
Below is the image that the table shows and the solution that I preferred is to group those 3 months of sum into a single cell.
Here is the image:
You can use this sample:
<?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="year_sum_quarter" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<queryString>
<![CDATA[]]>
</queryString>
<field name="year" class="java.lang.Integer"/>
<field name="month" class="java.lang.String"/>
<field name="sum" class="java.lang.Integer"/>
<field name="q" class="java.lang.Integer"/>
<variable name="yearSum" class="java.lang.Integer" resetType="Group" resetGroup="yearGroup" calculation="Sum">
<variableExpression><![CDATA[$F{sum}]]></variableExpression>
</variable>
<variable name="qSum" class="java.lang.Integer" resetType="Group" resetGroup="quaterGroup" calculation="Sum">
<variableExpression><![CDATA[$F{sum}]]></variableExpression>
</variable>
<group name="yearGroup">
<groupExpression><![CDATA[$F{year}]]></groupExpression>
<groupFooter>
<band height="20">
<textField>
<reportElement x="100" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="0.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="0.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA["Jan-Dec, " + $F{year}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="200" y="0" width="100" height="20"/>
<box leftPadding="0">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="0.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="0.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$V{yearSum}]]></textFieldExpression>
</textField>
<staticText>
<reportElement x="0" y="0" width="100" height="20"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="0.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[]]></text>
</staticText>
<staticText>
<reportElement x="300" y="0" width="100" height="20"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="0.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[]]></text>
</staticText>
</band>
</groupFooter>
</group>
<group name="quaterGroup">
<groupExpression><![CDATA[$F{year} + $F{q}]]></groupExpression>
</group>
<columnHeader>
<band height="50">
<staticText>
<reportElement x="100" y="30" width="100" height="20"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[Month]]></text>
</staticText>
<staticText>
<reportElement x="0" y="30" width="100" height="20"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[Year]]></text>
</staticText>
<staticText>
<reportElement x="200" y="30" width="100" height="20"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[Month Sum]]></text>
</staticText>
<staticText>
<reportElement x="300" y="30" width="100" height="20"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[Quarter Sum]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<staticText>
<reportElement x="300" y="0" width="100" height="20"/>
<box>
<topPen lineWidth="0.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="0.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[]]></text>
</staticText>
<staticText>
<reportElement x="0" y="0" width="100" height="20"/>
<box>
<topPen lineWidth="0.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="0.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[]]></text>
</staticText>
<textField>
<reportElement x="0" y="0" width="100" height="20">
<printWhenExpression><![CDATA[$V{yearGroup_COUNT} == 1]]></printWhenExpression>
</reportElement>
<box leftPadding="10">
<topPen lineWidth="0.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="0.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{year}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="200" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{sum}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="100" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{month}]]></textFieldExpression>
</textField>
<textField evaluationTime="Group" evaluationGroup="quaterGroup" isBlankWhenNull="false">
<reportElement stretchType="RelativeToBandHeight" isPrintRepeatedValues="false" x="300" y="0" width="100" height="20" printWhenGroupChanges="quaterGroup">
<printWhenExpression><![CDATA[$V{quaterGroup_COUNT} == 1]]></printWhenExpression>
</reportElement>
<box leftPadding="10">
<topPen lineWidth="0.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="0.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$V{qSum}]]></textFieldExpression>
</textField>
<line>
<reportElement x="300" y="0" width="100" height="1" printWhenGroupChanges="quaterGroup">
<printWhenExpression><![CDATA[$V{quaterGroup_COUNT} == 1]]></printWhenExpression>
</reportElement>
</line>
</band>
</detail>
</jasperReport>
The result will be (in pdf format):
In this sample I've used two elements in the Detail band for the Year column:
one textField with only vertical borders and with printWhenExpression: "$V{yearGroup_COUNT} == 1" property (I'm show it only once for the whole yearGroup) and one staticText without any text and with only vertical borders.
I've used three elements in the Detail band for the Quarter Sum column:
one textField with only vertical borders and with printWhenExpression: "$V{quaterGroup_COUNT} == 1" property (I'm show it only once for the whole quaterGroup), one staticText without any text and with only vertical borders and the line element for drawing horizontal border with printWhenExpression: "$V{quaterGroup_COUNT} == 1" property.

Merging the cells which have same data

I have a report which has the columns Type,S.No,Date.Here the Type column will have the same value for all rows.So i just want to merge all the cells of Type column.I have referred the below link
Group several same value field into a single cell
and tried with their suggestion.But if i do like that i am getting like in below image.
Edit:
Below is the code i am using in my jrxml for merging the cells which have same data.
<field name="type" class="java.lang.String"/>
<group name="type">
<groupExpression><![CDATA[$F{type}]]></groupExpression>
</group>
In detail band i tried by creating the fake statictext behind the type textfield like below.
<staticText>
<reportElement x="0" y="0" width="121" height="20"/>
<box>
<leftPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement/>
<text><![CDATA[]]></text>
</staticText>
<textField>
<reportElement isPrintRepeatedValues="false" x="0" y="0" width="121" height="20"/>
<box>
<topPen lineWidth="0.0"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.0"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{type}]]></textFieldExpression>
</textField>
But no use.
Tried by using printWhenExpression in type textfield like below.
<textField>
<reportElement x="0" y="0" width="121" height="20">
<printWhenExpression><![CDATA[$V{type_COUNT} == 1]]></printWhenExpression>
</reportElement>
<box>
<topPen lineWidth="0.0"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.0"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{type}]]></textFieldExpression>
</textField>
But no use.
If i add the bottom border for static text then the image looks like below.
Thank You.
Add the bottom border in GroupFooter band for group "type"

How to avoid using a lots of ternary operators for translation of value to text

I am using JasperReports to create some reports of a Django web application.
Let's say that I have a model that has an id and a value. This value is a Django choice so I end up in my database with only the keys and not the values (the values are in my code). To make it more clear for people that don't use Django, I end up with something like that in my database:
id value
1 'GD'
2 'VG'
3 'VG'
4 'VG'
5 'GD'
6 'AV'
7 'GD'
8 'AV'
I want to display Good instead of 'GD', Average instead of 'AV' and Very Good instead of VG in my report. I know that this can be done with two equally not desirable to me options:
Create a new table in my database that has key - value and join that in the JR query. I really hate this because I'd need to create around 10 such tables.
Use the ternary operator to display the correct value:
field.equals("GD")?"Good":(field.equals("AV")?"Average":(field.equals("VG")?"Very good":"-"))
I also hate this because it would be very complicated if I had, for instance, 10 key-value pairs.
My ideal solution would be to define a dictionary (HashMap) variable in my report that would contain all the key value pairs and then just do a DICTIONARY.get(field) to represent the field value. Can this be done ? Can you possibly propose another, better solution?
Please don't tell me to alter my database design, I know that some people won't like but it perfect for my needs.
You can solve this issue with help of Java. For example, the Guava library can help us in solving this task.
Using report's parameter
We can add the parameter of java.util.Map type and use it for extracting the value by the key (it can be the value field from your sample)
The sample:
<?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="using_map" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="e566b23d-ca97-472e-9cc3-8073573f4537">
<import value="com.google.common.collect.*"/>
<import value="com.google.common.base.*"/>
<parameter name="values" class="java.util.Map" isForPrompting="false">
<defaultValueExpression><![CDATA[new ImmutableMap.Builder<String, String>().put("GD", "Good").put("AV", "Average").build()]]></defaultValueExpression>
</parameter>
<queryString>
<![CDATA[]]>
</queryString>
<field name="id" class="java.lang.String"/>
<field name="value" class="java.lang.String"/>
<columnHeader>
<band height="20">
<staticText>
<reportElement uuid="f23f6abc-cd9b-4415-a591-cb7a51ad0392" x="0" y="0" width="100" height="20"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true" isUnderline="false"/>
</textElement>
<text><![CDATA[Id]]></text>
</staticText>
<staticText>
<reportElement uuid="f23f6abc-cd9b-4415-a591-cb7a51ad0392" x="100" y="0" width="100" height="20"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true" isUnderline="false"/>
</textElement>
<text><![CDATA[Value]]></text>
</staticText>
<staticText>
<reportElement uuid="f23f6abc-cd9b-4415-a591-cb7a51ad0392" x="200" y="0" width="100" height="20"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true" isUnderline="false"/>
</textElement>
<text><![CDATA[Value from Map]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement uuid="8eb8bbef-430b-4ad1-b592-000fe7ccce9f" x="0" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="8eb8bbef-430b-4ad1-b592-000fe7ccce9f" x="100" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{value}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="8eb8bbef-430b-4ad1-b592-000fe7ccce9f" x="200" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$P{values}.get($F{value})]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
In this sample we use ImmutableMap.Builder class for creating and filling the Map.
The result will be (via iReport preview):
As you can see that the third columns contains null value for values that are not in Map.
Note: Don't forget to add Guava library to classpath.
Using scriptlet
You can do the same with help of Scriptlets.
You can write the simple Java class with static method, for example for getting value from the Map.
Using internationalization mechanism
May be the simplest way is to use report's internationalization support.
You can attach the properties file with values to your report.
The sample of 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="localization" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" resourceBundle="marks" uuid="a5d74b61-8d62-41ac-b874-76d6f40da79e">
<queryString>
<![CDATA[]]>
</queryString>
<field name="id" class="java.lang.String"/>
<field name="value" class="java.lang.String"/>
<columnHeader>
<band height="20">
<staticText>
<reportElement uuid="f23f6abc-cd9b-4415-a591-cb7a51ad0392" x="0" y="0" width="100" height="20"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true" isUnderline="false"/>
</textElement>
<text><![CDATA[Id]]></text>
</staticText>
<staticText>
<reportElement uuid="f23f6abc-cd9b-4415-a591-cb7a51ad0392" x="100" y="0" width="100" height="20"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true" isUnderline="false"/>
</textElement>
<text><![CDATA[Value]]></text>
</staticText>
<staticText>
<reportElement uuid="f23f6abc-cd9b-4415-a591-cb7a51ad0392" x="200" y="0" width="114" height="20"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true" isUnderline="false"/>
</textElement>
<text><![CDATA[Value from Properties]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement uuid="8eb8bbef-430b-4ad1-b592-000fe7ccce9f" x="0" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="8eb8bbef-430b-4ad1-b592-000fe7ccce9f" x="100" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{value}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="8eb8bbef-430b-4ad1-b592-000fe7ccce9f" x="200" y="0" width="114" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[str($F{value})]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
The marks.properties file:
GD=Good
AV=Average
VG=Very Good
And the result will be (with iReport preview):
I've used the str() method and the $R{} syntax.

Removing the space when hiding the column

I am using JasperReports 4.5.0 to generate the reports. I am generating the reports in different formats like html, csv, pdf, xls, doc. In one of my report I have 6 columns say col1, col2, col3, col4, col5, col6. But based on the condition I want to hide the col4. For this I am using the printWhenExpression. Now it is hiding the column based on the condition but the problem is the space occupied by that column is remaining same.
How can I remove this empty space in between the col3 and col5?
The below code is for understanding how i am using it.
<staticText>
<reportElement x="500" y="0" width="0" height="20" isRemoveLineWhenBlank="true" isPrintWhenDetailOverflows="true">
<printWhenExpression><![CDATA[$P{routeType}==Boolean.FALSE]]></printWhenExpression>
</reportElement>
<box>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="12" isBold="true"/>
</textElement>
<text><![CDATA[Col4]]></text>
</staticText>
<textField isBlankWhenNull="true">
<reportElement x="500" y="0" width="0" height="20" isRemoveLineWhenBlank="true" isPrintWhenDetailOverflows="true">
<printWhenExpression><![CDATA[$P{routeType}==Boolean.FALSE]]></printWhenExpression>
</reportElement>
<box>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{col4}]]></textFieldExpression>
</textField>
This field will print only if the routeType value is false.Otherwise it should remove the column with empty space also.
You can use JasperReports API or DynamicJasper API
Here is the samples of using DynamicJasper API
You can use "fake" field (textField) for showing data of one column instead of showing two fields (two columns)
You can put another textField (which has the width of two textFields) under the both textFields. Using printWhenExpression property you can show two columns or one.
Here is the working sample.
Here is my report's design (in iReport):
My jrxml file:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport ..>
<parameter name="hideStreetColumn" class="java.lang.Boolean">
<defaultValueExpression><![CDATA[false]]></defaultValueExpression>
</parameter>
<queryString>
<![CDATA[SELECT id, street, city FROM address]]>
</queryString>
<field name="ID" class="java.lang.Integer"/>
<field name="STREET" class="java.lang.String"/>
<field name="CITY" class="java.lang.String"/>
<title>
<band height="79" splitType="Stretch">
<staticText>
<reportElement x="139" y="13" width="279" height="20"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[Sample of hiding column]]></text>
</staticText>
<textField>
<reportElement x="189" y="33" width="211" height="20"/>
<box leftPadding="10"/>
<textElement/>
<textFieldExpression><![CDATA["To hide Street column: " + $P{hideStreetColumn}]]></textFieldExpression>
</textField>
</band>
</title>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement x="100" y="0" width="200" height="20">
<printWhenExpression><![CDATA[$P{hideStreetColumn}]]></printWhenExpression>
</reportElement>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{CITY}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="0" y="0" width="100" height="20"/>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="100" y="0" width="100" height="20">
<printWhenExpression><![CDATA[!$P{hideStreetColumn}]]></printWhenExpression>
</reportElement>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{STREET}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="200" y="0" width="100" height="20">
<printWhenExpression><![CDATA[!$P{hideStreetColumn}]]></printWhenExpression>
</reportElement>
<box leftPadding="10">
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement/>
<textFieldExpression><![CDATA[$F{CITY}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
To show all three columns we should set hideStreetColumn parameter to false. The result will be:
To show only two columns (the Street column is hiding) we should set hideStreetColumn parameter to true. The result will be:
You can trick it with two detail bands. One detail band B1 with 6 columns and the second band B2 with 5 columns. Then in the band properties print B1 when printWhenExpression is false and B2 when printWhenExpression is true.

Hiding the field in jasper report

I am working with jasper report-4.5.0.I want to hide a field in jasper based on the flag value.Below is tha code which i am using in columnHeader band.
<staticText>
<reportElement x="365" y="0" width="100" height="20"/>
<box>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<text><![CDATA[TotalCharge]]></text>
</staticText>
and below code i am using in detail band.
<textField>
<reportElement x="365" y="0" width="100" height="20">
<printWhenExpression><![CDATA[!$P{dataType}]]></printWhenExpression>
</reportElement>
<box>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font size="8"/>
</textElement>
<textFieldExpression><![CDATA[$F{chargeTotal}]]></textFieldExpression>
</textField>
i am declaring the parameter in jrxml as
<parameter name="dataType" class="java.lang.Boolean"/>
the value to this parameter is coming from java class.If it is the false value i dont want to display this chargeTotal column.What i am doing wrong in my code.I am not getting.Any one can point me where i am doing wrong its urgent to me.
The code snippets you provided for the textfield look correct, but if you are wanting to hide the column header also you need to also add a printWhenExpression for it.
<staticText>
<reportElement x="365" y="0" width="100" height="20">
<printWhenExpression><![CDATA[$P{dataType}]]></printWhenExpression>
</reportElement>
<box>
<topPen lineWidth="0.25"/>
<leftPen lineWidth="0.25"/>
<bottomPen lineWidth="0.25"/>
<rightPen lineWidth="0.25"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<text><![CDATA[TotalCharge]]></text>
</staticText>
Edit: Actually, you have the conditional value backwards. If you want to show the column when dataType is true, your printWhenExpression needs to look like:
<printWhenExpression><![CDATA[$P{dataType}]]></printWhenExpression>