How to specify custom units format in pattern when exporting to excel? - jasper-reports

Using jasper reports I want to generate an excel report that has the option of having units/currencies in the column.
Like column 1: 5,74 €/h and column 2: 24,7 kg.
In excel I can enter this pattern as #.##0,00" €/h"
However using same pattern in jrxml
<textField isBlankWhenNull="true">
<reportElement height="125" style="detail">
<property name="net.sf.jasperreports.export.xls.pattern" value="#,##0" €/h""/>
</reportElement>
<textFieldExpression><![CDATA[$F{attributeWithUnit}]]></textFieldExpression>
</textField>
does not work, excel cannot understand it and therefore deformate the whole column or sheet and gives error.
How can/should I format this jasper export for excel to understand how to format it?

The issue you have is that to the export engine you actually need to pass #,##0" €/h" and not #,##0" €/h" and since attributes is parsed it's converted to #,##0" €/h"
before you start be sure to enable xls.detect.cell.type
<property name="net.sf.jasperreports.export.xls.detect.cell.type" value="true"/>
and make sure you have a font that can display the Euro symbol correctly.
With jasper-reports versione 6.4 o above
Use the CDATA tag
<textField>
<reportElement x="0" y="0" width="180" height="30" uuid="a5698aa9-a36e-444d-b590-3340250059a1">
<property name="net.sf.jasperreports.export.xls.pattern">
<![CDATA[#,##0.0" €/h"]]>
</property>
</reportElement>
<textFieldExpression><![CDATA[1235]]></textFieldExpression>
</textField>
Versione previous to 6.4
You need to do escape the & so it is correctly & when passed to export hence the format string becomes #,##0.0&quot; €/h&quot;
<textField>
<reportElement x="0" y="0" width="180" height="30" uuid="a5698aa9-a36e-444d-b590-3340250059a1">
<property name="net.sf.jasperreports.export.xls.pattern" value="#,##0.0&quot; €/h&quot;"/>
</reportElement>
<textFieldExpression><![CDATA[1236]]></textFieldExpression>
</textField>

Related

remove the white space from columnFooter because columnFooter is only shown on the last page on Jasper [duplicate]

I have a report made in iReport. I included a textFieldExpression in the column footer. My problem is that even though the line is blank it still takes up the space of the height of the band and thus sending the rest of fields to another sheet.
The code that I have is the following:
<columnFooter>
<band height="12" splitType="Stretch">
<printWhenExpression><![CDATA[$F{descripcionComentario}!=null]]></printWhenExpression>
<textField>
<reportElement x="42" y="0" width="100" height="12" uuid="5a9cbe9d-486a-4dd4-a865-d421cd7366a6"/>
<textElement>
<font size="8"/>
</textElement>
<textFieldExpression><![CDATA[$F{descripcionComentario}]]></textFieldExpression>
</textField>
</band>
</columnFooter>
The columnFooter is not resizable at run time, you need to find another report structure to solve your issue.
The solution often is to use groups where you have access to the groupFooter band.

Jasper Report assign a variable value from a DTO to the backcolor tag [duplicate]

I am using Jasper Report for designing report. I have a report where there is a static text. I want to set the background color of it by parameter value. In the XML it generates for the text like this:
<reportElement mode="Opaque" x="434" y="0" width="121" height="12" backcolor="#A6A6A6" uuid="e088bd9f-a0ac-4f34-9375-df765c829ec2"/>.
Now I need to set the backcolor from a parameter which will come from database. for here like #A6A6A6.
What can I try next? I have researched it, but no luck.
You can do that with net.sf.jasperreports.style.* element level properties (for which you can have expressions as values).
In your case you would need
<textField>
<reportElement ...>
<propertyExpression name="net.sf.jasperreports.style.backcolor">$P{someColor}</propertyExpression>
</reportElement>
...

Jasper Reports subreport text alignment is wrong only when in main report

When I preview my jasper subreport, text is aligned to the right (what I want).
But when it is used in the main report, text is aligned to the left
On both reports, per element styling is used, set in Jasper Studio.
The jrxml per element looks like...
<textElement textAlignment="Right" markup="html">
<paragraph leftIndent="2"/>
</textElement>
<textFieldExpression><![CDATA[$F{col_1_initials}]]> </textFieldExpression>
The element including the subreport looks like
<band height="161" splitType="Stretch">
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
<subreport isUsingCache="false" runToBottom="false">
<reportElement x="-20" y="0" width="595" height="161" uuid="4e6fb330-c43f-4d47-b7aa-6884dad63d14"/>
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("weekPairs")]]></dataSourceExpression>
<subreportExpression><![CDATA["weeksubreport.jasper"]]></subreportExpression>
</subreport>
</band>
I'm not using report Style Templates because for a reason I can't diagnose, Jaspersoft Studio crashes instantly trying to assign a style template.
What about the main report ovverides the subreport text-aligment and what would I have to change in the source to fix this?
EDIT
In the report designer->properties of a jrxml file there is a checkbox for "Derive Attributes". I've tried that both on and off, but nothing changed.

How to hide a bar chart with an empty list of objects

I have a bar chart in my report. In some cases the list of objects which is sent from the Java application is empty. In that cases I want to hide the bar chart in the report, but I cannot do it. It would be great if someone can help me.
You can set a "print when expression" on the chart and/or on the band containing it.
Add this <printWhenExpression><![CDATA[$F{male}==10]]></printWhenExpression>
You can specify the condition here: <![CDATA[$F{male}==10]]>
Complete solution
<barChart>
<chart isShowLegend="false" evaluationTime="Report">
<reportElement x="0" y="5" width="201" height="131" >
<property name="com.jaspersoft.studio.unit.width" value="pixel"/>
<printWhenExpression><![CDATA[$F{male}==10]]></printWhenExpression>
</reportElement>
<chartTitle>
<titleExpression><![CDATA["Population"]]></titleExpression>
</chartTitle>
<chartSubtitle/>
<chartLegend/>
</chart>
....

How to create a multi language report?

I want to create a document, which prints in more than one language, based on the Locale.
I have created 2 resource bundles, one in English and one in Chinese, but I am not sure how to use them.
Here is the sample of how to implement internationalization support for JasperReports.
The main idea is to use special expression $R{} for localizing text and images.
The sample for images:
<image scaleImage="Clip">
<reportElement positionType="Float" x="20" y="20" width="100" height="50"/>
<imageExpression class="java.lang.String"><![CDATA[$R{image.flag}]]></imageExpression>
</image>
The samples for text (the $R{} syntax):
<textField isBlankWhenNull="true">
<reportElement x="20" y="100" width="530" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$R{sampleString}]]></textFieldExpression>
</textField>
or (the msg() method):
text.message=The program picked up {0} as a random number.
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement x="20" y="210" width="530" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[msg($R{text.message}, $P{number})]]></textFieldExpression>
</textField>
Since the document generator may be part of your application, you should somewhere have a language selector menu-item, check-box or combo-box which is already preselected.
So, why don't you just add an if statement that reads the Locale, or the needed language before the report generation, and load the appropriate report accordingly to the locale.
This way you will need to keep one jrxml file for every language. It will be fairly easy to just translate the headers and labels manually.
Your data should be already translated in your database, where you have to keep the relevant attribute values multilingual anyway.
You will need to modify the SQL query for the appropriate language, but since the Query is part of your jrxml it will be executed automatically.