在jasper报告中编写方法?

Don*_*onX 8 java jasper-reports

我正在创建一个jasper报告.在那里我想写一个方法,它接受整数并做一些处理并返回一个字符串.我不知道如何在jasper report中编写方法.是否有可能写?可以任何人帮助我这个

我正在使用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>
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,"$ F {intValue}"返回integer.我希望将它传递给一个方法,并且方法返回类型想要成为字符串.

谢谢

Bor*_*vić 16

使用静态方法编写一个辅助Java类,该方法将接收整数参数并返回所需的结果:

package com.yourname.reports.util;

public class JrUtils {
  public static String intFormatter(int arg) {
    return "Beautified int: " + arg;
  }
}
Run Code Online (Sandbox Code Playgroud)

将此类添加到用于编译jasperreports模板和运行时的类路径中.在iReport中,右键单击"报表检查器"视图中的报表标题,然后选择"属性".向下滚动到"Imports"并添加您的课程:

com.yourname.reports.util.JrUtils
Run Code Online (Sandbox Code Playgroud)

将import Java类添加到报表中,并使用以下命令从字段调用静态方法:

<![CDATA["Transformed int: " + JrUtils.intFormatter($F{intValue}) ]>
Run Code Online (Sandbox Code Playgroud)