Jasper iReport custom date and custom time

元气小坏坏 提交于 2019-12-02 04:03:53
Alex K

It is quite slightly harder to solve this your new case than the old one described in Excel datasource in JasperReports/iReport: Unable to get value for field 'Date' of class 'java.sql.Date' post.

Solution

Datasource, data

The xls file (flights.xls) contains data like this:

The cells in Departure and Duration columns have Text format (without any patterns).

Datasource, configuration

The Excel datasource have configuration (in iReport, you can do the same with Java code):

The Date format is set as yyyy.MM.dd HH:mm for reading data from Departure column of Excel file.

It is impossible to declare several fields of java.util.Date for Excel datasource which contains Date data with different patterns.
This behavior is determined by implementation net.sf.jasperreports.engine.data.JRXlsDataSource.getFieldValue(JRField) method. You can see the source code of this class and you can change this behavior.

Template

The fields in report will be:

<field name="Flight" class="java.lang.String"/>
<field name="Departure" class="java.util.Date"/>
<field name="Duration" class="java.lang.String"/>

Note that the Duration field has java.lang.String type.

The jrxml file:

<?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="flights" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="827d6b81-9342-48fd-88b5-f9ea2cfd8bfb">
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="Flight" class="java.lang.String"/>
    <field name="Departure" class="java.util.Date"/>
    <field name="Duration" class="java.lang.String"/>
    <columnHeader>
        <band height="20" splitType="Stretch">
            <staticText>
                <reportElement uuid="ccfc5f9d-0b0f-4127-be62-3c3eefc16c5e" mode="Opaque" x="273" y="0" width="100" height="20" backcolor="#999999"/>
                <box leftPadding="10">
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle" markup="none">
                    <font isBold="true" isItalic="true" isUnderline="false"/>
                </textElement>
                <text><![CDATA[Duration]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="5f7a483c-2c9b-456e-8d6e-3063534c1710" mode="Opaque" x="0" y="0" width="173" height="20" backcolor="#999999"/>
                <box leftPadding="10">
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle" markup="none">
                    <font isBold="true" isItalic="true" isUnderline="false"/>
                </textElement>
                <text><![CDATA[Flight]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="45724e54-4a70-48b1-980e-acbba6266670" mode="Opaque" x="173" y="0" width="100" height="20" backcolor="#999999"/>
                <box leftPadding="10">
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle" markup="none">
                    <font isBold="true" isItalic="true" isUnderline="false"/>
                </textElement>
                <text><![CDATA[Departure]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement uuid="a45c9487-3521-4440-8c4d-c016023dc799" x="0" y="0" width="173" height="20"/>
                <box leftPadding="10">
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA[$F{Flight}]]></textFieldExpression>
            </textField>
            <textField pattern="dd/MM/yyyy HH:mm">
                <reportElement uuid="ad60f65d-72ba-48d3-bf3e-0d4275edabb4" x="173" y="0" width="100" height="20"/>
                <box leftPadding="10">
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA[$F{Departure}]]></textFieldExpression>
            </textField>
            <textField isBlankWhenNull="false">
                <reportElement uuid="b20e0e58-feab-47f9-9ce8-63d89425e914" x="273" y="0" width="100" height="20"/>
                <box leftPadding="10">
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA[$F{Duration}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

Output result

The result will be (via preview in iReport):

Notes:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!