net.sf.jasperreports.export.xls.pattern to export time format to Excel

Hi all,

 

I am trying to use the net.sf.jasperreports.export.xls.pattern.{arbitrary_pattern} in Jasper studio pro 7.1 to set the time format for a cell that I export to excel.  Mt textfield code looks like this:

            <textField pattern="h:mm a">
                <reportElement x="625" y="18" width="100" height="16" uuid="bcea4688-0afe-4091-880e-f52b375c7a95">
                    <property name="com.jaspersoft.studio.unit.height" value="px"/>
                    <property name="com.jaspersoft.studio.unit.rightIndent" value="px"/>
                    <property name="com.jaspersoft.studio.unit.leftIndent" value="px"/>
                </reportElement>
                <textElement textAlignment="Center" verticalAlignment="Bottom">
                    <font size="6"/>
                    <paragraph leftIndent="2" rightIndent="2"/>
                </textElement>
                <textFieldExpression><![CDATA[new java.util.Date()]]></textFieldExpression>
            </textField>

I have set the pattern in the following ways:

    <property name="net.sf.jasperreports.export.xls.pattern.h:mm a=h:mm AM/PM"/>

or

    <property name="net.sf.jasperreports.export.xls.pattern.h:mm\ a=h:mm AM/PM"/>

In both cases, when I export to excel, my time in the cell looks like

13:43 a

and when I look in the formula window, I see 5/31/2019 1:43:26 PM. Can someone please tell me what I am doing incorrect?

soake's picture
42
Joined: Jul 24 2017 - 7:37pm
Last seen: 3 years 10 months ago

I had the same issue recently. It has been solved by adding <property name="net.sf.jasperreports.export.xls.pattern" value="M/d/yyyy h:mm AM/PM"/>  to the cell that need to be formatted.

qianyd - 2 years 6 months ago

2 Answers:

I had the same issue recently. It has been solved by adding <property name="net.sf.jasperreports.export.xls.pattern" value="M/d/yyyy h:mm AM/PM"/>  to the cell that need to be formatted.

qianyd

qianyd's picture
128
Joined: Nov 11 2020 - 7:23am
Last seen: 9 months 1 week ago

In my case, I needed to display timestamps based on locale. Adding onto qianyd's answer above, I selected the element that needed formatting, went to properties, searched for the "net.sf.jasperreports.export.xls.pattern" property  and used a Pattern Expression for that property with this value

$P{REPORT_LOCALE}.equals(Locale.forLanguageTag("en-gb")) ? "dd/MM/yyyy HH:mm" : $P{REPORT_LOCALE}.equals(Locale.forLanguageTag("fr-ca")) ? "yy-MM-dd HH:mm" :  "M/d/yy h:mm AM/PM"
davidfritch's picture
Joined: May 24 2016 - 9:52am
Last seen: 1 month 2 days ago
Feedback
randomness