報表填充過(guò)程JasperPrint對象的輸出可以使用內置的瀏覽器組件來(lái)查看,打印或導出到更多的流行的文件格式,如PDF,HTML,RTF,XLS,ODT,CSV或XML。Jasper文件查看和打印將包括在本章中。導出將包括在下一章導出報表.
JasperReport提供了一個(gè)內置的瀏覽器觀(guān)看原始格式生成的報表。這是一個(gè)基于Swing的組件和其他Java應用程序可以無(wú)需將文檔導出為其他格式,以便查看或打印此集成組件。net.sf.jasperreports.view.JRViewer類(lèi)表示這個(gè)可視組件。這個(gè)類(lèi)也可以被定制為每個(gè)應用程序的需要,通過(guò)繼承它。
JasperReports也有用來(lái)查看報表的可視化組件Swing應用程序。此應用程序可以幫助在相同的格式查看報表為*.jrprint就產(chǎn)生了。這個(gè)Swing應用程序是在類(lèi)net.sf.jasperreports.view.JasperViewer實(shí)現。要使用此功能,我們可以把這個(gè)包成一個(gè)Ant目標,以查看報表。
下面的示例演示如何查看使用JasperViewer類(lèi)的報表。
讓我們來(lái)寫(xiě)一個(gè)報告模板。在JRXML文件(C:\tools\jasperreports-5.0.1\test\jasper_report_template.jrxml)的內容如下:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE jasperReport PUBLIC "http://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/jasperreportshttp://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"> <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> <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>
接下來(lái),讓我們通過(guò)Java數據對象(Java bean)的集合,到Jasper報表引擎,填補了這一編譯報告。
寫(xiě)一個(gè)POJO DataBean.java表示數據對象(的Java bean)。這個(gè)類(lèi)定義了兩個(gè)字符串對象name和country。把它保存到目錄 C:\tools\jasperreports-5.0.1\test\src\com\yiibai.
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; }}
編寫(xiě)一個(gè)類(lèi)DataBeanList.java具有業(yè)務(wù)邏輯生成java bean對象的集合。這是進(jìn)一步傳遞到Jasper 報表引擎,生成報告。在這里,我們添加在列表中的4個(gè)DataBean進(jìn)行對象。把它保存到目錄C:\tools\jasperreports-5.0.1\test\src\com\yiibai.
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; }}
寫(xiě)一個(gè)主類(lèi)文件JasperReportFill.java,它從類(lèi)(DataBeanList)得到的java bean的集合,并將其傳遞到Jasper報表引擎,填補了報告模板。把它保存到目錄 C:\tools\jasperreports-5.0.1\test\src\com\yiibai.
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(); } }}讓我們來(lái)寫(xiě)一個(gè)目標viewFillReport的build.xml文件。 build.xml文件如下所示:
導入文件 - baseBuild.xml環(huán)境設置,并應放置在同一目錄中的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>
接下來(lái),讓我們打開(kāi)命令行窗口并轉到build.xml文件放置的目錄。最后執行的命令 ant -Dmain-class=com.yiibai.JasperReportFill(viewFillReport是默認的目標)。因此,我們看到一個(gè)JasperViewer窗口,如下面的屏幕:

我們可以使用net.sf.jasperreports.engine.JasperPrintManager類(lèi)打印的JasperReports類(lèi)庫生成的文件(在他們的專(zhuān)有格式i.eJasperPrint對象)。這是依賴(lài)于Java2 API打印一個(gè)假象類(lèi)。我們還可以打印文檔,一旦JasperReport的文檔導出為其他格式,如HTML或PDF。
下面的代碼演示報表的打印。讓我們更新現有的類(lèi)JasperReportFill。我們將使用JasperPrintManager.printReport()方法。此方法需要源文件名.jrprint(這里我們通過(guò)我們在上一步生成的使用方法JasperFillManager.fillReportToFile())作為第一個(gè)參數。第二個(gè)參數是布爾值,用于顯示標準打印對話(huà)框(我們將其設置為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(); ArrayListdataList = 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:\tools\jasperreports-5.0.1\test\src\com\yiibai. 我們將使用ANT編譯并執行此文件.build.xml文件的內容如下:
<?xml version="1.0" encoding="UTF-8"?><project name="JasperReportTest" default="executereport" basedir="."> <import file="baseBuild.xml"/> <target name="executereport" depends="compile,compilereportdesing,run"> <echo message="Im here"/> </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>
接下來(lái),讓我們打開(kāi)命令提示符并轉到build.xml文件放置的目錄。最后,執行命令 ant -Dmain-class=com.yiibai.JasperReportPrint. 因此,會(huì )出現一個(gè)打印對話(huà)框。單擊確定以打印文檔。
聯(lián)系客服