Excel datasource in JasperReports/iReport: Unable 

2019-02-20 01:47发布

问题:

I've used an Excel file as datasource for building report with iReport. The xls file contains dates yyyy-MM-dd format.

I've set the Date field class to java.sql.Date and I click on preview in iReport. There is an error: Unable to get value for field 'Date' of class 'java.sql.Date'.

I have tried with java.util.Date too. Do you have any ideas?

回答1:

It is quite simple.

I've xls file (birhdays.xls) like this:

The datasource definition (in iReport) is:

As you can see from the image above I've set yyyy-MM-dd format for date field in DS definition.

The date field definition in jrxml file:

<field name="Birthday" class="java.util.Date"/>

The full 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="excel_ds" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="b0f82d57-325b-4c30-b635-f464851e0a47">
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="Name" class="java.lang.String"/>
    <field name="Birthday" class="java.util.Date"/>
    <columnHeader>
        <band height="20" splitType="Stretch">
            <staticText>
                <reportElement uuid="cd75f6d9-1781-4938-bf24-178b6663bcd1" x="0" y="0" width="147" height="20"/>
                <box leftPadding="10">
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true" isItalic="true"/>
                </textElement>
                <text><![CDATA[Name]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="5d4c21f1-9afd-40c7-ade0-6fcc0b3d8373" x="147" y="0" width="100" height="20"/>
                <box leftPadding="10">
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true" isItalic="true"/>
                </textElement>
                <text><![CDATA[Birthday]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement uuid="82f5a338-ef5d-4838-8cd0-c2544af3a73d" x="0" y="0" width="147" height="20"/>
                <box leftPadding="10">
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA[$F{Name}]]></textFieldExpression>
            </textField>
            <textField pattern="dd/MM/yyyy">
                <reportElement uuid="e6eb6f83-b8f3-42fd-b116-02db2322b6a5" x="147" y="0" width="100" height="20"/>
                <box leftPadding="10">
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA[$F{Birthday}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

And the result will be (via preview in iReport):

Note:

I've used iReport 5.1.0