jasperreport和ireports學習文檔

Jasper筆記

一.學會ireports

1.      目前ireport的版本是4.6.0版本在生成的ireport的xml文件中有一個特定的屬性,uuid這個編號。而在eclipse中不能識別這個屬性。當移植在eclipse中的時候刪掉這個uuid的時候才能行。

2.      頁面佈局方面,如果要想讓column這一列進行顯示在上面,我們要在page頁面屬性float column foder進行選中,然後就可以進行設置各個類型(title,page header。。。)的高度的設置,進行格式上得調整。

3.      創建數據連接,在打開的頁面上工具圖標上有一個圖標,進行點擊創建數據(fields),但是創建的數據源可以有幾種,一種是直接連接數據庫,但是首先要穿件數據源;二種是javabean的這種數據,但是首先要創建一個javapath路徑,但是這個路徑必須在build路徑下得class包中。

4.      講解一下工具工具欄的一個菜單的用法,preview這個工具欄,這個工具欄就是各種顯示界面的類型。

5.      創建一個新的jrxml文件,在打開頁面的文件—new—reports這樣建立一個新的jrxml文件。

6.      如果要在新的jrxml文件上創建圖形分析圖的話,打開窗口菜單欄的下的組件面板,直接選擇上面的控件往你要進行數據顯示的區域。

7.      各個區域的介紹:tite 是表示該報表的頭部信息,但是這個信息只有第一頁顯示,其他頁面不在顯示。Page header 表示文章的作者信息或其其他說明的,也是第一頁有效,detail這個就是數據進行循環顯示的內容。表示這個數據有多少個對象就要顯示幾次。(一般進行最後分析的結果建議不要放在這個選擇框中),column footer這個框表示只顯示一列。

8.      給各種圖像進行填充數據,在該圖上單擊右鍵(char set),進行填充數據。

(主要的具體操作還是要查看官方文檔和其他學習文檔)

 

二.學會在eclipse上安裝插件插件的的路徑:htt://jasperwave/update-site/

基本操作和irePORTS的操作差不多,要細細去研究。

 

三.學會看懂jrxml的文件

具體看是jrxml的頭文件,對於每一組件都要進行識別

 

四.結合eclipse進行打印輸出圖形

方式一:(通過struts進行配置)

情況一:已經生成.jasper文件

         <actionname=”” class=”” method=””>

                   <resultname=”success” type=”jasper”>

                            <param name=”location”>

                                     /**/***.jasper//表示jasper的保存文件夾

</param>

<param name=”dataSource”>

         ***//表示一個list列表的集合

<param>

<param name=”format”>

PDF//表示格式(HTML,XML,RTF….)

</param>

                   </result>

         </action>

 

情況二:只有jrxml文件

首先:在action中進行處理jrxml文件轉換成jasper文件

Tage = userdao.t…..;

String reportSoure =ServletActionContext.getServletContext().getRealPath(“/**/**.jrxml”);

         Fileparent = new File(reportSoure).getParentFile();

         JasperCompileManager.compileReportToFile(reportSoure,newFile(parent,”**.jasper”).geAbsolutePath());

         其次:就是進行進行上面情況一的步驟

 

方式二:(通過通用的一個格式轉換類來進行轉換)

在action中寫上

tage = userdao.TService();
System.out.println("-----------=============--------------"
+ tage.size());
JRBeanCollectionDataSource dataSouce = new JRBeanCollectionDataSource(
tage);
System.out.println(LoginAction.class.getResource("/").toString()
.substring(6));

JasperReport jasperReport = (JasperReport) JasperCompileManager
.compileReport(LoginAction.class.getResource("/")
.toString().substring(6)
+ "Test.jrxml");

Map parameters = new HashMap();

JasperPrint jasperPrint = JasperFillManager.fillReport(
jasperReport, parameters, dataSouce);

ExportReport.exportToHTML1(jasperPrint);

 

另外創建一個處理類 :ExportReport.java

package com.shop.util;

 

import javax.servlet.http.HttpServletRequest;

 

import org.apache.struts2.ServletActionContext;

 

import net.sf.jasperreports.engine.JRException;

import net.sf.jasperreports.engine.JRExporterParameter;

import net.sf.jasperreports.engine.JasperPrint;

import net.sf.jasperreports.engine.export.JRHtmlExporter;

importnet.sf.jasperreports.engine.export.JRHtmlExporterParameter;

import net.sf.jasperreports.engine.export.JRPdfExporter;

import net.sf.jasperreports.engine.export.JRPdfExporterParameter;

import net.sf.jasperreports.engine.export.JRRtfExporter;

import net.sf.jasperreports.engine.export.JRXlsExporter;

