Jasper-reports-jasper-report-sections

提供:Dev Guides
移動先:案内検索

レポートセクション

簡単なレポートテンプレートの構造については、章のリンク:/jasper_reports/jasper_getting_started [Getting Started]で説明しました。 同様の行で、JasperReportsはレポートテンプレートを複数のセクションに構造化します。 セクションは、指定された高さを持つレポートの一部であり、線、長方形、画像、テキストフィールドなどのレポートオブジェクトを含めることができます。

レポートエンジンは、レポート入力時に、指定されたレポートデータソースの仮想レコードを反復処理します。 各セクションの定義された動作に応じて、エンジンは適切なときに各レポートセクションをレンダリングします。 たとえば、詳細セクションは、データソースの各レコードに対して表示されます。 改ページが発生すると、ページヘッダーセクションとページフッターセクションが必要に応じてレンダリングされます。

JasperReportsでは、用語とレポートセクションは「レポートバンド」とも呼ばれます。 セクションは、1つ以上のバンドで構成されます。 これらのセクションは、レポート生成時に繰り返し記入され、最終ドキュメントを準備します。

主なセクション

JasperReportsのレポートテンプレートには、次のメインセクションがあります-

<title></title>

<pageheader></pageheader>

<columnheader></columnheader>

<groupheader></groupheader>

<detail></detail>

<groupfooter></groupfooter>

<columnfooter></columnfooter>

<pagefooter></pagefooter>

<lastpagefooter></lastpagefooter>

<summary></summary>

<nodata></nodata>

<background></background>

次の表は、各セクションをまとめたものです-

S.NO Section and Description
1

Title

このセクションは、レポートの最初に1回だけ表示されます。

2

Page Header

このセクションは、生成されたドキュメントの各ページの先頭に表示されます。

3

Column Header

このセクションは、生成されたドキュメントの各列の先頭に表示されます。 レポートに定義されている列が1つだけの場合、列ヘッダーとフッターセクションは無視されます。

4

Group Header

このセクションは、レポートグループによって導入されます(チャプターリンク:/jasper_reports/jasper_report_groups [Groups])。 グループ化式の値が変更されるたびに、グループヘッダーセクションが詳細セクションの上に印刷されます。 複数のグループが定義されている場合、グループヘッダーはグループ定義の順序で印刷されます。

5

Detail

このセクションは、レポートのデータソースから提供されるデータの各行に対して繰り返されます。 詳細セクションは、複数のバンドで構成できます。

6

Group Footer

このセクションは、レポートグループによって導入されます(チャプターリンク:/jasper_reports/jasper_report_groups [Groups])。 グループ化式の値が変更される前に、グループフッターセクションが詳細セクションの下に印刷されます。 グループフッターは、データソースのデータの最後の行に対して常に印刷されます。 複数のグループが定義されている場合、グループフッターはグループ定義の逆の順序で印刷されます。

7

Column Footer

このセクションは、各列の下部に表示されます。 レポートの列数が1の場合、列ヘッダーセクションとフッターセクションは無視されます。

8

Page Footer

このセクションは、各ページの下部に表示されます。

9

Last Page Footer

このセクションは、レポートの最後のページの通常のページフッターを置き換えます。 場合によっては、要約セクションも存在するため、これはドキュメントの最後のページではない可能性があります。 このセクションは、最後のページの下部に要約情報を表示する必要がある場合に役立ちます。

10

Summary

このセクションは、レポートの最後に一度だけ表示されます。

11

No Data

このセクションは、[データ印刷レポートなしの場合]プロパティが[データなしセクション]に設定されている場合に印刷されます。 <noData>セクションがレポートテンプレートで定義され、データソースが空の場合、<noData>セクションが記入時に考慮される唯一のセクションになり、そのコンテンツがレポート出力を生成します。

12

Background

背景セクションはすべてのページに表示され、次のページにオーバーフローすることはできません。 このセクションに配置された要素は、ページの初期化時に評価され、バックグラウンドで表示されます。 他のすべてのページオブジェクトは、背景オブジェクトの上に表示されます。 このセクションは、ページの透かしを作成するのに役立ちます。

セクション、要素、属性の関係

次の図は、レポートのセクション内の要素と属性の関係を示しています。

レポートセクション

セクション要素

上記のレポートセクションはすべてオプションです。 ただし、どのレポートテンプレートにも、そのようなセクションが少なくとも1つあります。 これらの各セクションには、単一の<* band >要素が唯一のサブ要素として含まれています。 < band *>には、ゼロ個以上の次のサブ要素を含めることができます-

_ <line>、<rectangle>、<ellipse>、<image>、<staticText>、<textField>、<subReport>、または<elementGroup> _

これらの各要素には、最初の要素として1つの<* 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* -グループ内で最も高いオブジェクトに対応するように要素が伸縮します。 *RelativeToBand* -要素はバンドの高さに合わせて伸縮します。
positionType Specifies the element’s position when the band stretches.

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

  • FixRelativeToTop(デフォルト)*-要素は、バンドの上部に対して固定位置を維持します。

    *FixRelativeToBottom* -要素は、バンドの底に対して固定位置を維持します。
isPrintRepeatedValues Specifies if repeated values are printed.

true (default) − Repeated values will be printed.

  • false* -繰り返される値は印刷されません。
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 ディレクトリに保存します。 このファイルでは、各セクションにテキストを表示します(上で説明しました)。 ファイルの内容は以下のとおりです-

