Jasper-reports-jasper-view-and-print-reports

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

ジャスパーレポート-レポートの表示と印刷

レポート入力プロセス_JasperPrintオブジェクト_の出力は、組み込みのビューアコンポーネントを使用して表示したり、印刷したり、PDF、HTML、RTF、XLS、ODT、CSV、XMLなどの一般的なドキュメント形式にエクスポートしたりできます。 Jasperドキュメントの表示と印刷についてはこの章で説明し、エクスポートについては次の章で説明します。 リンク:/jasper_reports/jasper_exporting_reports ['レポートのエクスポート']

レポートを表示する

JasperReportは、生成されたレポートを元の形式で表示するためのビルトインビューアーを提供します。 これはスイングベースのコンポーネントであり、他のJavaアプリケーションは、表示または印刷するためにドキュメントを他の形式にエクスポートすることなく、このコンポーネントを統合できます。 _net.sf.jasperreports.view.JRViewer_クラスは、この視覚的なコンポーネントを表します。 このクラスは、サブクラス化することにより、アプリケーションのニーズに応じてカスタマイズすることもできます。

JasperReportsには、レポートを表示するための視覚コンポーネントを使用するSwingアプリケーションもあります。 このアプリケーションは、*。jrprintが生成されるのと同じ形式でレポートを表示するのに役立ちます。 このSwingアプリケーションは、_net.sf.jasperreports.view.JasperViewer_クラスに実装されています。 このクラスを使用してレポートを表示するには、ANTターゲットにラップする必要があります。

生成されたレポートの表示

次の例は、JasperViewerクラスを使用してレポートを表示する方法を示しています-

レポートテンプレートを作成しましょう。 JRXMLファイル(C:\ tools \ jasperreports-5.0.1 \ test \ jasper_report_template.jrxml)の内容は以下のとおりです-

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN"
   "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">

<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" language = "groovy" pageWidth = "595"
   pageHeight = "842" columnWidth = "555" leftMargin = "20" rightMargin = "20"
   topMargin = "20" bottomMargin = "20">

   <queryString>
      <![CDATA[]]>
   </queryString>

   <field name = "country" class = "java.lang.String">
      <fieldDescription><![CDATA[country]]></fieldDescription>
   </field>

   <field name = "name" class = "java.lang.String">
      <fieldDescription><![CDATA[name]]></fieldDescription>
   </field>

   <columnHeader>
      <band height = "23">

         <staticText>
            <reportElement mode = "Opaque" x = "0" y = "3"
               width = "535" height = "15" backcolor = "#70A9A9"/>

            <box>
               <bottomPen lineWidth = "1.0" lineColor = "#CCCCCC"/>
            </box>

            <textElement/>
            <text><![CDATA[]]> </text>
         </staticText>

         <staticText>
            <reportElement x = "414" y = "3" width = "121" height = "15"/>

            <textElement textAlignment = "Center" verticalAlignment = "Middle">
               <font isBold = "true"/>
            </textElement>
            <text><![CDATA[Country]]></text>
         </staticText>

         <staticText>
            <reportElement x = "0" y = "3" width = "136" height = "15"/>

            <textElement textAlignment = "Center" verticalAlignment = "Middle">
               <font isBold = "true"/>
            </textElement>
            <text><![CDATA[Name]]></text>
         </staticText>

      </band>
   </columnHeader>

   <detail>
      <band height = "16">

         <staticText>
            <reportElement mode = "Opaque" x = "0" y = "0"
               width = "535" height = "14" backcolor = "#E5ECF9"/>

            <box>
               <bottomPen lineWidth = "0.25" lineColor = "#CCCCCC"/>
            </box>

            <textElement/>
            <text><![CDATA[]]> </text>
         </staticText>

         <textField>
            <reportElement x = "414" y = "0" width = "121" height = "15"/>

            <textElement textAlignment = "Center" verticalAlignment = "Middle">
               <font size = "9"/>
            </textElement>

            <textFieldExpression class = "java.lang.String">
               <![CDATA[$F{country}]]>
            </textFieldExpression>
         </textField>

         <textField>
            <reportElement x = "0" y = "0" width = "136" height = "15"/>
            <textElement textAlignment = "Center" verticalAlignment = "Middle"/>

            <textFieldExpression class = "java.lang.String">
               <![CDATA[$F{name}]]>
            </textFieldExpression>
         </textField>

      </band>
   </detail>

