2009-09-24 90 views
7

我正在創建一個jasper報告。我想寫一個方法,它需要整數並執行一些處理並返回一個字符串。我不知道如何在jasper報告中編寫方法。是否可以編寫?是否有人可以幫助我在這在jasper報告中寫入方法?

我正在使用iReport3.6.0。

示例代碼:

<textField> 
    <reportElement x="400" y="10" width="80" height="15"/> 
    <textElement textAlignment="Left" verticalAlignment="Middle"/> 
    <textFieldExpression  class="java.lang.String"> 
       <![CDATA[$F{intValue}]]> 
    </textFieldExpression> 
</textField> 

在上面的代碼「$ F {}的intValue」返回integer.I想傳遞一個方法和方法的返回類型都想成爲字符串。

感謝

回答

14

寫一個靜態方法幫助Java類將接收整型參數並返回期望的結果:

package com.yourname.reports.util; 

public class JrUtils { 
    public static String intFormatter(int arg) { 
    return "Beautified int: " + arg; 
    } 
} 

該類添加到用於編譯JasperReports的模板,並在類路徑運行。在iReport中,右鍵單擊「Report Inspector」視圖中的報告標題,然後選擇「屬性」。向下滾動到「進口」和你的類添加:

com.yourname.reports.util.JrUtils 

添加導入Java類到您的報表,並使用調用從外地靜態方法:

<![CDATA["Transformed int: " + JrUtils.intFormatter($F{intValue}) ]> 
+0

謝謝...做工不錯。 .. – DonX 2009-09-24 09:36:53

+0

不客氣 – 2009-09-24 11:45:11

1

@Boris洛維奇的答案是好的,但我認爲它想念一個小想法 - classpath。所以,如果你對編譯如下錯誤:

net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file: 
Only a type can be imported. com.core.report.Util resolves to a package import com.core.report.Util; 

. Util cannot be resolved value = (java.lang.String)(Util.doit(((java.sql.Timestamp)field_time.getValue()))); 

您必須添加的* .jar的你的項目其中包含聲明輔助類如下:

> In You iReport Designer go to Tool -> Options -> iReport -> Classpath -> 
and press button "Add JAR" and select You project's jar.