The report with several columns: How to reprint group headers in each column? - jasper-reports

I'm creating a report with 2 columns per page with JasperReports 4.7.1.
When the records belonging to a group span across multiple columns, I want to print the header not only at the beginning of the group but also at the start of each column.
Using Reprint Header (isReprintHeaderOnEachPage property) in group properties, header can be printed at the start of each page. But I still couldn't find a way to do the same at the start of a new column.
How can I achieve this?
Screenshot of the current report page...
What I want to achieve (note the headers in the 2nd column)...

I had a similar issue to yours and I managed to solve it by adding a ColumnHeader band and only displaying it for the 2nd column of my 2-column report.
It is important to have isReprintHeaderOnEachPage set to true and the height of the groupHeader and columnHeader to be the same for my solution to work.
Here is the relevant code (of course you replace "telephoneNumber" with your appropriate group expression:
<group name="telephoneNumberGroup" isStartNewPage="true" isReprintHeaderOnEachPage="true">
<groupExpression><![CDATA[$F{telephoneNumber}]]></groupExpression>
<groupHeader>
<band height="17">
<textField>
<reportElement positionType="Float" x="5" y="0" width="170" height="15" uuid="5e8b892b-f907-4907-9c6d-4419e57325e5"/>
<textElement verticalAlignment="Middle">
<font fontName="Arial" size="6" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{telephoneNumber}]]></textFieldExpression>
</textField>
</band>
</groupHeader>
</group>
<columnHeader>
<band height="17">
<printWhenExpression><![CDATA[$V{COLUMN_NUMBER} == 2]]></printWhenExpression>
<textField>
<reportElement positionType="Float" x="5" y="0" width="170" height="15" uuid="5e8b892b-f907-4907-9c6d-4419e57325e5"/>
<textElement verticalAlignment="Middle">
<font fontName="Arial" size="6" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{telephoneNumber}]]></textFieldExpression>
</textField>
</band>
</columnHeader>

Related

Jasper Report Freeze header of Excel report

I am using Jasper IReport designer version 3.7.6 . Is there any way to freeze the first two rows of the excel report that is generated? Below is a part of the column header. Row 1 is the PDD , Row 2 is Reparto. I want them freezed during scroll.
<columnHeader>
<band height="24">
<staticText>
<reportElement x="0" y="0" width="50" height="12">
<property name="net.sf.jasperreports.export.xls.freeze.row.edge" value="Bottom"/>
</reportElement>
<textElement verticalAlignment="Middle">
<font size="7" isBold="true"/>
</textElement>
<text><![CDATA[ Pdd]]></text>
</staticText>
<staticText>
<reportElement x="0" y="12" width="50" height="12"/>
<textElement verticalAlignment="Middle">
<font size="7" isBold="true"/>
</textElement>
<text><![CDATA[ Reparto]]></text>
</staticText>
</band>
</columnHeader>
From the documentation : https://jasperreports.sourceforge.net/config.reference.html#net.sf.jasperreports.export.xls.freeze.column
I tried to use:
net.sf.jasperreports.export.xls.freeze.row
net.sf.jasperreports.export.xls.freeze.row.edge
without success. I am guessing it's because I am using an older version of Jasper Ireport. Is there any alternative to achieve my goal without updating the version of the app. Updating the version is not an option.
Thank you
Changing the export type from xlsx to xls managed to solve the issue.

How to set table column footer to the bottom of the detail band?

I'm using table in jasperreport and I need to add table column footer to the bottom just before "group footer" band like
But currently it generates table like here
as you can see a lot of spaces between the table (which in detail band) and group footer band..
<detail>
<band height="80">
<componentElement>
<reportElement key="table1" style="tableStyle" x="1" y="0" width="552" height="80" isPrintWhenDetailOverflows="true" uuid="9a24d27b-9228-4f23-8c02-628268479882">
<printWhenExpression><![CDATA[$V{REPORT_COUNT} == 1]]></printWhenExpression>
</reportElement>
<jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" whenNoDataType="AllSectionsNoDetail">
<datasetRun subDataset="TableDataset" uuid="aad66998-86ba-485c-b8f4-3f2f6eda4796">
<dataSourceExpression><![CDATA[$P{datasourceItems}]]></dataSourceExpression>
</datasetRun>
<jr:column width="25" uuid="eb2f74b3-8006-4762-a52e-838f5e08a86f">
<jr:columnHeader height="40" rowSpan="2">
<staticText>
<reportElement style="tableProduct_CH" x="0" y="0" width="25" height="40" uuid="6ce8147d-c282-467a-b348-0d5cf3def735"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[No]]></text>
</staticText>
</jr:columnHeader>
<jr:columnFooter style="tableProductFooterNoLine" height="20" rowSpan="1"/>
<jr:detailCell height="20" rowSpan="1">
<textField isStretchWithOverflow="true">
<reportElement style="tableProduct_TD" x="0" y="0" width="25" height="20" uuid="fc581740-46a3-4602-b769-18beca53d355"/>
<textElement textAlignment="Center"/>
<textFieldExpression><![CDATA[$F{no}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column> ...
</jr:table>
</componentElement>
</band>
</detail>
AFIK: This is not possible in jasper reports version <=6.7 (current version at date of answer) using the jr:table component.
You could draw the lines in the background band and then add the footer for example in a groupFooter band, but I would instead suggest that you create a subreport to avoid problems with page break.
The subreport will mimic the table using the standard detail and columnFooter band, again you will be adding the vertical lines in the background band.
How the columnFooter will behave is define by the attribute isFloatColumnFooter hence in your case false

different pages on change of group in jasper reports

My report groups contains 4-5 table component which will be printed each time when group changed.I don't want to print multiple entries of same group in the table components inside the group.
<textField>
<reportElement x="150" y="0" width="60" height="20" isPrintRepeatedValues="false" />//**isPrintRepeatedValues="false"**
<textElement textAlignment="Left">
<font isBold="false" isUnderline="false"/>
</textElement>
<textFieldExpression><![CDATA[$F{activityname}]]></textFieldExpression>
</textField>
<textField>

How to sum all values in a column in Jaspersoft iReport Designer?

I have below similar report in Jaspersoft iReport Designer, but not able to figure out how to sum all values present in vertical column "Doctor Payment" to get a total of "1601"? Length of this columns is variable (i.e. no of rows vary according to size of database & its update frequency ).
Is there any variable like $V{COLUMN_COUNT} (actually gives no of rows, here 5),
which gives sum of all values in a column? If no, how to do sum?
Doctor ID Doctor Payment
A1 123
B1 223
C2 234
D3 678
D1 343
Total 1601
It is quite easy to solve your task. You should create and use a new variable for summing values of the "Doctor Payment" column.
In your case the variable can be declared like this:
<variable name="total" class="java.lang.Integer" calculation="Sum">
<variableExpression><![CDATA[$F{payment}]]></variableExpression>
</variable>
the Calculation type is Sum;
the Reset type is Report;
the Variable expression is $F{payment}, where $F{payment} is the name of a field contains sum (Doctor Payment).
The working example.
CSV datasource:
doctor_id,payment
A1,123
B1,223
C2,234
D3,678
D1,343
The template:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport ...>
<queryString>
<![CDATA[]]>
</queryString>
<field name="doctor_id" class="java.lang.String"/>
<field name="payment" class="java.lang.Integer"/>
<variable name="total" class="java.lang.Integer" calculation="Sum">
<variableExpression><![CDATA[$F{payment}]]></variableExpression>
</variable>
<columnHeader>
<band height="20" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="100" height="20"/>
<box leftPadding="10"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="10" isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[Doctor ID]]></text>
</staticText>
<staticText>
<reportElement x="100" y="0" width="100" height="20"/>
<box leftPadding="10"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="10" isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[Doctor Payment]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="100" height="20"/>
<box leftPadding="10"/>
<textElement/>
<textFieldExpression><![CDATA[$F{doctor_id}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="100" y="0" width="100" height="20"/>
<box leftPadding="10"/>
<textElement/>
<textFieldExpression><![CDATA[$F{payment}]]></textFieldExpression>
</textField>
</band>
</detail>
<summary>
<band height="20">
<staticText>
<reportElement x="0" y="0" width="100" height="20"/>
<box leftPadding="10"/>
<textElement>
<font isBold="true"/>
</textElement>
<text><![CDATA[Total]]></text>
</staticText>
<textField>
<reportElement x="100" y="0" width="100" height="20"/>
<box leftPadding="10"/>
<textElement>
<font isBold="true" isItalic="true"/>
</textElement>
<textFieldExpression><![CDATA[$V{total}]]></textFieldExpression>
</textField>
</band>
</summary>
</jasperReport>
The result will be:
You can find a lot of info in the JasperReports Ultimate Guide.
iReports Custom Fields for columns
(sum, average, etc)
Right-Click on Variables and click Create Variable
Click on the new variable
a. Notice the properties on the right
Rename the variable accordingly
Change the Value Class Name to the correct Data Type
a. You can search by clicking the 3 dots
Select the correct type of calculation
Change the Expression
a. Click the little icon
b. Select the column you are looking to do the calculation for
c. Click finish
Set Initial Value Expression to 0
Set the increment type to none
Leave Incrementer Factory Class Name blank
Set the Reset Type (usually report)
Drag a new Text Field to stage (Usually in Last Page Footer, or Column Footer)
Double Click the new Text Field
Clear the expression “Text Field”
Select the new variable
Click finish
Put the new text in a desirable position 

Printing serial number in jasper report detail

I have created a jasper report. In that report in detail areaI have "serialNumber" column. That column wants to be auto incrementive and stats with "1". I am using hibernate for query.
Sample code is :
<detail>
<band height="17" splitType="Stretch">
<textField isBlankWhenNull="true">
<reportElement x="12" y="0" width="27" height="15"/>
<textElement/>
<textFieldExpression class="java.lang.Integer"><![CDATA[serialNumber]]>
</textFieldExpression>
</textField>
<textField>
<reportElement x="51" y="0" width="37" height="15"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{date}]]>
</textFieldExpression>
</textField>
<textField>
<reportElement x="138" y="0" width="75" height="15"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{time}]]>
</textFieldExpression>
</textField>
</band>
</detail>
Can anyone help to print serial number in jasper report.
Using variable we can achieve that.
Sample code :
<variable name="seraialNumber" class="java.lang.Integer" resetType="None"
calculation="Count"/>
Depends on the requirement we have to change expression
You have to bind the column to a bean which returns incrementing numbers.
You Can use alternate solution for this problem that build in Variable $V{REPORT_COUNT}.
This variable will return the row count in Integer format.
Sample Expression :
<textFieldExpression class="java.lang.Integer"><![CDATA[$V{REPORT_COUNT}]]></textFieldExpression>.
<variable name="serial number" class="java.lang.Integer" resetType="Column" calculation="Count">
<variableExpression><![CDATA[0]]></variableExpression>
</variable>
I will show an image for better understanding