非常规格式数据分组处理

【问题】

I am new to Jasper report. I am working on the report which shows transaction data in a table. I have several groups in the table (the group number is not fixed), for the 1st item of each group, I don’t need to show the “+” sign before the item’s actual text. If there are more than 1 items for the group, then I need to show the “+” sign before the item’s actual text, and calculate the subtotal for the group items. If there is only 1 item for the group, I don’t need to calculate the subtotal.

In the table, I only show the items for each group, I don’t show each group’s name.

Could someone please advise on how to achieve this? How to show the “+” sign conditionally, and how to calculate the subtotal conditionally?

Thanks!

(for group1)

item1 value1

+item2 value2

subtotal group1

(for group2)

item1 value1

+item2 value2

+item3 value3

subtotal group2

(for group3)

item1 value

【回答】

控制数据是否隐藏可实现这个需求,即整理出 Jasper 需要的数据格式,然后用 table 控件直接呈现。使用 Jasper 的脚本、SQL、存储过程都可以,这里用较简单的 SPL 为例:

A B C
1 =myDB1.query(“select Seller,Client,Amount from sOrder where Amount>?”,arg)
2 =create(item,value)
3 for A1.group(SellerId) >A2.insert(0:A3,if(#>1,“+”)+Client,Amount)
4 if A3.len()>1 >A2.insert(0,A3.SellerId+“subtotal:”,A3.sum(Amount))
5 result A2

A1:从数据库查询数据,arg 是参数

A2:创建由两个字段组成的序表

A3-B3:将查出的数据分组,并逐条插入到序表,大于 1 条时加 + 号

B2-B3:分组记录数大于 1 条时,追加一条统计记录;

A5:返回序表;

​Jasper 可以通过 JDBC 连接集算器,调用脚本方法和调用存储过程一样,详情参考【JasperReport 调用 SPL 脚本】。