</jasperReport>

次に、Javaデータオブジェクト(Java Bean)のコレクションをJasperReports Engineに渡して、このコンパイル済みレポートに入力します。

データオブジェクト(Java Bean)を表すPOJO DataBean.javaを記述します。 このクラスは、2つのStringオブジェクト、つまり 「名前」と「国」。それをディレクトリ C:\ tools \ jasperreports-5.0.1 \ test \ src \ com \ finddevguides に保存します。

package com.finddevguides;

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;
   }
}

Java Beanオブジェクトのコレクションを生成するビジネスロジックを持つクラスDataBeanList.javaを記述します。 これはさらにJasperReportsエンジンに渡され、レポートを生成します。 ここでは、リストに4つのDataBeanオブジェクトを追加しています。 それをディレクトリ C:\ tools \ jasperreports-5.0.1 \ test \ src \ com \ finddevguides に保存します。

package com.finddevguides;

import java.util.ArrayList;

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

      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;
   }
}

メインクラスファイル JasperReportFill.java を作成します。このファイルは、クラス(DataBeanList)からJava Beanコレクションを取得し、それをJasperReportsエンジンに渡して、レポートテンプレートに入力します。 それをディレクトリ C:\ tools \ jasperreports-5.0.1 \ test \ src \ com \ finddevguides に保存します。

package com.finddevguides;

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<DataBean> dataList = DataBeanList.getDataBeanList();

      JRBeanCollectionDataSource beanColDataSource = new
         JRBeanCollectionDataSource(dataList);

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

ターゲット viewFillReport をbuild.xmlファイルに書き込みましょう。 build.xmlファイルは次のとおりです-

インポートファイル-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 を実行します(viewFillReportがデフォルトのターゲットです)。 その結果、以下の画面に示すように、JasperViewerウィンドウが表示されます-

Jasper Report Viewer

レポートの印刷

JasperReportsライブラリによって生成されたドキュメントを独自の形式(つまり、 _JasperPrint_オブジェクト)_net.sf.jasperreports.engine.JasperPrintManager_クラスを使用します。 これは、Java 2 Printing APIに依存するファサードクラスです。 JasperReportドキュメントがHTMLやPDFなどの他の形式にエクスポートされたら、ドキュメントを印刷することもできます。

生成されたレポートの印刷

次のコードは、レポートの印刷を示しています。 既存のクラスJasperReportFillを更新しましょう。 JasperPrintManager.printReport()_メソッドを使用します。 このメソッドは、ソースファイル名(ここでは、JasperFillManager.fillReportToFile()メソッドを使用して前のステップで生成した.jrprint_ファイルを渡します)を最初のパラメーターとして使用します。 2番目のパラメーターは、標準の印刷ダイアログを表示するためのブール値です(ここでは true に設定しています)。

package com.finddevguides;

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.JasperPrintManager;
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";
      String printFileName = null;
      DataBeanList DataBeanList = new DataBeanList();
      ArrayList<DataBean> dataList = DataBeanList.getDataBeanList();

      JRBeanCollectionDataSource beanColDataSource = new
         JRBeanCollectionDataSource(dataList);

      Map parameters = new HashMap();
      try {
           printFileName = JasperFillManager.fillReportToFile(
            sourceFileName, parameters, beanColDataSource);
         if(printFileName != null){
            JasperPrintManager.printReport( printFileName, true);
         }
      } catch (JRException e) {
         e.printStackTrace();
      }
   }
}

次に、このファイルをディレクトリ C:\ tools \ jasperreports-5.0.1 \ test \ src \ com \ finddevguides に保存します。 ANTを使用してこのファイルをコンパイルして実行します。 build.xmlの内容は以下のとおりです-

<?xml version = "1.0" encoding = "UTF-8"?>
<project name = "JasperReportTest" default = "executereport" basedir = ".">
   <import file = "baseBuild.xml"/>

   <target name = "executereport" depends = "compile,compilereportdesing,run">
      <echo message = "Im here"/>
   </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.JasperReportPrint を実行します。 その結果、印刷​​ダイアログボックスが表示されます。 [OK]をクリックしてドキュメントを印刷します。