これはより簡単な方法で実現できるため、サブレポートは使用しません。レポートグループと変数を利用してこれを実現できます。データが出力に応じてソートされていることを確認してください。
準備
- StudentNameに基づいてレポートグループを作成します。レポートのレポートインスペクターでiReportを右クリックし、[レポートグループの追加]を選択します。ウィザードに従い、名前を付け(例
Student
)、グループ式としてフィールドを選択しStudentName
、ヘッダーを追加しますが、フッターは追加しません。
- 学生の合計を保持する変数を作成します。変数のレポートインスペクターでiReportを右クリックし、 [変数の追加]を選択します。プロパティパネルで、次のように構成します。名前:totalMarkByStudent、変数クラス:java.lang.Long、計算:合計、リセットタイプ:グループ、リセットグループ:学生、変数式:$F{Marks}。残りはデフォルト値のままにします。
レポートデザイン
StudentName
フィールドをレポートインスペクターからレポートデザイナー、Studentグループヘッダーバンドにドラッグアンドドロップします。表示する値の種類を尋ねるポップアップが表示されたら、[フィールド値]を選択して[OK]をクリックします。
totalMarksByStudent
変数をレポートインスペクターからレポートデザイナー、Studentグループヘッダーバンドにドラッグアンドドロップします。テキストフィールドをクリックし、プロパティパネルで次の設定を変更します。
- 幅:(
200
より多くのスペースを確保するため)
- テキストフィールド式:
"Total : " + $V{totalMarkByStudent}
- 式クラス:
java.lang.String
- 評価時間:(
Group
グループの処理が終了すると値が評価されることを意味します)
- 評価グループ:
Student
[コース]フィールドと[マーク]フィールドをレポートインスペクターからレポートデザイナーの詳細バンドにドラッグアンドドロップします。コースフィールドを右クリックして、[式の編集]を選択します。式を変更して"Subject : " + $F{Course}
、[適用]をクリックします。
この構成では、次の画像のようなレポート出力を実現します。

ここでさらに参照するには、レポートグループと変数を使用した完全なJRXMLを参照してください。
<?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="report3" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="0dfbb9b2-a9ce-4447-beee-37d653140dd1">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<queryString>
<![CDATA[select * from (
Select 'Peter' as StudentName, 'Bio' as Course, 65 as Marks
union select 'Peter', 'Chem', 70
union select 'Peter', 'Music', 80
union select 'David', 'Chem', 50
) tbl
order by StudentName, Course]]>
</queryString>
<field name="StudentName" class="java.lang.String"/>
<field name="Course" class="java.lang.String"/>
<field name="Marks" class="java.lang.Long"/>
<variable name="totalMarkByStudent" class="java.lang.Long" resetType="Group" resetGroup="Student" calculation="Sum">
<variableExpression><![CDATA[$F{Marks}]]></variableExpression>
</variable>
<group name="Student">
<groupExpression><![CDATA[$F{StudentName}]]></groupExpression>
<groupHeader>
<band height="50">
<textField>
<reportElement uuid="ea996b6c-d41d-47bb-bef1-5df580b5c161" x="0" y="30" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{StudentName}]]></textFieldExpression>
</textField>
<textField evaluationTime="Group" evaluationGroup="Student">
<reportElement uuid="8ddc9b5b-9c57-4fce-8ed0-587c6b54143c" x="180" y="30" width="200" height="20"/>
<textElement/>
<textFieldExpression><![CDATA["Total : " + $V{totalMarkByStudent}]]></textFieldExpression>
</textField>
</band>
</groupHeader>
</group>
<detail>
<band height="20">
<textField>
<reportElement uuid="f67b4e51-4da6-4758-b3d3-bd75de70c0f7" x="0" y="0" width="180" height="20"/>
<textElement/>
<textFieldExpression><![CDATA["Subject : " + $F{Course}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="ea82c278-d2f3-4467-bf5d-8dab9ff99ae3" x="180" y="0" width="277" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{Marks}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
代わりにサブレポートを使用する場合
サブレポートは、特定の学生のデータを示す学生IDでパラメータ化されていると思います。メインレポートStudentName
には、詳細パネルにフィールドとサブレポートが表示されます。
total
学生の合計を計算する変数をサブレポートに作成します。
totalByStudent
計算タイプをに設定して、メインレポートに変数を作成しますSystem
。
- サブレポートをクリックし、プロパティパネルで[戻り値]をクリックします。[追加]をクリックして、次を選択します。サブレポート変数:合計、ローカル宛先変数:totalByStudent、残りはデフォルトのままにします。[OK]をクリックします。
totalByStudent
変数を詳細バンドにドラッグアンドドロップします。それを選択し、[プロパティ]パネルで[評価時間をバンド]に設定します。出力は上記と同じになります。
レポートの複雑さを軽減し、この方法のパフォーマンスが向上するため、レポートグループと変数を使用したアプローチを使用することをお勧めします。