JasperReports 报表参数

1 JasperReports 报表参数声明

填充报表的主要输入有 :报表模板、参数和数据源。本章将介绍参数,下一章将讨论数据源

参数是对象引用,它们在报表填充操作期间传递给报表引擎。不能通过数据源传递的数据,可以通过参数传递。作者姓名、报告标题等数据可以通过参数传递。JasperReports 模板或 JRXML 模板可以有零个或多个参数元素。

参数声明如下:

<parameter name = "exampleParameter" class = "java.lang.String" />

name属性

<parameter> 元素的name属性是必须的。它按名称引用报表表达式中的参数。参数名称应该是一个单词。它不应包含任何特殊字符,如点或逗号。

class属性

class属性也是必需的,它指定的参数值的类名。它的默认值是java.lang.String。这可以更改为运行时可用的任何类。无论报表参数的类型如何,引擎都会在使用 $P{} 标记的报表表达式中进行转换,因此无需手动转换。

报表参数值总是打包在一个 java.util.Map 对象中,该对象以参数名称为键。报表参数可用于报表的查询字符串中,以便进一步自定义从数据库中检索的数据集。这些在为报表提供数据的查询中充当动态过滤器。

2 JasperReports 报表内置参数

以下是预定义的报告参数,可以在报表表达式中使用:

参数 描述
REPORT_PARAMETERS_MAP 返回由指定数组支持的固定大小列表,类似于 Arrays.asList(Object[])。
REPORT_CONNECTION 如果可能,返回等于 value 的 int 值。
REPORT_DATA_SOURCE 比较两个指定的 int 值。
REPORT_MAX_COUNT 返回每个提供的数组中组合成单个数组的值。
REPORT_SCRIPTLET 如果目标作为数组中任何位置的元素存在,则返回 true。
REPORT_LOCALE 返回包含与数组相同的值的数组,但保证具有指定的最小长度。
REPORT_RESOURCE_BUNDLE 返回 int 值,其 big-endian 表示存储在字节的前 4 个字节中;相当于 ByteBuffer.wrap(bytes).getInt()。
REPORT_TIME_ZONE 以big-endian顺序返回字节表示为给定 4 个字节的 int 值;相当于 Ints.fromByteArray(new byte[] {b1, b2, b3, b4})。
REPORT_VIRTUALIZER 返回值的哈希码;等于调用 ((Integer) value).hashCode() 的结果。
REPORT_CLASS_LOADER 返回值目标在数组中第一次出现的索引。
IS_IGNORE_PAGINATION 返回指定目标在数组中第一次出现的起始位置,如果没有这样的出现,则返回 -1。

3 JasperReports 参数的示例

项目结构如下:

pom.xml文件

<dependencies>
        <!-- https://mvnrepository.com/artifact/net.sf.jasperreports/jasperreports -->
        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports</artifactId>
            <version>6.17.0</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>2.4.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>
    </dependencies>

DataBean实体类:

package com.yiidian;

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类:

package com.yiidian;

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

jasper_report_template.jrxml模板文件:

<?xml version = "1.0"?>
<!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" pageWidth = "595"
              pageHeight = "842" columnWidth = "515"
              leftMargin = "40" rightMargin = "40" topMargin = "50" bottomMargin = "50">

    <parameter name = "ReportTitle" class = "java.lang.String"/>
    <parameter name = "Author" class = "java.lang.String"/>

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

    <title>
        <band height = "70">

            <line>
                <reportElement x = "0" y = "0" width = "515" height = "1"/>
            </line>

            <textField isBlankWhenNull = "true" bookmarkLevel = "1">
                <reportElement x = "0" y = "10" width = "515" height = "30"/>

                <textElement textAlignment = "Center">
                    <font size = "22"/>
                </textElement>

                <textFieldExpression class = "java.lang.String">
                    <![CDATA[$P{ReportTitle}]]>
                </textFieldExpression>

                <anchorNameExpression>
                    <![CDATA["Title"]]>
                </anchorNameExpression>
            </textField>

            <textField isBlankWhenNull = "true">
                <reportElement  x = "0" y = "40" width = "515" height = "20"/>

                <textElement textAlignment = "Center">
                    <font size = "10"/>
                </textElement>

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

        </band>
    </title>

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

先编译报表文件:

package com.yiidian;

import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.view.JasperViewer;

public class JasperReportFill {

   public static void main(String[] args) {
      String filePath = "D:/idea_codes/jasper_reports/jasper_report_template.jrxml";
      try {
         JasperCompileManager.compileReportToFile(filePath,"d:/jasper_report_template.jasper");
         System.out.println("编译成功");
      } catch (JRException e) {
         e.printStackTrace();
      }
   }
}

编译后在D:/盘根目录下生成jasper_report_template.jasper文件,然后再填充数据:

package com.yiidian;

import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

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

public class JasperReportFill {

   public static void main(String[] args) {
         String sourceFileName = "d:/jasper_report_template.jasper";

         DataBeanList DataBeanList = new DataBeanList();
         ArrayList<DataBean> 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();
         }
      }
}

填充数据完毕后,在D:/盘根目录下生成jasper_report_template.jrprint文件,最后使用预览程序预览:

package com.yiidian;

import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.view.JasperViewer;

public class JasperReportFill {

   public static void main(String[] args) {
      String filePath = "D:/jasper_report_template.jrprint";
      try {
         JasperViewer.viewReport(filePath,false);
      } catch (JRException e) {
         e.printStackTrace();
      }
   }
}

最后输出结果如下:

从效果我们看到,报告标题“联系人列表”和作者“由 Manisha 准备”显示在报表的开头。

热门文章

优秀文章