📜  报告部分

📅  最后修改于: 2020-11-16 07:59:33             🧑  作者: Mango


我们在入门一章中讨论了简单报告模板的结构。类似地,JasperReports将报告模板分为多个部分。节是报表中具有指定高度的部分,可以包含报表对象,例如线条,矩形,图像或文本字段。

报表引擎在报表填充时会遍历提供的报表数据源的虚拟记录。根据每个部分的定义行为,引擎将在适当时呈现每个报告部分。例如,为数据源中的每个记录呈现详细信息部分。发生分页符时,将根据需要呈现页眉和页脚节。

在JasperReports中,术语和报告部分也称为报告带。部分由一个或多个带组成。这些部分在报告生成时重复填充,并准备最终文档。

主要部分

JasperReports中的报告模板具有以下主要部分-
























下表总结了每个部分-

S.NO Section and Description
1

Title

This section appears only once at the beginning of the report.

2

Page Header

This section appears at the beginning of each page in the generated document.

3

Column Header

This section appears at the beginning of each column in the generated document. If the report has only one column defined, then column header and footer sections are ignored.

4

Group Header

This section is introduced by a report group (chapter Groups). Each time the grouping expression changes its value, the group header section is printed above the detail section. In case, if more than one group is defined, the group header is printed in the order of group definition.

5

Detail

This section is repeated for each line of data supplied by the report’s data source. The detail section can be made of multiple bands.

6

Group Footer

This section is introduced by a report group (chapter Groups). The group footer section is printed below the detail section before the value of the grouping expression changes. The group footer is always printed for the last line of data in data source. In case, if more than one group is defined, the group footer is printed in the reverse order of group definition.

7

Column Footer

This section appears at the bottom of each column. If the report’s column count is 1, then column header and footer sections are ignored.

8

Page Footer

This section appears at the bottom of each page.

9

Last Page Footer

This section replaces the regular page footer on the last page of the report. In case, the summary section is also present, then this might not be the very last page of the document. This section is sometimes useful when summary information has to be displayed at the bottom of the last page.

10

Summary

This section appears only once at the end of the report.

11

No Data

This section is printed when the When No Data Print report property is set to No Data Section. If the section is defined in the report template, and if the data source is empty, then the section will be the only one taken into account at fill time, and its content will produce the report output.

12

Background

The background section is displayed on every page and cannot overflow to the next page. Elements placed on this section are evaluated at page initialization time and are displayed in the background. All other page objects are displayed on top of the background objects. This section is useful for creating page watermarks.

部分,元素和属性关系

下图显示了报告部分中的元素和属性关系。

报告部分

剖面元素

上述所有报告部分都是可选的。但是任何报告模板都将至少有一个这样的部分。这些部分中的每个部分都包含一个< band >元素作为其唯一的子元素。 < band >可以包含零个或多个以下子元素-

这些元素中的每个元素都必须包含一个< reportElement >作为其第一个元素(elementGroup除外)。 < reportElement >确定该特定元素的数据布局方式。与变量和参数不同,报表元素不需要具有名称,因为通常您不需要在报表模板中获取任何单独的元素。

下表总结了< reportElement >的属性-

Attribute Description Valid Values
x Specifies the x coordinate of the band element. An integer value indicating the x coordinate of the element in pixels. This attribute is required.
y Specifies the y coordinate of the band element. An integer value indicating the y coordinate of the element in pixels. This attribute is required.
width Specifies the width of the band element. An integer value indicating the element width in pixels. This attribute is required.
height Specifies the height of the band element. An integer value indicating the element height in pixels. This attribute is required.
key Unique identifier of band element. A unique string value.
stretchType Specifies how does the element stretch when the containing band stretches

NoStretch (default) − The element will not stretch.

RelativeToTallestObject − The element will stretch to accommodate the tallest object in its group.

RelativeToBand − The element will stretch to fit the band’s height.

positionType Specifies the element’s position when the band stretches.

Float − The element will move depending on the size of the surrounding elements.

FixRelativeToTop (default) − The element will maintain a fixed position relative to the band’s top.

FixRelativeToBottom − The element will maintain a fixed position relative to the band’s bottom.

