📜  报告组

📅  最后修改于: 2020-11-16 08:00:49             🧑  作者: Mango


JasperReports中的组以逻辑方式帮助组织报告数据。报告组代表数据源中一系列连续的记录,这些记录具有一些共同点,例如某些报告字段的值。报告组由元素定义。报告可以有任意数量的组。声明后,可以在整个报告中引用组。

报告组包含三个元素-

  • 组表达式-这表示必须更改以启动新数据组的数据。

  • 组标题部分-帮助将标签放置在分组数据的开头。

  • 组页脚部分-帮助将标签放置在分组数据的末尾。

在报表填充时通过数据源进行迭代的过程中,如果组表达式的值发生更改,则会发生组破裂,并将相应的部分插入到生成的文档中。

报表组机制不对数据源提供的数据执行任何排序。仅当已经根据报表中使用的组表达式对数据源中的记录进行了排序时,数据分组才能按预期工作。

组属性

元素包含允许我们控制如何排列分组数据的属性。属性汇总在下表中-

S.NO Attribute and Description
1

name

This is mandatory. It references the group in report expressions by name. It follows the same naming conventions that we mentioned for the report parameters, fields, and report variables. It can be used in other JRXML attributes when you want to refer a particular report group.

2

isStartNewColumn

When set to true, each data group will begin on a new column. Default value is false.

3

isStartNewPage

When set to true, each data group will begin on a new page. Default value is false.

4

isResetPageNumber

When set to true, the report page number will be reset every time a new group starts. Default value is false.

5

isReprintHeaderOnEachPage

When set to true, the group header will be reprinted on every page. Default value is false.

6

minHeightToStartNewPage

Defines minimum amount of vertical space needed at the bottom of the column in order to place the group header on the current column. The amount is specified in report units.

7

footerPosition

Renders position of the group footer on the page, as well as its behavior in relation to the report sections that follow it.Its value can be: Normal, StackAtBottom, ForceAtBottom, and CollateAtBottom. Default value is Normal.

8

keepTogether

When set to true, prevents the group from splitting on its first break attempt.

让我们向现有报告模板(“章报表设计” )添加一个组(“ CountryGroup )。计算每个国家/地区的出现次数,并将其显示为组页脚。在组标题中,每个记录的计数都带有前缀。修改后的报告模板(jasper_report_template.jrxml)如下。将其保存到C:\ tools \ jasperreports-5.0.1 \ test目录-






   
   

   
      
   

   
      
   

   
      
   
   
   
   
   
   
      
   
   
   
      
      
      
         
            
            
               
               
               
                  
               
               
               
               
                  
               
               
               
                  
               
            
         
         
      
      
      
         
            
            
               
               
               
            
            
            
               
               
               
               
                  
               
            
         
         
      
   
   
   
   
      <band height="70">
         
         <line>
            <reportelement x="0" y="0" width="515" height="1"></reportelement>
         </line>
         
         <textfield isblankwhennull="true" bookmarklevel="1">
            <reportelement x="0" y="10" width="515" height="30"></reportelement>
            
            <textelement textalignment="Center">
               <font size="22"></font>
            </textelement>
            
            <textfieldexpression class="java.lang.String">
               
            </textfieldexpression>
            
            <anchornameexpression>
               
            </anchornameexpression>
         </textfield>
            
         <textfield isblankwhennull="true">
            <reportelement x="0" y="40" width="515" height="20"></reportelement>
            
            <textelement textalignment="Center">
               <font size="10"></font>
            </textelement>
            
            <textfieldexpression class="java.lang.String">
               
            </textfieldexpression>
         </textfield>
      
      </band>
   

   
      
         
         
            
            
            
               
            
            
            
                
            
               
            
         
         
         
            
            
            
               
            
            
            
         
         
         
            
            
            
               
            
            
            
         
      
      
   

   
      
         
         
            
            
            
               
            
                
            
                
            
                 
            
         
         
         
            
            
            
               
            
            
            
               
            
         
         
         
            
            
            
            
               
            
         
      
      
   
    

用于报告填充的Java代码保持不变。文件C:\ tools \ jasperreports-5.0.1 \ test \ src \ com \ tutorialspoint \ JasperReportFill.java的内容如下-

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

