excel - Jasper iReport custom date and custom time -
in excel, have date format yyyy.mm.dd hh:mm , time format hh:mm. set source ireport database.
for date set custom date format in ireport, same in excel. set class="java.util.date".
for time, class should choose? i've tried many, none working.
i think, problem is, cannot set custom date format, ireport not recognise it.
thank help.
it quite harder solve new case old 1 described in excel datasource in jasperreports/ireport: unable value field 'date' of class 'java.sql.date' post.
solution
datasource, data
the xls file (flights.xls) contains data this:
the cells in departure , duration columns have text format (without patterns).
datasource, configuration
the excel datasource have configuration (in ireport, can same java code):
the date format set yyyy.mm.dd hh:mm
reading data departure column of excel file.
it impossible declare several fields of java.util.date
excel datasource contains date data different patterns.
behavior determined implementation net.sf.jasperreports.engine.data.jrxlsdatasource.getfieldvalue(jrfield) method. can see source code of class , can change behavior.
template
the fields in report be:
<field name="flight" class="java.lang.string"/> <field name="departure" class="java.util.date"/> <field name="duration" class="java.lang.string"/>
note 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 (via preview in ireport):
notes:
- i've used ireport 5.1.0.
- you can change format of duration field of java.text .dateformat.format(date date) method. can example in mysql datetime format change using java jasperreports generation post.
- the format defined in datasource applied departure field.
Comments
Post a Comment