在JasperReports中对记录进行分组 [英] Grouping records in JasperReports

查看:68
本文介绍了在JasperReports中对记录进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了JasperReports的问题。我想根据一个特定列的值对记录进行分组。

I have a problem with JasperReports. I want to group the records depending on one specific column's value.

例如输入数据:

Name--email--PledgeType--amount
aaa--aa@yahoo.com--1--20.00
bbb--bb@yahoo.com--2--30.00
ccc--cc@gmai.com--1--35.00
ddd--dd@gmai.com--2-- 40.00

输出报告将按PledgeType值(1,2,...数字)分组:

The output report will be grouped by the "PledgeType" value (1, 2, ... number):

Total for group one: 55.00
Name email         amount
aaa  aa@yahoo.com 20.00
ccc  cc@gmai.com  35.00
------------------------------------
Total for group two: 70.00
Name email         amount
bbb  bb@yahoo.com  30.00
ddd  dd@gmai.com   40.00

JasperReports可以解决这个问题吗?如何?

Can JasperReports solve this problem? how?

推荐答案

您可以在JasperReports中定义分组。 JasperReports为您计算总数,有一种舒适的方式来添加组和总计。这里概述了您需要在iReport中执行的操作。

You can define grouping in JasperReports. JasperReports calculates the total for you, there is comfortable way to add groups and totals. Here an overview what you need to do in iReport.

添加组


  • 通过pledgeType修改您的查询以进行排序 - JasperReports需要根据您的分组排序数据。

  • 右键单击报告检查器中的报告并选择添加报告组

  • 按照向导,设置为组名 PledgeType ,然后选择 Group by以下报表对象,在其中选择字段 PledgeType 。点击下一步。选中添加组标题,然后单击完成

  • modify your query to order by pledgeType - JasperReports requires the data sorted according to your grouping.
  • right click on the report in the report inspector and choose Add Report Group.
  • Follow the wizard, set as group name PledgeType and choose Group by the following report object where you select the field PledgeType. Click next. Check Add the group header and click Finish.

添加总额


  • 右键单击报表检查器中的变量,然后选择添加变量

  • 在属性面板中选择此配置:变量类:BigDecimal,计算:Sum,ResetType:Group,ResetGroup PledgeType ,变量表达式: $ F {amount}

  • 拖拽和将变量放入报表设计器中的组标题中。单击该字段并更改:文本字段表达式:组的总计+ $ F {PledgeType} +:+ $ V {totalPledge} ,表达式类: java.lang.String中。评估时间:小组。评估组:PledgeType。

  • right click on variables in the report inspector and choose Add Variable.
  • In the properties panel choose this configuration: Variable class: BigDecimal, Calculation: Sum, ResetType: Group, ResetGroup PledgeType, Variable Expression: $F{amount}.
  • Drag & drop the variable into the group header in the report designer. Click on the field and change: Text field expression: "Total for group " + $F{PledgeType} + ": " + $V{totalPledge}, Expression Class: java.lang.String. Evaluation time: Group. Evaluation Group: PledgeType.

信息:评估时间决定何时评估变量,即何时显示计算总和。如果将其设置为分组,则表示组处理完成后。

Info: The evaluation time decides when a variable gets evaluated, i.e. when the sum of the calculation will be shown. If you set it to group it means 'once the group processing is completed'.

附上生成的报告和JRXML。

Attached the generated report and the JRXML.

JRXML是用iReport 5.0创建的 - 但是,如果你按照上面的步骤操作,它应该适用于JR v 2 +

The JRXML is created with iReport 5.0 - however, if you follow the steps above it should work with JR v 2+

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report2" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="ce08fe1c-1543-4460-8613-7f03b200082b">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <queryString>
        <![CDATA[select * from
(select 'aaa' as Name, 'aa@yahoo.com' as email, 1 as PledgeType, 20.00 as amount
union select 'bbb', 'bb@yahoo.com' ,2, 30.00
union select 'ccc', 'cc@gmai.com' ,1, 35.00
union select 'ddd', 'dd@gmai.com' ,2, 40.00) tbl
order by PledgeType]]>
    </queryString>
    <field name="Name" class="java.lang.String"/>
    <field name="email" class="java.lang.String"/>
    <field name="PledgeType" class="java.lang.Long"/>
    <field name="amount" class="java.math.BigDecimal"/>
    <variable name="totalPledge" class="java.math.BigDecimal" resetType="Group" resetGroup="PledgeType" calculation="Sum">
        <variableExpression><![CDATA[$F{amount}]]></variableExpression>
    </variable>
    <group name="PledgeType">
        <groupExpression><![CDATA[$F{PledgeType}]]></groupExpression>
        <groupHeader>
            <band height="61">
                <textField evaluationTime="Group" evaluationGroup="PledgeType">
                    <reportElement uuid="401c7b3b-af73-4d40-8982-9c1692eb7085" x="0" y="21" width="555" height="20"/>
                    <textElement/>
                    <textFieldExpression><![CDATA["Total for group " + $F{PledgeType} + ": " + $V{totalPledge}]]></textFieldExpression>
                </textField>
                <staticText>
                    <reportElement uuid="87cd0d21-014d-4e6c-a54a-006165a38414" x="0" y="41" width="185" height="20"/>
                    <textElement/>
                    <text><![CDATA[Name]]></text>
                </staticText>
                <staticText>
                    <reportElement uuid="bd0fc2f5-4963-4c9d-a9be-3659be06e436" x="185" y="41" width="185" height="20"/>
                    <textElement/>
                    <text><![CDATA[email]]></text>
                </staticText>
                <staticText>
                    <reportElement uuid="5d5d7ce1-5353-4f83-91b4-57725b0c922b" x="370" y="41" width="185" height="20"/>
                    <textElement/>
                    <text><![CDATA[amount]]></text>
                </staticText>
            </band>
        </groupHeader>
    </group>
    <detail>
        <band height="20">
            <textField>
                <reportElement uuid="5b325da6-7c56-4357-8808-911dad16ec53" x="0" y="0" width="185" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{Name}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="0bc06b28-7b8c-4af9-997a-714d1599def1" x="185" y="0" width="185" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{email}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="e5504bb9-c3c0-4135-94c6-7ea935f97cb6" x="370" y="0" width="185" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{amount}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

这篇关于在JasperReports中对记录进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