填充一個報表的主要輸入是:報表模板,引數和資料源。本章將介紹這些引數,並在接下來的章節中,我們將介紹資料源。
引數是在報表填充操作傳遞給報表引擎的物件參照。引數傳遞有用的資料到報表引擎,它可以不通過資料源來傳遞的資料是有用的。如作者姓名,報告等的標題資料,可以通過引數傳遞。 Jasper報表模板或JRXML模板可以包含零個或多個引數的元素。
引數宣告很簡單,如下所示:
<parameter name="exampleParameter" class="java.lang.String" />
parameter>元素的name屬性是強制性的。它通過名稱參照的引數在報表表示式。引數名應該是一個單詞。它不應該包含任何特殊字元,如句號或逗號。
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" 顯示在報表的開頭。