JasperReport查看和打印報告

報表填充過程JasperPrint對象的輸出可以使用內置的瀏覽器組件來查看,打印或導出到更多的流行的文件格式,如PDF,HTML,RTF,XLS,ODT,CSV或XML。Jasper文件查看和打印將包括在本章中。導出將包括在下一章導出報表.

查看報表

JasperReport提供了一個內置的瀏覽器觀看原始格式生成的報表。這是一個基於Swing的組件和其他Java應用程序可以無需將文檔導出爲其他格式,以便查看或打印此集成組件。net.sf.jasperreports.view.JRViewer類表示這個可視組件。這個類也可以被定製爲每個應用程序的需要,通過繼承它。

JasperReports也有用來查看報表的可視化組件Swing應用程序。此應用程序可以幫助在相同的格式查看報表爲*.jrprint就產生了。這個Swing應用程序是在類net.sf.jasperreports.view.JasperViewer實現。要使用此功能,我們可以把這個包成一個Ant目標,以查看報表。

查看生成的報告

下面的示例演示如何查看使用JasperViewer類的報表。

讓我們來寫一個報告模板。在JRXML文件(C: oolsjasperreports-5.0.1 estjasper_report_template.jrxml)的內容如下:

<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"> <![CDATA[]]> <field name="country" class="java.lang.String"> <![CDATA[country]]> <field name="name" class="java.lang.String"> <![CDATA[name]]> <band height="23"> <reportElement mode="Opaque" x="0" y="3" width="535" height="15" backcolor="#70A9A9" /> <bottomPen lineWidth="1.0" lineColor="#CCCCCC" /> <![CDATA[]]> <reportElement x="414" y="3" width="121" height="15" /> <textElement textAlignment="Center" verticalAlignment="Middle"> <font isBold="true" /> <![CDATA[Country]]> <reportElement x="0" y="3" width="136" height="15" /> <textElement textAlignment="Center" verticalAlignment="Middle"> <font isBold="true" /> <![CDATA[Name]]> <band height="16"> <reportElement mode="Opaque" x="0" y="0" width="535" height="14" backcolor="#E5ECF9" /> <bottomPen lineWidth="0.25" lineColor="#CCCCCC" /> <![CDATA[]]> <reportElement x="414" y="0" width="121" height="15" /> <textElement textAlignment="Center" verticalAlignment="Middle"> <font size="9" /> <textFieldExpression class="java.lang.String"> <![CDATA[$F{country}]]> <reportElement x="0" y="0" width="136" height="15" /> <textElement textAlignment="Center" verticalAlignment="Middle" /> <textFieldExpression class="java.lang.String"> <![CDATA[$F{name}]]>

接下來,讓我們通過Java數據對象(Java bean)的集合,到Jasper報表引擎,填補了這一編譯報告。

寫一個POJO DataBean.java表示數據對象(的Java bean)。這個類定義了兩個字符串對象name和country。把它保存到目錄 C: oolsjasperreports-5.0.1 estsrccomyiibai.

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

編寫一個類DataBeanList.java具有業務邏輯生成java bean對象的集合。這是進一步傳遞到Jasper 報表引擎,生成報告。在這裏,我們添加在列表中的4個DataBean進行對象。把它保存到目錄C: oolsjasperreports-5.0.1 estsrccomyiibai.

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

寫一個主類文件JasperReportFill.java,它從類(DataBeanList)得到的java bean的集合,並將其傳遞到Jasper報表引擎,填補了報告模板。把它保存到目錄 C: oolsjasperreports-5.0.1 estsrccomyiibai.

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(); try { JasperFillManager.fillReportToFile( sourceFileName, parameters, beanColDataSource); } catch (JRException e) { e.printStackTrace(); } } }

讓我們來寫一個目標viewFillReport的build.xml文件。 build.xml文件如下所示:

導入文件 - baseBuild.xml環境設置,並應放置在同一目錄中的build.xml。

<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="."> <fileset dir="."> <include name="*.jrxml" /> <classpath refid="classpath" />

接下來,讓我們打開命令行窗口並轉到build.xml文件放置的目錄。最後執行的命令 ant -Dmain-class=com.yiibai.JasperReportFill(viewFillReport是默認的目標)。因此,我們看到一個JasperViewer窗口,如下面的屏幕:

Jasper

打印報表

我們可以使用net.sf.jasperreports.engine.JasperPrintManager類打印的JasperReports類庫生成的文件(在他們的專有格式i.eJasperPrint對象)。這是依賴於Java2 API打印一個假象類。我們還可以打印文檔,一旦JasperReport的文檔導出爲其他格式,如HTML或PDF。

打印生成的報告

下面的代碼演示報表的打印。讓我們更新現有的類JasperReportFill。我們將使用JasperPrintManager.printReport()方法。此方法需要源文件名.jrprint(這裏我們通過我們在上一步生成的使用方法JasperFillManager.fillReportToFile())作爲第一個參數。第二個參數是布爾值,用於顯示標準打印對話框(我們將其設置爲true這裏)。

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.JasperPrintManager; 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"; String printFileName = null; DataBeanList DataBeanList = new DataBeanList(); ArrayList

dataList
=

DataBeanList
.
getDataBeanList
();

JRBeanCollectionDataSource
beanColDataSource
=

new

JRBeanCollectionDataSource
(
dataList
);

Map
parameters
=

new

HashMap
();

try

{
printFileName
=

JasperFillManager
.
fillReportToFile
(
sourceFileName
,
parameters
,
beanColDataSource
);

if
(
printFileName
!=

null
){

JasperPrintManager
.
printReport
(
printFileName
,

true
);

}

}

catch

(
JRException
e
)

{
e
.
printStackTrace
();

}

}

}

現在,讓我們將此文件保存到目錄C: oolsjasperreports-5.0.1 estsrccomyiibai. 我們將使用ANT編譯並執行此文件.build.xml文件的內容如下:

<project name="JasperReportTest" default="executereport" basedir="."> <import file="baseBuild.xml"/> <target name="executereport" depends="compile,compilereportdesing,run"> <echo message="Im here"/> <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="."> <fileset dir="."> <include name="*.jrxml" /> <classpath refid="classpath" />

接下來,讓我們打開命令提示符並轉到build.xml文件放置的目錄。最後,執行命令 ant -Dmain-class=com.yiibai.JasperReportPrint. 因此,會出現一個打印對話框。單擊確定以打印文檔。