isPrintRepeatedValues Specifies if repeated values are printed.

true (default) − Repeated values will be printed.

false − Repeated values will not be printed.

mode Specifies the background mode of the element Opaque, Transparent
isRemoveLineWhenBlank Specifies if the element should be removed when it is blank and there are no other elements in the same horizontal space. true, false
isPrintInFirstWholeBand Specifies if the element must be printed in a whole band, that is, a band that is not divided between report pages or columns. true, false
isPrintWhenDetailOverFlows Specifies if the element will be printed when the band overflows to a new page or column. true, false
printWhenGroupChanges Specifies that the element will be printed when the specified group changes. A string value.
forecolor Specifies the foreground color of the element. Either a hexadecimal RGB value preceded by the # character, or one of the following predefined values: black, blue, cyan, darkGray, gray, green, lightGray, magenta, orange, pink, red, yellow, white.
backcolor Specifies the background color of the element. Same as Valid Values for forecolor

节属性

以下是报告部分的属性-

高度

该部分的高度指定该特定部分的像素高度,这在整个报表设计中非常重要。

表达式时打印

一个布尔表达式,确定是否应打印该部分。

允许分割

一个标志,指示当该部分不适合当前页面时是否允许拆分。如果为true,则该部分将转移到下一页。请注意,如果该部分不适合下一页,则无论该标志的值如何,该部分都会被拆分。 splitType可以采用以下值-

  • splitType =“ Stretch:”拆分拉伸的内容。如果该部分在当前页面上伸展(如果可用空间小于声明的高度),则允许将添加到原始高度的区域拆分到下一页。

  • splitType =“ Prevent:”防止在第一次尝试时分裂。如果该部分不适用于下一页,则拆分会正常进行,因为防止频段拆分仅在第一次尝试拆分时有效。

  • splitType =“ Immediate:”立即拆分。除了上面最上面的元素,乐队可以拆分其他任何地方。

为了演示每个部分,让我们编写报告模板(jasper_report_template.jrxml)。将此文件保存到C:\ tools \ jasperreports-5.0.1 \ test目录。在此文件中,我们将在每个部分中显示一个文本(我们在上面讨论过)。该文件的内容如下所示-





   
      <band height="50">
         
         <textfield>
            <reportelement x="100" y="16" width="100" height="20"></reportelement>
            <textelement></textelement>
            
            <textfieldexpression>
               
            </textfieldexpression>
        
         </textfield>
      
      </band>
   
   
   
      
         
         
            
            
            
               
            
            
            
               
            
         
      
      
   
   
   
      
            
         
            
            
            
               
            
            
            
               
            
         
      
      
   
   
   
      
         
         
            
            
            
            
               
            
         
      
      
   
   
   
      
         
         
            
            
            
            
               
            
         
      
      
   
   
   
      
         
         
            
            
            
            
               
            
         
      
      
   
   
   
      
         
         
            
            
            
            
               
            
         
      
      
   
   
   
      
         
         
            
            
            
            
               
            
         
      
      
   
    

下面提供了用于填充和生成报告的Java代码。让我们将此文件JasperReportFill.java保存到C:\ tools \ jasperreports-5.0.1 \ test \ src \ com \ tutorialspoint目录。

package com.tutorialspoint;

import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;

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

      try {
         JasperFillManager.fillReportToFile(sourceFileName, null,
            new JREmptyDataSource());
      } catch (JRException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }

   }
}

在这里,当填充报表时,我们使用JREmptyDataSource的实例来模拟其中包含一个记录的数据源,但是该单个记录中的所有字段均为空。

报告生成

我们将使用常规的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, defau
   [javac] Compiling 1 source file 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.JRXmlDigesterFac
   [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.ExtensionsEnviro
   [java] log4j:WARN Please initialize the log4j system properly.

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

BUILD SUCCESSFUL
Total time: 18 minutes 22 seconds

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

Jasper报告部分示例

在这里,我们可以在每个部分中看到打印出的文本。请注意,由于JRXML包含元素,因此它将显示在报告的最后一页中,而不是显示元素。如果元素具有多个列,则只会显示在报告中。