<?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 = "jasper_report_template" pageWidth = "300" pageHeight = "300"
   columnWidth = "300" leftMargin = "0" rightMargin = "0"
   topMargin = "0" bottomMargin = "0" >

   <title>
      <band height = "50">

         <textField>
            <reportElement x = "100" y = "16" width = "100" height = "20"/>
            <textElement/>

            <textFieldExpression>
               <![CDATA["Title"]]>
            </textFieldExpression>

         </textField>

      </band>
   </title>

   <pageHeader>
      <band height = "40">

         <textField>
            <reportElement  mode = "Opaque" x = "100" y = "10"
               width = "90" height = "20"/>

            <textElement>
               <font isBold = "true"/>
            </textElement>

            <textFieldExpression>
               <![CDATA["Page Header"]]>
            </textFieldExpression>
         </textField>

      </band>
   </pageHeader>

   <columnHeader>
      <band height = "40">

         <textField>
            <reportElement  x = "100" y = "10" width = "90" height = "20"/>

            <textElement>
               <font isItalic = "true"/>
            </textElement>

            <textFieldExpression>
               <![CDATA["Column Header"]]>
            </textFieldExpression>
         </textField>

      </band>
   </columnHeader>

   <detail>
      <band height ="40">

         <textField>
            <reportElement mode = "Opaque" x = "100" y = "10"
               width = "90" height = "20" backcolor = "#99CCFF"/>
            <textElement/>

            <textFieldExpression>
               <![CDATA["Report Details"]]>
            </textFieldExpression>
         </textField>

      </band>
   </detail>

   <columnFooter>
      <band height = "40">

         <textField>
            <reportElement  x = "100" y = "10" width = "90" height = "20"/>
            <textElement/>

            <textFieldExpression>
               <![CDATA["Column Footer"]]>
            </textFieldExpression>
         </textField>

      </band>
   </columnFooter>

   <pageFooter>
      <band height = "40">

         <textField>
            <reportElement  x = "100" y = "10" width = "90" height = "20"/>
            <textElement/>

            <textFieldExpression>
               <![CDATA["Page Footer"]]>
            </textFieldExpression>
         </textField>

      </band>
   </pageFooter>

   <lastPageFooter>
      <band height = "40">

         <textField>
            <reportElement  x = "100" y = "10" width = "90" height = "20"/>
            <textElement/>

            <textFieldExpression>
               <![CDATA["Last Page Footer"]]>
            </textFieldExpression>
         </textField>

      </band>
   </lastPageFooter>

   <summary>
      <band height = "40">

         <textField>
            <reportElement  x = "100" y = "10" width = "90" height = "20"/>
            <textElement/>

            <textFieldExpression>
               <![CDATA["Summary"]]>
            </textFieldExpression>
         </textField>

      </band>
   </summary>

</jasperReport>

レポートに入力して生成するJavaコードを以下に示します。 このファイル JasperReportFill.java をC:\ tools \ jasperreports-5.0.1 \ test \ src \ com \ finddevguidesディレクトリに保存しましょう。

package com.finddevguides;

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のインスタンスを使用して、1つのレコードを含むデータソースをシミュレートしますが、この単一のレコードのすべてのフィールドはnullです。

レポート生成

通常のANTビルドプロセスを使用して、上記のファイルをコンパイルおよび実行します。 ファイルbuild.xmlの内容(ディレクトリC:\ tools \ jasperreports-5.0.1 \ testに保存)は以下のとおりです。

インポートファイル-baseBuild.xmlは、チャプターリンク:/jasper_reports/jasper_environment_setup [Environment Setup]から取得され、build.xmlと同じディレクトリに配置する必要があります。

<?xml version = "1.0" encoding = "UTF-8"?>
<project name = "JasperReportTest" default = "viewFillReport" basedir = ".">

   <import file = "baseBuild.xml"/>
   <target name = "viewFillReport" depends = "compile,compilereportdesing,run"
      description = "Launches the report viewer to preview
      the report stored in the .JRprint file.">

      <java classname = "net.sf.jasperreports.view.JasperViewer" fork = "true">
         <arg value = "-F${file.name}.JRprint"/>
         <classpath refid = "classpath"/>
      </java>

   </target>

   <target name = "compilereportdesing" description = "Compiles the JXML file and
      produces the .jasper file.">

      <taskdef name = "jrc"
         classname = "net.sf.jasperreports.ant.JRAntCompileTask">
         <classpath refid = "classpath"/>
      </taskdef>

      <jrc destdir = ".">
         <src>
            <fileset dir = ".">
               <include name = "*.jrxml"/>
            </fileset>
         </src>
         <classpath refid = "classpath"/>
      </jrc>

   </target>

</project>

次に、コマンドラインウィンドウを開き、build.xmlが配置されているディレクトリに移動します。 最後に、次のようにコマンド ant -Dmain-class = com.finddevguides.JasperReportFill (viewFullReportがデフォルトのターゲットです)を実行します-

C:\tools\jasperreports-5.0.1\test>ant -Dmain-class=com.finddevguides.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/faql#noconfig for more info.
   [jrc] File : C:\tools\jasperreports-5.0.1\test\jasper_report_template.jrxml ... OK.

run:
   [echo] Runnin class : com.finddevguides.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には<lastPageFooter>要素が含まれているため、表示されている<pageFooter>要素ではなく、レポートの最後のページに表示されることに注意してください。 <columnHeader>および<columnFooter>要素は、レポートに複数の列がある場合にのみ表示されます。