Writing methods in jasper report?

前端 未结 2 1229
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-29 13:51

I am creating a jasper report.In that I want to write one method which takes integer and does some process and returns a string.I dont know how to write methods in jasper re

相关标签:
2条回答
  • 2020-12-29 14:08

    Write a helper Java class with a static method that will receive the integer argument and return desired outcome:

    package com.yourname.reports.util;
    
    public class JrUtils {
      public static String intFormatter(int arg) {
        return "Beautified int: " + arg;
      }
    }
    

    Add this class to the classpath used for compiling jasperreports template and for the runtime. In the iReport right click on report's title in 'Report Inspector' view and choose 'Properties'. Scroll down to 'Imports' and add your class:

    com.yourname.reports.util.JrUtils
    

    Add import Java class to your report and invoke the static method from the field using:

    <![CDATA["Transformed int: " + JrUtils.intFormatter($F{intValue}) ]>
    
    0 讨论(0)
  • 2020-12-29 14:16

    @Boris Pavlović answer is good, but I think it miss one little think- classpath. So If You have an error on compile like:

    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())));
    

    You have to add *.jar of You project which contains declared helper class as follow:

    > In You iReport Designer go to Tool -> Options -> iReport -> Classpath -> 
    and press button "Add JAR" and select You project's jar.
    
    0 讨论(0)
提交回复
热议问题