JasperReport報表參數
填充一個報表的主要輸入是:報表模板,參數和數據源。本章將介紹這些參數,並在接下來的章節中,我們將介紹數據源。
參數是在報表填充操作傳遞給報表引擎的對象引用。參數傳遞有用的數據到報表引擎,它可以不通過數據源來傳遞的數據是有用的。如作者姓名,報告等的標題數據,可以通過參數傳遞。 Jasper報表模板或JRXML模板可以包含零個或多個參數的元素。
參數聲明
參數聲明很簡單,如下所示:
<parameter name="exampleParameter" class="java.lang.String" />
name屬性
parameter>元素的name屬性是強製性的。它通過名稱引用的參數在報表表達式。參數名應該是一個單詞。它不應該包含任何特殊字符,如句號或逗號。
class屬性
class屬性也是強製性的,它指定了參數值的類名。它的默認值是java.lang.String。這是可以改變的,以在運行時可用任何類。不論報表參數的類型,引擎采用構造於$P{}標記是用來報表表達,從而使手工投射不必要。
內置參數
以下是預定義的報表參數,準備在表達式中使用:Parameter Name | 描述 |
---|---|
REPORT_PARAMETERS_MAP | 包含所有用戶定義和內置參數映射 |
REPORT_CONNECTION | 這指向用於JDBC數據源的用戶提供java.sql.Connection中 |
REPORT_DATA_SOURCE | 這是JRDataSource代表任一用戶提供的實例中的內置的數據源類型或用戶定義 |
REPORT_MAX_COUNT | 這是一個java.lang.Integer的值,從而允許用戶從數據源限製記錄。 |
REPORT_SCRIPTLET | 這指向net.sf.jasperreports.engine.JRAbstractScriptlet和包含報表的scriptlet,由用戶提供的一個實例 |
REPORT_LOCALE | 這是一個java.util.Locale的實例,包含資源包所需的語言環境 |
REPORT_RESOURCE_BUNDLE | 這指向java.util.ResourceBundle對象和包含本地化的消息 |
REPORT_TIME_ZONE | 這是一個java.util.TimeZone的實例,用於日期格式 |
REPORT_VIRTUALIZER | 這是net.sf.jasperreports.engine.JRVirtualizerobject一個實例,以及用於網頁的虛擬化(優化內存消耗) |
REPORT_CLASS_LOADER | 這是在報告充填過程中使用的加載,如圖像,字體和子報表模板資源java.lang.ClassLoader的實例 |
IS_IGNORE_PAGINATION | 如果設置為java.lang.Boolean.TRUE報告將在一個很長的網頁和分頁符來產生不會發生 |
例子
讓我們通過ReportTitle和 Author報表(由JasperReportFill.java生成)。經修訂的文件C: oolsjasperreports-5.0.1 estsrccomyiibaiJasperReportFill.java 如下:
package com.yiibai; 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(); /** * 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(); } } }
POJO文件的內容 C: oolsjasperreports-5.0.1 estsrccomyiibaiDataBean.java 如下所示:
package com.yiibai; 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; } }
文件 C: oolsjasperreports-5.0.1 estsrccomyiibaiDataBeanList.java 的內容如下:
package com.yiibai; 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; } }
讓我們添加參數<ReportTitle>和<AUTHOR>我們現有的報告模板(章報表設計)。報告標題和作者將在報表的開頭顯示。修訂後的報告模板(jasper_report_template.jrxml)如下。將其保存到 C: oolsjasperreports-5.0.1 est 目錄:
<?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>
報表生成
編譯和執行使用常規Ant構建過程上麵的文件。 build.xml文件中的內容(根據目錄保存C: oolsjasperreports-5.0.1 est)情況如下。
導入文件 - baseBuild.xml可以從環境設置章節中了解,並應放置在同一目錄中的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.yiibai.JasperReportFill (viewFullReport是默認的目標),如下所示:
C: oolsjasperreports-5.0.1 est>ant -Dmain-class=com.yiibai.JasperReportFill Buildfile: C: oolsjasperreports-5.0.1 estuild.xml clean-sample: [delete] Deleting directory C: oolsjasperreports-5.0.1 estclasses [delete] Deleting: C: oolsjasperreports-5.0.1 estjasper_report_template.jasper [delete] Deleting: C: oolsjasperreports-5.0.1 estjasper_report_template.jrprint compile: [mkdir] Created dir: C: oolsjasperreports-5.0.1 estclasses [javac] C: oolsjasperreports-5.0.1 estaseBuild.xml:28: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds [javac] Compiling 7 source files to C: oolsjasperreports-5.0.1 estclasses compilereportdesing: [jrc] Compiling 1 report design files. [jrc] log4j:WARN No appenders could be found for logger (net.sf.jasperreports.engine.xml.JRXmlDigesterFactory). [jrc] log4j:WARN Please initialize the log4j system properly. [jrc] log4j:WARN See http://logging.apache.org/log4j/1.2/faq.htmll#noconfig for more info. [jrc] File : C: oolsjasperreports-5.0.1 estjasper_report_template.jrxml ... OK. run: [echo] Runnin class : com.yiibai.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. viewFillReport: [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: 18 seconds
正如上文編譯的結果,JasperViewer窗口打開如下麵的屏幕:
在這裡,我們看到的是,報表頭 "List Of Contacts",並著有"Prepared By Manisha" 顯示在報表的開頭。