Car*_*Way 5 java jasper-reports jaspersoft-studio
场景:
我有两个报告:主报告(让我们称之为A)和子报告(让我们称之为B).
报告A在详细信息带中包含子报告B,因此在报告A数据源中为每个元素显示子报告B. 子报告B还将一个变量返回到主报告A.
我想要的是将子报告B中的返回值相加并在主报告摘要中对它们进行总计.
为此,我尝试创建一个新的报表变量,将这些返回值相加...这样的事情:

但是,我发现在渲染波段细节之前总会评估这样的变量表达式,所以我总是会错过第一个子报表返回值...
可悲的是,评估时间(正如这个链接所说)不能在这些变量上改变,所以我被卡住了......
经过几个小时的努力...并在互联网上搜索解决方案...我提出了一个解决方法(启发性的论坛是这些:一和二)。
首先,您需要定义一个 java 类帮助器,它允许您计算一些算术运算,在我的例子中是 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;
}
}Run Code Online (Sandbox Code Playgroud)
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<>();
}
}Run Code Online (Sandbox Code Playgroud)
其次,在 jasper 报告中,您需要定义四个文本字段:
1) 用于初始化计算器的文本字段;它应该(理想地)位于报告的标题部分,并且应该具有如下表达式:SumCalculator.get("$V{SUB_REPORT_RETURN_VALUE}").reset()。该文本字段应具有评估时间:NOW。
2)一个调用增量函数的文本字段(即SumCalculator.get("$V{SUB_REPORT_RETURN_VALUE}").add($V{SUB_REPORT_RETURN_VALUE})。这个文本字段将驻留在子报表元素之后的详细信息带中;并且它应该具有评估时间:BAND(这非常重要!!)
3) 打印计算器总计的文本字段。该文本字段将驻留在您的摘要区域中,其计算结果为NOW。其表达式为:SumCalculator.get("$V{SUB_REPORT_RETURN_VALUE}").getTotal()
4)破坏计算器的文本字段。此文本字段也将驻留在您的摘要区域中,并且必须出现在文本字段之后 3. 文本字段应具有如下表达式:SumCalculator.destroy("$V{SUB_REPORT_RETURN_VALUE}")。该文本字段应具有评估时间:NOW。
另外,文本字段:1、2 和 4 应该具有“空时空白”属性,因此它们永远不会被打印(这就是为什么这些 java 操作总是返回 null)。
就是这样。然后,您的报告可能如下所示:
