【问题标题】:Jasper iReport custom date and custom timeJasper iReport 自定义日期和自定义时间
【发布时间】:2013-08-16 09:03:37
【问题描述】:

在 Excel 中,我有一个日期格式 yyyy.MM.dd hh:mm 和一个时间格式 hh:mm。我将此设置为 iReport 数据库的来源。

对于日期,我在 iReport 中设置了自定义日期格式,与 Excel 中相同。然后我设置 class="java.util.Date".

目前,我应该选择哪个课程?我尝试了很多,没有一个工作。

我认为,问题在于,我无法设置其他自定义日期格式,因此 iReport 无法识别它。

感谢您的帮助。

【问题讨论】:

  • 以前有人告诉过,在 Excel 中,每一列都必须是文本,而不是其他任何内容。

标签: excel jasper-reports ireport


【解决方案1】:

Excel datasource in JasperReports/iReport: Unable to get value for field 'Date' of class 'java.sql.Date' 帖子中描述的旧案例相比,解决您的新案例要困难得多。

解决方案

数据源、数据

xls 文件 (flights.xls) 包含如下数据:

DepartureDuration 列中的单元格具有 Text 格式(没有任何模式)。

数据源、配置

Excel 数据源有配置(在 iReport 中,你可以用 Java 代码做同样的事情):

日期格式设置为yyyy.MM.dd HH:mm,用于从Excel文件的Departure列读取数据。

不可能为 Excel 数据源 声明多个 java.util.Date 字段,其中包含具有不同模式的 日期 数据。
此行为由实现net.sf.jasperreports.engine.data.JRXlsDataSource.getFieldValue(JRField) 方法确定。你可以看到这个类的源代码,你可以改变这个行为。

模板

报告中的字段将是:

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

请注意,Duration 字段具有 java.lang.String 类型。

jrxml 文件:

<?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>

输出结果

结果将是(通过 iReport 中的预览):

注意事项

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多