public class JasperReportFill {
   @SuppressWarnings("unchecked")
   public static void main(String[] args) {
      String sourceFileName =
      "C://tools/jasperreports-5.0.1/test/jasper_report_template.jasper";

      DataBeanList DataBeanList = new DataBeanList();
      ArrayList dataList = DataBeanList.getDataBeanList();

      JRBeanCollectionDataSource beanColDataSource =
      new JRBeanCollectionDataSource(dataList);

      Map parameters = new HashMap();
      /**
       * Passing ReportTitle and Author as parameters
       */
      parameters.put("ReportTitle", "List of Contacts");
      parameters.put("Author", "Prepared By Manisha");

      try {
         JasperFillManager.fillReportToFile(
         sourceFileName, parameters, beanColDataSource);
      } catch (JRException e) {
         e.printStackTrace();
      }
   }
}

POJO文件C:\ tools \ jasperreports-5.0.1 \ test \ src \ com \ tutorialspoint \ DataBean.java的内容如下-

package com.tutorialspoint;

public class DataBean {
   private String name;
   private String country;

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public String getCountry() {
      return country;
   }

   public void setCountry(String country) {
      this.country = country;
   }
}

文件C:\ tools \ jasperreports-5.0.1 \ test \ src \ com \ tutorialspoint \ DataBeanList.java的内容如下所示-

package com.tutorialspoint;

import java.util.ArrayList;

public class DataBeanList {
   public ArrayList getDataBeanList() {
      ArrayList dataBeanList = new ArrayList();

      dataBeanList.add(produce("Manisha", "India"));
      dataBeanList.add(produce("Dennis Ritchie", "USA"));
      dataBeanList.add(produce("V.Anand", "India"));
      dataBeanList.add(produce("Shrinath", "California"));

      return dataBeanList;
   }

   /**
    * This method returns a DataBean object,
    * with name and country set in it.
    */
   private DataBean produce(String name, String country) {
      DataBean dataBean = new DataBean();
      dataBean.setName(name);
      dataBean.setCountry(country);
      
      return dataBean;
   }
}

报告生成

我们将使用常规的ANT构建过程来编译并执行上述文件。文件build.xml的内容(保存在目录C:\ tools \ jasperreports-5.0.1 \ test下)如下。

导入文件-baseBuild.xml是从“环境设置”一章中提取的,应与build.xml放在同一目录中。



   
   
   
      
      
         
         
      
   
   
   
      
      
         
      
      
      
         
            
               
            
         
         
      
   
   
    

接下来,让我们打开命令行窗口并转到build.xml所在的目录。最后,将命令ant -Dmain-class = com.tutorialspoint.JasperReportFill (viewFullReport是默认目标)执行为-

C:\tools\jasperreports-5.0.1\test>ant -Dmain-class=com.tutorialspoint.JasperReportFill
Buildfile: C:\tools\jasperreports-5.0.1\test\build.xml

clean-sample:
   [delete] Deleting directory C:\tools\jasperreports-5.0.1\test\classes
   [delete] Deleting: C:\tools\jasperreports-5.0.1\test\jasper_report_template.jasper
   [delete] Deleting: C:\tools\jasperreports-5.0.1\test\jasper_report_template.jrprint

compile:
   [mkdir] Created dir: C:\tools\jasperreports-5.0.1\test\classes
   [javac] C:\tools\jasperreports-5.0.1\test\baseBuild.xml:28: warning:
   'includeantruntime' was not set, defaulting to build.sysclasspath=last;
   set to false for repeatable builds
   [javac] Compiling 7 source files to C:\tools\jasperreports-5.0.1\test\classes

compilereportdesing:
   [jrc] Compiling 1 report design files.
   [jrc] log4j:WARN No appenders could be found for logger
   (net.sf.jasperreports.engine.xml.JRXmlDigesterFactory).
   [jrc] log4j:WARN Please initialize the log4j system properly.
   [jrc] log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig
   for more info.
   [jrc] File : C:\tools\jasperreports-5.0.1\test\jasper_report_template.jrxml ... OK.

run:
   [echo] Runnin class : com.tutorialspoint.JasperReportFill
   [java] log4j:WARN No appenders could be found for logger
   (net.sf.jasperreports.extensions.ExtensionsEnvironment).
   [java] log4j:WARN Please initialize the log4j system properly.

viewFillReport:
   [java] log4j:WARN No appenders could be found for logger
   (net.sf.jasperreports.extensions.ExtensionsEnvironment).
   [java] log4j:WARN Please initialize the log4j system properly.

BUILD SUCCESSFUL
Total time: 18 seconds

经过以上编译,JasperViewer窗口打开,如下图所示:

Jasper报告组示例

在这里,我们看到每个国家都被分组了,每个国家的出现次数都显示在每个组的页脚处。