如何将子报告值传递给主报告? [英] How can I pass sub-report value to main-report?

查看:267
本文介绍了如何将子报告值传递给主报告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是关系表

StudentName
Course
Marks



Peter
Bio
65

Peter
Chem
70



Peter
Music
80



David
Chem
50

如何制作以下内容

Peter            Total : 215

Subject : Bio    -   65
Subject : Chem   -   70
Subject : Music  -   80

Peter            Total : 50

Subject : Chem   -   50


推荐答案

我不会使用子报告,因为这可以通过更简单的方式实现:您可以使用报告组和变量来实现此目的。确保数据根据输出进行排序。

I would not use a subreport for this as this can be achieved in a simpler way: You can utilize Report groups and variables to achieve this. Make sure that the data is sorted accordingly to the output.

准备


  • 根据 StudentName 创建报告组。右键单击报表检查器中的iReport,然后选择添加报表组。按照向导,给它一个名字(例如学生),选择字段 StudentName 作为组表达式,添加标题但不页脚。

  • 创建一个包含学生总数的变量。在 Variables 上的报告检查器中右键单击iReport,然后选择添加变量。在属性面板中,配置如下:名称:totalMarkByStudent,变量类:java.lang.Long,计算:总和,重置类型:组,重置组:学生,变量表达式:$ F {标记}。其余部分保留默认值。

  • Create a report group based on StudentName. Right Click in iReport in the report inspector on the report and select Add Report group. Follow the wizard, give it a name (e.g. Student), select field StudentName as group expression, add header but no footer.
  • Create a variable that holds the total for a student. Right Click in iReport in the report inspector on Variables and select Add Variable. In the properties panel configure as follows: Name: totalMarkByStudent, Variable Class: java.lang.Long, Calculation: Sum, Reset Type: Group, Reset Group: Student, Variable Expression: $F{Marks}. Leave the rest to its default values.

报告设计


  • 将Field StudentName 从报表检查器拖放到报表设计器中,放入Student组标题区。将会有一个弹出窗口询问应显示哪种值,选择字段值并单击确定。

  • 拖放变量 totalMarksByStudent 从报表检查器进入报表设计器进入Student组标题区。单击文本字段并在属性面板中修改以下设置:

  • Drag and drop the Field StudentName from the report inspector into the report designer into the Student group header band. There will be a popup asking what kind of value shall be displayed, select The field value and click Ok.
  • Drag and drop the Variable totalMarksByStudent from the report inspector into the report designer into the Student group header band. Click on the text field and modify following settings in the properties panel:


  • 宽度 200 (允许更多空间)

  • 文本字段表达式总计:+ $ V {totalMarkByStudent}

  • 表达式类 java.lang.String

  • 评估时间(意味着在完成组处理后评估该值)

  • 评估小组学生

  • Width: 200 (to allow more space)
  • Text field expression: "Total : " + $V{totalMarkByStudent}
  • Expression Class: java.lang.String
  • Evaluation Time: Group (meaning that the value gets evaluated once the processing of the group is finished)
  • Evaluation Group: Student

将报告检查器中的课程标记字段拖放到报告设计器的详细信息区域中。右键单击课程字段,然后选择编辑表达式。将表达式移植到主题:+ $ F {课程} ,然后单击应用。

Drag and drop the fields Course and Marks from the report inspector into the detail band in the report designer. Right click on the course field and select Edit Expression. CHange the expression to "Subject : " + $F{Course} and click on Apply.

使用此配置,您可以获得报告输出,如下图所示。

With this configuration you achieve a report output as in the image below.

供进一步参考这里是完整的JRXML使用报告组和变量:

For further reference here the complete JRXML using report group and variable:

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

如果您使用子报告

我假设子报告是paraemterized,学生ID显示给定学生的数据。主报告显示字段 StudentName 和详细信息面板中的子报告。

I assume that the subreport is paraemterized with the student id shows the data for a given student. The main report shows the field StudentName and the subreport in the detail panel.


  • 在子报表中创建一个变量 total ,用于计算学生的总计。

  • 在主报表中创建一个变量 totalByStudent ,计算类型设置为系统

  • 单击子报表,然后在属性面板中单击返回值。单击添加并选择:子报表变量:总计,本地目标变量:totalByStudent,休假保留为默认值。单击确定。

  • 将变量 totalByStudent 拖放到详细信息区域中。选择它,然后在属性面板中将评估时间设置为 Band 。输出与上面显示的相同。

  • Create a variable total in the subreport that calculates the total for the student.
  • Create a variable totalByStudent in the main report with calculation type set to System.
  • Click on the subreport and in the properties panel click on Return values. Click Add and select: Subreport variable: total, Local Destination Variable: totalByStudent, rest leave to default. Click on Ok.
  • Drag and drop the variable totalByStudent into the detail band. Select it and in the Properties panel set Evaluation time to Band. The output will be the same as shown above.

我建议将此方法与报告组和变量一起使用,因为它降低了报告的复杂性,以及这种方式的性能会更好。

I recommend to use the approach with report group and variable as it reduces the complexity of the report, as well as performance for this way would be better.

这篇关于如何将子报告值传递给主报告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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