Printing serial number in jasper report detail - jasper-reports

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

Related

How to create a column footer for Jasper Report?

I am creating an Excel using Jasper Report. I want to create a footer at the end of the columns which has a total. Eg:
Col 1 | Col 2
1.0 1.0
2.0 3.0
Total 3.0 4.0
I have in my XML:
...
<variable name="totalCol1" class="java.math.BigDecimal" calculation="Sum">
<variableExpression><![CDATA[$F{col1}]]></variableExpression>
</variable>
<variable name="totalCol2" class="java.math.BigDecimal" calculation="Sum">
<variableExpression><![CDATA[$F{col2}]]></variableExpression>
</variable>
...
<detail>
<band height="83" splitType="Prevent">
<crosstab isRepeatColumnHeaders="false" columnBreakOffset="0" ignoreWidth="true">
....
....
</crosstab>
</band>
</detail>
<columnFooter>
<band height="210" splitType="Prevent">
<textField>
<reportElement x="2210" y="180" width="100" height="30" uuid="d4580669-65c4-46c2-b7ca-9e10624d9651"/>
<textElement textAlignment="Center">
<font isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$P{lblTotal}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="2310" y="180" width="100" height="30" uuid="1e41e36a-6eca-43d6-8096-edc893686f92"/>
<textElement>
<font isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$V{totalcol1}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="2410" y="180" width="100" height="30" uuid="ffff6945-f711-44cf-8356-092f3f3da688"/>
<textElement>
<font isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$V{totalCol2}]]></textFieldExpression>
</textField>
</band>
</columnFooter>
Columns 1 and 2 appears. But neither the label for Total nor the sum appears. Can someone help? If you need more info, please comment. Thanks in advance.

how to show page number in jasper report

How we can show the current page number in jasper sub report ? i have done for only first page with variable like this $V{PAGE_NUMBER} and "("+$V{PAGE_COUNT}+")" but how we can show in all sub reports ?
you have to use page footer band for that,
set the $V{PAGE_NUMBER}'s print when expression to "REPORT" when you have to display total pages and,
$V{PAGE_NUMBER}'s print when expression to "NOW" when you have to display current page number.
TRY This
<pageFooter>
<band height="22" splitType="Stretch">
<textField pattern="M/d/yy h:mm a">
<reportElement x="580" y="0" width="220" height="20" uuid="941d5c67-e986-4d5b-ba7e-2754f065e008"/>
<box padding="3"/>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font fontName="SansSerif" size="8"/>
</textElement>
<textFieldExpression><![CDATA["Printed on : "+new java.util.Date()]]></textFieldExpression>
</textField>
<textField>
<reportElement x="280" y="1" width="200" height="20" uuid="afe76ecf-00e9-4d52-a00b-44d38dc3aa65"/>
<box padding="3"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="SansSerif" size="8" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA["Page "+$V{PAGE_NUMBER}]]></textFieldExpression>
</textField>
</band>
</pageFooter>
Follow the below pattern , it should work.
<jasperReport>
<title>
</title>
<detail>
//Here goes all subreports
</detail>
<pageFooter>
<textField evaluationTime="Report">
<textFieldExpression><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression>
</textField>
</pageFooter>
</jasperReport>

Date formatting part of text in Jasper / ireports

how do I format a date field (database field) displayed part of a text in Jasper / iReports (4.5.1)
Displayed via a text field in the report... (using Groovy please)
"Sub total for this date: " + $F(DEPOSIT_DATE)
I have tried (new SimpleDateFormat("MM/dd/yyyy")).parse($F{DEPOSIT_DATE})and I am getting error message:
net.sf.jasperreports.engine.fill.JRExpressionEvalException: 
Error evaluating expression : Source text : (new SimpleDateFormat("MM/dd/yyyy")).parse($F{BANK_DATE}) 
What I want to display in my report is as follows...
Sub total for this date: MM/DD/YYYY - format...
Try this:
new SimpleDateFormat("MM/dd/yyyy").format($F{BANK_DATE})
I agree with Mateusz, textField with pattern perfrormes faster than new SimpleDateFormat("someFormat").format("jasperField"). It's important when you have deal with huge reports. This is my example
<textField pattern="MM/dd/yyyy" isBlankWhenNull="true">
...
<textFieldExpression class="java.util.Date"><![CDATA[$F{certIssueDate}]]></textFieldExpression>
</textField>
It seems, that you try to parse instead of formatting (as stated above).
You could also use Pattern in the textfield properties tab to pretty-print the date, or manually change the pattern in the jrxml:
<textField pattern="MM/dd/yyyy">
<!-- here comes other generated data-->
<textFieldExpression><![CDATA[$F{BANK_DATE}]]></textFieldExpression>
</textField>
if the Date field is a String value, say : "2014-11-20"
<field name="dateField" class="java.lang.String"/>
then you can do this
<variable name="THE_DATE" class="java.util.Date">
<variableExpression>
<![CDATA[new java.text.SimpleDateFormat("yyyy-mm-dd").parse($F{dateField})]]>
</variableExpression>
</variable>
<textField pattern="dd/MM/yyyy" isBlankWhenNull="true">
<reportElement x="0" y="0" width="88" height="20" uuid="47b41787-a8fd-44ea-bf96-7e5484e477fb"/>
<textElement textAlignment="Left" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[ $V{THE_DATE} ]]></textFieldExpression>
</textField>
You can set the pattern by
right clicking the field -> click field pattern -> Select Date -> Choose a Date Pattern
you may also do this
<textField isBlankWhenNull="true">
<reportElement x="0" y="0" width="88" height="20" uuid="47b41787-a8fd-44ea-bf96-7e5484e477fb"/>
<textElement textAlignment="Left" verticalAlignment="Middle"/>
<textFieldExpression class="java.util.Date"><![CDATA[ new java.text.SimpleDateFormat("dd/MM/yyyy").format(new java.text.SimpleDateFormat("yyyy-mm-dd").parse($F{dateField})) ]]></textFieldExpression>
</textField>
However,If the DateField is of type Date
then doing the below is just fine.
<field name="dateField" class="java.util.Date"/>
<textField pattern="dd/MM/yyyy" isBlankWhenNull="true">
<reportElement x="0" y="0" width="88" height="20" uuid="47b41787-a8fd-44ea-bf96-7e5484e477fb"/>
<textElement textAlignment="Left" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[ $F{dateField} ]]></textFieldExpression>
</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 

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

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>