importnet.sf.jasperreports.engine.export.JRXlsExporterParameter;

import net.sf.jasperreports.engine.export.JRXmlExporter;

 

public class ExportReport {

    /**

     * 導出html

     * @param jasperPrint

     * @return

     */

    public staticString exportToHTML(JasperPrint jasperPrint){

       HttpServletRequestrequest = ServletActionContext.getRequest();

       StringBuffersb = new StringBuffer();

       try {

           JRHtmlExporterexporter = new JRHtmlExporter();

           exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);

           exporter.setParameter(JRExporterParameter.OUTPUT_STRING_BUFFER,sb);

       exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI,request.getContextPath()+"/WebContent/images?image=");

           //報表邊框圖片設置IS_USING_IMAGES_TO_ALIGN,Boolean.FALSE,不使用圖片

          exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,Boolean.FALSE);

           exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING,"UTF-8");

      

           exporter.exportReport();

       } catch(JRException e) {

           // TODOAuto-generated catch block

           e.printStackTrace();

       }

       returnsb.toString();

    }

   

    /**

     * 導出html

     * @param jasperPrint

     * @return

     */

    public staticvoid exportToPDF(JasperPrint jasperPrint){

       try {

           JRPdfExporterpdfexporter = new JRPdfExporter();

           pdfexporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);

            //如果保存到硬盤用下面語句

            pdfexporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,"D:/mypdf.pdf");

           pdfexporter.setParameter(JRPdfExporterParameter.IS_ENCRYPTED,Boolean.FALSE);

           pdfexporter.setParameter(JRExporterParameter.CHARACTER_ENCODING,"UTF-8");

           pdfexporter.exportReport();

       } catch (JRExceptione) {

           // TODOAuto-generated catch block

           e.printStackTrace();

       }

    }

    /**

     * 導出RTF

     * @param jasperPrint

     * @return

     */

    public staticvoid exportToRTF(JasperPrint jasperPrint){

       try {

           JRRtfExporterrtfExporter=new JRRtfExporter();

           rtfExporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);

           //設置導出文件名

           rtfExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,"D:/myrtf.rtf");

           rtfExporter.setParameter(JRExporterParameter.CHARACTER_ENCODING,"UTF-8");

           rtfExporter.exportReport();

       } catch(JRException e) {

           // TODOAuto-generated catch block

           e.printStackTrace();

       }

    }

    /**

     * 導出xml

     * @param jasperPrint

     * @return

     */

    public staticvoid exportToXML(JasperPrint jasperPrint){

       try {

           JRXmlExporterxmlExporter=new JRXmlExporter();

           xmlExporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);

           xmlExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,"D:/myxml.xml");

           xmlExporter.setParameter(JRExporterParameter.CHARACTER_ENCODING,"UTF-8");

           xmlExporter.exportReport();

 

       } catch(JRException e) {

           // TODOAuto-generated catch block

           e.printStackTrace();

       }

    }

    /**

     * 導出Excel

     * @param jasperPrint

     * @return

     */

    public staticvoid exportToExcel(JasperPrint jasperPrint){

       try {

           JRXlsExporterjrXlsExporter=new JRXlsExporter();

           jrXlsExporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);

           //設置輸出流

           jrXlsExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,"D:/myexcel.xls");

           jrXlsExporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,Boolean.FALSE);

           jrXlsExporter.setParameter(JRExporterParameter.CHARACTER_ENCODING,"UTF-8");

           jrXlsExporter.exportReport();

       } catch(JRException e) {

           // TODOAuto-generated catch block

           e.printStackTrace();

       }

    }

   

    /**

     * 輸出指定的html的文件

     * @param jasperPrint

     */

    public staticvoid exportToHTML1(JasperPrint jasperPrint){

       HttpServletRequestrequest = ServletActionContext.getRequest();

       Stringa=request.getRealPath("/");

       System.out.println(a);

//     StringBuffersb = new StringBuffer();

       try {

           JRHtmlExporterexporter = new JRHtmlExporter();

           exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);

           exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,a+"show/my.html");

           //exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI,"./images?image=");

           //報表邊框圖片設置IS_USING_IMAGES_TO_ALIGN,Boolean.FALSE,不使用圖片

          exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,Boolean.FALSE);

          exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING,"UTF-8");

      

           exporter.exportReport();

       } catch(JRException e) {

           // TODOAuto-generated catch block

           e.printStackTrace();

       }

      

    }

 

}

 

 

 

 

 

 

 

 

 

 

 

寫:小林

郵箱:[email protected]

請各位讀者諒解,剛剛初學,有些比較含糊,具體內容需要參考官方API


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章