JasperReports / iReport中的Excel数据源:无法获取类'java.sql.Date'的字段'Date'的值 [英] Excel datasource in JasperReports/iReport: Unable to get value for field 'Date' of class 'java.sql.Date'

查看:172
本文介绍了JasperReports / iReport中的Excel数据源:无法获取类'java.sql.Date'的字段'Date'的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了一个 Excel 文件作为使用 iReport 构建报表的数据源。 xls 文件包含日期 yyyy-MM-dd 格式。



我已经将Date字段类设置为 java.sql.Date ,我在iReport中点击预览 。有一个错误:无法获取类'java.sql.Date'的字段'Date'的值。



我也试过与 java.util.Date 。你有什么想法吗?

解决方案

这很简单。



我有这样的 xls 文件( birhdays.xls ):





数据源定义(在 iReport )是:





从上图可以看出,我设置了 yyyy-MM-dd 定义中的日期字段的格式



jrxml 中的日期字段定义> file:

 < field name =Birthdayclass =java.util.Date /> 

完整的 jrxml 文件:

 <?xml version =1.0encoding =UTF-8?> 
< jasperReport xmlns =http://jasperreports.sourceforge.net/jasperreportsxmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation =http: //jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsdname =excel_dslanguage =groovypageWidth =595pageHeight =842columnWidth =555 leftMargin =20rightMargin =20topMargin =20bottomMargin =20uuid =b0f82d57-325b-4c30-b635-f464851e0a47>
< queryString>
<![CDATA []]>
< / queryString>
< field name =Nameclass =java.lang.String/>
< field name =Birthdayclass =java.util.Date/>
< columnHeader>
< band height =20splitType =Stretch>
< staticText>
< reportElement uuid =cd75f6d9-1781-4938-bf24-178b6663bcd1x =0y =0width =147height =20/>
< box leftPadding =10>
< topPen lineWidth =1.0/>
< leftPen lineWidth =1.0/>
< bottomPen lineWidth =1.0/>
< rightPen lineWidth =1.0/>
< / box>
< textElement textAlignment =CenterverticalAlignment =Middle>
< font isBold =trueisItalic =true/>
< / textElement>
< text><![CDATA [Name]]>< / text>
< / staticText>
< staticText>
< reportElement uuid =5d4c21f1-9afd-40c7-ade0-6fcc0b3d8373x =147y =0width =100height =20/>
< box leftPadding =10>
< topPen lineWidth =1.0/>
< leftPen lineWidth =1.0/>
< bottomPen lineWidth =1.0/>
< rightPen lineWidth =1.0/>
< / box>
< textElement textAlignment =CenterverticalAlignment =Middle>
< font isBold =trueisItalic =true/>
< / textElement>
< text><![CDATA [Birthday]]>< / text>
< / staticText>
< / band>
< / columnHeader>
< detail>
< band height =20splitType =Stretch>
< textField>
< reportElement uuid =82f5a338-ef5d-4838-8cd0-c2544af3a73dx =0y =0width =147height =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-02db2322b6a5x =147y =0width =100height =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>

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





注意



我已经使用了iReport 5.1.0 >

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?

解决方案

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

这篇关于JasperReports / iReport中的Excel数据源:无法获取类'java.sql.Date'的字段'Date'的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