JasperReports - 报表填充



任何报表工具的主要目的是生成高质量的文档。报表填充过程通过操作数据集来帮助报表工具实现这一目标。

报表填充过程所需的主要输入包括:

  • 报表模板 - 这是实际的 JasperReport 文件。

  • 报表参数 - 这些基本上是在报表填充时传递给引擎的命名值。我们将在报表参数章节中详细讨论它们。

  • 数据源 - 我们可以从各种数据源填充 Jasper 文件,例如 SQL 查询、XML 文件、CSV 文件、HQL(Hibernate 查询语言)查询、Java Bean 集合等。这将在报表数据源章节中详细讨论。

此过程生成的输出是.jrprint文档,该文档已准备好进行查看、打印或导出到其他格式。外观类net.sf.jasperreports.engine.JasperFillManager通常用于使用数据填充报表模板。此类具有各种fillReportXXX()方法,这些方法填充报表模板(模板可以位于磁盘上、从输入流中获取或直接作为内存中的数据提供)。

此外观类中存在两种类型的fillReportXXX()方法:

  • 第一种类型以java.sql.Connection对象作为第三个参数。大多数情况下,报表都是使用关系数据库中的数据填充的。这是通过以下方式实现的:

    • 通过 JDBC 连接到数据库。

    • 在报表模板中包含 SQL 查询。

    • JasperReports 引擎使用传递的连接并执行 SQL 查询。

    • 因此,为填充报表生成了报表数据源。

  • 第二种类型接收net.sf.jasperreports.engine.JRDataSource对象,当需要填充的数据以其他形式可用时。

填充报表模板

让我们编写一个报表模板。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)的集合传递给 JasperReport 引擎,以填充此编译后的报表。

编写一个 POJO DataBean.java,它表示数据对象(Java bean)。此类定义了两个 String 对象,即“name”和“country”。将其保存到目录C:\tools\jasperreports-5.0.1\test\src\com\tutorialspoint

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

编写一个类 DataBeanList.java,它具有生成 Java bean 对象集合的业务逻辑。这将进一步传递给 JasperReports 引擎以生成报表。在这里,我们在列表中添加了 4 个 DataBean 对象。将其保存到目录C:\tools\jasperreports-5.0.1\test\src\com\tutorialspoint

package com.tutorialspoint;

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\tutorialspoint

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

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

生成报表

我们现在将使用常规的 ANT 构建过程编译并执行这些文件。build.xml 文件如下所示:

导入文件 - baseBuild.xml 从环境设置章节中获取,应放置在与 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.tutorialspoint.JasperReportFillexecutereport是默认目标)如下所示:

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

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

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.

BUILD SUCCESSFUL
Total time: 8 seconds

作为上述执行的结果,在与.jasper文件相同的目录中生成一个文件jasper_report_template.jrprint(在本例中,它生成在 C:\tools\jasperreports-5.0.1\test)。

广告

© . All rights reserved.