有没有办法在Jasper Report中生成带小计的表?
我已经使用querystring
获得了一些reference小计。我想使用subDataset
和JRBeanCollectionDataSource
。如何在模板中进行小计?
模板
<subDataset name="dataSource">
<field name="product" class="java.lang.String"/>
<field name="qty" class="java.lang.Integer"/>
<field name="date" class="java.lang.Date"/>
</subDataset>
<parameter name="TableDataSource" class="net.sf.jasperreports.engine.JRDataSource"/>
....
<jr:table ...>
<datasetRun subDataset="dataSource">
<dataSourceExpression><![CDATA[$P{TableDataSource}]]></dataSourceExpression>
</datasetRun>
......
</jr:table>
程序
list.add(new Product("AAA", 100, date));
list.add(new Product("AAA", 100, date));
list.add(new Product("AAA", 100, date));
list.add(new Product("BBB", 200, date));
list.add(new Product("BBB", 150, date));
list.add(new Product("BBB", 100, date));
new JRBeanCollectionDataSource(list));
预期输出
+--------+-----------+-------+
|Product | Date | Qty |
+--------+-----------+-------+
|AAA | 08-08-210 | 100 |
| | 08-09-210 | 100 |
| | 08-10-210 | 100 |
+--------+-----------+-------|
| SubTotal | 300 |
+--------+-----+-------------+
|BBB | 08-08-210 | 200 |
| | 08-09-210 | 150 |
| | 08-10-210 | 100 |
+--------+-----------+-------|
| SubTotal | 450 |
+--------+-----+-------------+
答案 0 :(得分:0)
我认为从报告中计算出来并放在DataSource中很简单
list.add(new Product("AAA", 100, date));
list.add(new Product("AAA", 100, date));
list.add(new Product("AAA", 100, date));
list.add(new Product("TOTAL:", 300, date)); //total AAA
list.add(new Product("BBB", 200, date));
list.add(new Product("BBB", 150, date));
list.add(new Product("BBB", 100, date));
list.add(new Product("TOTAL:", 450, date)); //total BBB
new JRBeanCollectionDataSource(list));
更新:您可以在数据库级别计算相同
select 0 as ord, * from t
union all
select 1 as ord, 'Total' as product, sum(Qty) as Qty, null as date from t group by
order by ord, product
答案 1 :(得分:0)