JasperReports報表組
組在JasperReports的協助組織對報告的數據以邏輯方式。報告組代表連續記錄的數據源中有一些共同點,比如某個報表字段的值的序列。報告組由
報告組有三個要素:
Group expression: 這表示必須改變,以啓動一個新的數據組中的數據。
Group header section: 幫助位置標籤在分組數據的開始。
Group footer section: : 幫助位置標籤在分組數據的末尾。
在截至於若該組表達變化,一組發生斷裂和相應的
報表組的機制不執行由數據源所提供的資料的任何排序。數據分組按預期工作,只有當數據源中的記錄按照報告中使用的組表達式已經下令。
屬性組
屬性
描述
name
這是強制性的。它通過名稱引用該組中的報表表達式。它遵循相同的命名約定我們,我們提到的報告參數,字段和報表變量。它可以在其他JRXML屬性被用於當想引用一個特定的報告組。
isStartNewColumn
當設置爲true時,每個數據組將開始一個新的列。默認值是false
isStartNewPage
當設置爲true時,每個數據組將開始一個新的頁面上。默認值是false
isResetPageNumber
當設置爲true,該報告頁碼將每一個新組開始時被重置。默認值是false
isReprintHeaderOnEachPage
當設置爲true時,組頭會被重印每一頁上。默認值是false
minHeightToStartNewPage
定義在列的底部,以便將組頭當前列所需要的垂直空間最小量。被指定在報告單位的數量。
footerPosition
呈現在頁面上的組頁腳的位置,以及其有關的報告的部分它後面的行爲。它的值可以是: Normal, StackAtBottom, ForceAtBottom, CollateAtBottom.默認值是 Normal
keepTogether
當設置爲true,將阻止該集團從分割它第一次突破的嘗試
例子
讓我們添加一個組(CountryGroup)現有的報告模板(章報表設計)。每個國家的次數進行計數,計數顯示爲組頁腳。在組頭中每個記錄的計數前綴。修訂後的報告模板(jasper_report_template.jrxml)如下。將其保存到 C: oolsjasperreports-5.0.1 est 目錄:
<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"/>
在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; } }
報表生成
我們將編譯和執行使用我們常規Ant構建過程上面的文件.build.xml文件中的內容(根據目錄保存 C: oolsjasperreports-5.0.1 est)如下:
<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" /> <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" /> <jrc destdir=".">
接下來,讓我們打開命令行窗口並轉到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窗口打開如下面的屏幕:
在這裏,我們看到,各個國家分組和發生每個國家的計數顯示在每個組頁腳。