JasperReport:如何使用子报表返回值作为主报表变量计算的输入

时间:2015-03-11 22:28:22

标签: java jasper-reports jaspersoft-studio

情境:

我有两个报告:主要报告(让我们称之为A)和子报告(让我们称之为,B)。

报告A在详细信息带中包含子报告B,因此在报告A数据源中为每个元素显示子报告B.子报告B还将一个变量返回到主报告A。

我想要的是是对子报告B中的返回值求和,并在主报告摘要中对它们进行总计。

为此,我尝试创建一个新的报表变量,将这些返回值相加......就像这样:

Variable Definition Example

但是,我发现在渲染波段细节之前总会评估这样的变量表达式,所以我总是会错过第一个子报表返回值......

可悲的是,评估时间(如link所述)不能在这些变量上改变,所以我被卡住了......

2 个答案:

答案 0 :(得分:2)

经过几个小时的努力......并在互联网上寻找解决方案......我带来了一个解决方法(启发性论坛是这些:onetwo)。

首先,您需要定义一个java Class Helper,它允许您计算一些算术运算,在我的例子中是一个Sum运算。我定义了这些类:

package reports.utils;

import java.util.Map;

/**
 * Utility that allows you to sum Integer values.
 */
public class SumCalculator {

    /**
     * Stores a map of {@code SumCalculator} instances (A Map instance per thread).
     */
    private static final ThreadLocalMap<String, SumCalculator> calculatorsIndex = new ThreadLocalMap<>();

    /**
     * The sum total.
     */
    private int total = 0;


    /**
     * No arguments class constructor.
     */
    private SumCalculator() {
        super();
    }


    /**
     * Instance a new {@code SumCalculator} with the given ID.
     *
     * @param id    {@code SumCalculator}'s ID
     * @return      the new {@code SumCalculator} instance
     */
    public static SumCalculator get(String id) {
        Map<String, SumCalculator> map = calculatorsIndex.get();
        SumCalculator calculator       = map.get(id);

        if (calculator == null) {
            calculator = new SumCalculator();
            map.put(id, calculator);
        }
        return calculator;
    }


    /**
     * Destroy the {@code SumCalculator} associated to the given ID.
     *
     * @param id    {@code SumCalculator}'s ID
     * @return      {@code null}
     */
    public static String destroy(String id) {
        Map<String, SumCalculator> map;

        map = calculatorsIndex.get();
        map.remove(id);

        if (map.isEmpty()) {
            calculatorsIndex.remove();
        }
        return null;
    }


    /**
     * Resets the {@code SumCalculator} total.
     *
     * @return  {@code null}
     */
    public String reset() {
        total = 0;
        return null;
    }


    /**
     * Adds the given integer value to the accumulated total.
     *
     * @param i     an integer value (can be null)
     * @return      {@code null}
     */
    public String add(Integer i) {
        this.total += (i != null) ? i.intValue() : 0;
        return null;
    }


    /**
     * Return the accumulated total.
     *
     * @return  an Integer value (won't be null, never!)
     */
    public Integer getTotal() {
        return this.total;
    }
}

package reports.utils;

import java.util.HashMap;
import java.util.Map;

/**
 * Thread Local variable that holds a {@code java.util.Map}.
 */
class ThreadLocalMap<K, V> extends ThreadLocal<Map<K, V>> {

    /**
     * Class Constructor.
     */
    public ThreadLocalMap() {
        super();
    }


    /* (non-Javadoc)
     * @see java.lang.ThreadLocal#initialValue()
     */
    @Override
    protected Map<K, V> initialValue() {
        return new HashMap<>();
    }
}

其次,在您的jasper报告中,您需要定义四个文本字段:

1)一个文本字段,可以对您的计算器进行初始化;它应该(理想情况下)在报告的标题部分,并且应该有这样的表达式:SumCalculator.get("$V{SUB_REPORT_RETURN_VALUE}").reset()。此文本字段应具有评估时间:现在。

2)调用增量函数的文本字段(即SumCalculator.get("$V{SUB_REPORT_RETURN_VALUE}").add($V{SUB_REPORT_RETURN_VALUE})。此文本字段将位于子报表元素之后的详细信息区域;并且它应具有评估时间: BAND(this非常重要!)

3)打印计算器总数的文本字段。此文本字段将位于您的摘要范围内,它将评估为 NOW 。其表达式为:SumCalculator.get("$V{SUB_REPORT_RETURN_VALUE}").getTotal()

4)破坏计算器的文本字段。此文本字段也将位于摘要区域,并且必须显示在文本字段3之后。文本字段应具有如下表达式:SumCalculator.destroy("$V{SUB_REPORT_RETURN_VALUE}")。此文本字段应具有评估时间:现在。

此外,文本字段:1,2和4应具有属性“Blank at Null”,因此它们永远不会被打印(这就是为什么这些java操作总是返回null)。

就是这样。然后,您的报告可能如下所示:

reportExample

答案 1 :(得分:0)

如果我理解了这个问题,你无法总结主报告中子报告返回的数量,我遇到了同样的问题,我就这样解决了。

1.-创建一个从 net.sf.jasperreports.engine.JRDefaultScriptlet 扩展的类。并覆盖方法 beforeReportInit()

这是这个类的代码。

C2 A0

2.-在你的ireport的classpath中添加你的项目jar。 ireport classpath config.

3.-替换REPORT scriptlet的类。 default REPORT scriptlet

在您的课程的属性中。 your class

3.-在组页脚中添加要打印子报告返回的值的文本字段,其中包含以下表达式。

package com.mem.utils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import net.sf.jasperreports.engine.JRDefaultScriptlet;

public class SumarizacionSubtotales extends JRDefaultScriptlet {
    private final Log log = LogFactory.getLog(getClass());

    private Double total;

    public Double getTotal() {
        return total;
    }

    public Double add(Double cantidad) {
        if(log.isDebugEnabled())log.debug("AGREGANDO LA CANTIDAD : " + cantidad);
        this.total += cantidad;
        return cantidad;
    }
    @Override
    public void beforeReportInit() throws JRScriptletException {
        if(log.isDebugEnabled())log.debug("beforeReportInit");
       total = 0.0D;
    }
}

在这种情况下, $ V {sum_detalles} 是主报告中的变量,其中包含子报告返回的值。

4.-在“最后一页”页面中添加另一个带有以下表达式的文本字段。

$P{REPORT_SCRIPTLET}.add( $V{sum_detalles} )

report structure

相关问题