简体   繁体   English

有没有办法在Jasper Report中生成带有小计的表?

[英]Is there way to generate table with subtotal in Jasper Report?

Is there way to generate table with subtotal in Jasper Report? 有没有办法在Jasper Report中生成带有小计的表?

I already get some reference for subtotal using querystring . 我已经使用querystring获得了一些关于小计的参考 I want to use subDataset and JRBeanCollectionDataSource . 我想使用subDatasetJRBeanCollectionDataSource How can I do in template for subtotal? 如何在小计模板中做?

template 模板

<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>

program 程序

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

Expected output 预期产量

+--------+-----------+-------+
|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   |
+--------+-----+-------------+

I think it's simple to calculate it out of the report and place in the DataSource 我认为从报表中计算出来并将其放置在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));

UPDATE: You can calculate the same on DB level 更新:您可以在数据库级别计算相同的值

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

You can use report grouping. 您可以使用报告分组。 Here's what you should be doing. 这就是你应该做的。

  • Take a sub report move your data there. 生成一个子报告,将您的数据移到那里。
  • Take a group which changes on product name ie AAA in sub report. 参加一个在子报告中更改产品名称即AAA的小组。
  • Take a variable that resets on group change and that calculates the sum Qty . 取一个变量,该变量在组更改时重置并计算总和Qty

For more reference on report groups visit this link. 有关报告组的更多参考,请访问链接。

EDIT : 编辑:

Alternatively you can use report scriplets to accomplish it. 或者,您可以使用报告清单来完成它。 Visit here for more on report scriplets. 请访问此处以获取有关报告摘要的更多信息。

I suggest you use report scriplets this would save you from creating sub reports and report froups. 我建议你使用report scriplets这将节省您创建子报表和报表froups。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM