4

我创建了一个报告来生成具有部门列表的用户。我需要计算组而不是组内的记录。

如何用JasperReports做到这一点?

例如: -

**No: 1**
Name      : User1
Department: Depart1
            Depart2
            Depart3

**No: 2**
Name      : User2
Department: Depart1
            Depart2
            Depart3

**No: 3**
Name      : User3
Department: Depart1
            Depart2
            Depart3

在上面的示例中,我需要为 3 个用户计数 1、2、3。我不希望编号来计算用户内的记录,这意味着我不需要计数来计算每个用户的部门数量。

目前,我为用户创建了一个组,并自动创建了一个变量“User_COUNT”。我在我的详细信息带中使用它,但它似乎计算了用户组中的每条记录。

4

2 回答 2

5

You can use the variable on your Group.

The sample:

<?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="count_groups" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="7ca8f323-3cc7-4980-9222-77fb0f8f556b">
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="Name" class="java.lang.String"/>
    <field name="Department" class="java.lang.String"/>
    <variable name="cntUser" class="java.lang.Integer" incrementType="Group" incrementGroup="userGroup">
        <variableExpression><![CDATA[($V{userGroup_COUNT} == 1) ? $V{cntUser} + 1 : $V{cntUser}]]></variableExpression>
        <initialValueExpression><![CDATA[1]]></initialValueExpression>
    </variable>
    <group name="userGroup">
        <groupExpression><![CDATA[$F{Name}]]></groupExpression>
        <groupHeader>
            <band height="50">
                <textField>
                    <reportElement uuid="da974bc0-323d-4169-b584-eddb4ffcfa50" x="0" y="0" width="100" height="20"/>
                    <textElement/>
                    <textFieldExpression><![CDATA["No: " + $V{cntUser}]]></textFieldExpression>
                </textField>
                <staticText>
                    <reportElement uuid="57fcf335-26f2-44b4-89ad-11c1223c9539" x="0" y="30" width="100" height="20"/>
                    <textElement markup="none"/>
                    <text><![CDATA[Name         :]]></text>
                </staticText>
                <textField>
                    <reportElement uuid="16ccb7f0-eb39-403e-b7c4-1c6f35989f3d" x="100" y="30" width="100" height="20"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[$F{Name}]]></textFieldExpression>
                </textField>
            </band>
        </groupHeader>
    </group>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement uuid="4647a9aa-229e-4f5d-8d08-aca4cda1df2f" x="100" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{Department}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement uuid="bc4bd9ae-6c2f-43c2-823e-1501b76ef39a" x="0" y="0" width="100" height="20">
                    <printWhenExpression><![CDATA[$V{userGroup_COUNT} == 1]]></printWhenExpression>
                </reportElement>
                <textElement/>
                <text><![CDATA[Department:]]></text>
            </staticText>
        </band>
    </detail>
</jasperReport>

The report design in iReport is looks like this:

Report's design in iReport

For this input data (I've used CSV datasource in sample):

Name,Department
User1,Depart1
User1,Depart2
User1,Depart3
User2,Depart2
User2,Depart3
User3,Depart1
User3,Depart4
User3,Depart4
User3,Depart4

The result will be (via preview in iReport):

The result in iReport

Details:
In this sample I've used variable cntUser with Increment Type equals to Group (for group named userGroup). This variable incremented (see the variableExpression) only for the first record in each Group (the check $V{userGroup_COUNT} == 1).

Note: Don't forget to sort data if you are using the Grouping

于 2013-07-05T09:49:13.030 回答
0
<variable name="COUNT_GROUP" class="java.lang.Integer" incrementType="Group" incrementGroup="province" calculation="Count">
    <variableExpression><![CDATA[$V{COUNT_GROUP}]]></variableExpression>
    <initialValueExpression><![CDATA[1]]></initialValueExpression>
</variable>
于 2015-06-26T05:19:16.063 回答