第十五章 高級特性-開放源碼:BIRT報表CVS構建

15.1 使用CVS構建BIRT報表設計器和引擎

BIRT報表提供了在設計階段開放腳本編輯器,添加javascript和java事件機制來擴展報表的使用,另外還提供了包括數據源,數據項,聚合函數,數據展示,數據導出等等一系列的擴展接口,這對於一般的用戶來實現豐富多彩的報表已經足矣。不過BIRT報表作爲開源社區報表類別使用率最大的報表框架,開放源代碼以及組織良好的源碼和類庫功不可沒。這對於想深度定製報表功能的用戶是個很大的福音。

使用CVS構建BIRT報表設計器和引擎在官網上有一個詳細的介紹,地址如下:

http://www.eclipse.org/birt/phoenix/build/buildInstructions.php

大概的步驟如下:

首先用Eclipse連上BIRTCVS

填寫地址路徑用戶名端口等信息:

Host

dev.eclipse.org

Repository path

/cvsroot/birt

User

anonymous

Connection type

pserver

Use Default Port

Selected

CVS文件夾下 / home / BIRT BIRT模塊。BIRT有超過150個模塊,其中的一些描述如下。

項目文件夾

內容

org.eclipse.birt.chart.device.extension

擴展類的設備渲染架構的圖表

org.eclipse.birt.chart.engine

圖表模型和工廠類

org.eclipse.birt.chart.engine.extension

擴展類的模型渲染架構的圖表

org.eclipse.birt.chart.reportitem

BIRT報表設計器和運行時引擎的圖表模塊的類進行交互

org.eclipse.birt.chart.shared

圖表模塊的不同組件之間的共享的類

org.eclipse.birt.chart.ui

圖表生成器UI

org.eclipse.birt.chart.ui.extension

圖表生成器UI的擴展

org.eclipse.birt.chart.device.extension

圖表引擎擴展插件包含設備的渲染器實現(的SwingSWTPNGGIFJPGBMP

org.eclipse.birt.chart.device.svg

圖表引擎擴展插件的渲染器實現SVG裝置

org.eclipse.birt.converters

包含轉換器,可以轉換成其他格式的報告,的BIRT報表格式的

org.eclipse.birt.core

加載插件在運行時提供了一個框架,幷包含其他類,它們可用於由任何其他模塊

org.eclipse.birt.data

提供的數據引擎執行情況和官方發佈的工廠實現。它也包含類應用的數據轉換。

org.eclipse.birt.data.oda

官方發佈的公共接口:用於創建數據擴展的類和接口。

org.eclipse.birt.data.oda.adapter.dtp

適配器的數據工具項目

org.eclipse.birt.report.data.oda.flatfile

官方發佈的訪問平面文件數據源驅動程序

org.eclipse.birt.report.data.oda.flatfile.ui

官方發佈的平面文件驅動器的用戶界面

org.eclipse.birt.data.oda.jdbc

官方發佈JDBC驅動程序

org.eclipse.birt.data.oda.jdbc.ui

官方發佈JDBC驅動程序的用戶界面

org.eclipse.birt.report.data.oda.sampledb

官方開發的示例數據庫驅動程序

org.eclipse.birt.report.data.oda.sampledb.ui

官方開發的示例數據庫驅動程序的用戶界面

org.eclipse.birt.data.oda.xml

ODA驅動程序來訪問XML數據源

org.eclipse.birt.data.oda.xml.ui

ODA驅動程序來訪問XML數據源的用戶界面

org.eclipse.birt.report.debug.ui

用於創建啓動配置調試包含Java事件的報告時

org.eclipse.birt.report.designer.core

核心類的BIRT報表設計器,包括的BIRT模型的類互動,本地化和其他實用工具類

org.eclipse.birt.report.designer.lib

用於庫操作的類

org.eclipse.birt.report.designer.lib.explorer

處理庫內容的瀏覽和探索

org.eclipse.birt.report.designer.ui

BIRT報表設計器

org.eclipse.birt.report.designer.ui.rcp

BIRT RCP報告設計器

org.eclipse.birt.report.engine

BIRT報表引擎,生成報表的報表設計

org.eclipse.birt.report.engine.emitter.fo

發射器,用於生成報表輸出FO

org.eclipse.birt.report.engine.emitter.html

HTML生成報表輸出的發射器,

org.eclipse.birt.report.engine.emitter.pdf

發射器,生成報表輸出PDF

org.eclipse.birt.report.model

BIRT報表模型

org.eclipse.birt.report.viewer

BIRT報表查看器(BIRT Web應用程序)

org.eclipse.birt-feature 

BIRT功能

導入iText1.3和 iTextAsiancom.lowagie.itext / lib目錄中。  

從源碼中複製三個插件到當前項目根目錄下:

org.apache.batik.pdf_1.6.0.v20060623-1511
org.apache.batik_1.6.0.v20060623-1511
org.apache.xerces_2.8.0.v20060815-0630
重命名如下:
org.apache.batik.pdf
org.apache.batik
org.apache.xerces 

用ant編譯:

這樣,在源碼中改變視圖viewer語言,漢化視圖的方法如下:

Source裏有一個nl的文件夾 裏面有很多國際化文件

打開org.eclipse.birt.report.viewer.nl這個包的src/org/eclipse/birt/report/resource下邊可以看到幾個msg文件

接着Check out Message_zh_CN.msg這個msg文件

直接打開會是亂碼的 需要通過 native2ascii -encoding gbk BirtResources_zh_CN.msg BirtResources_zh_CN.properties 來編碼(也可以直接使用UE等高級文本工具打開後直接複製出來)。

最後複製這個編碼後的文件壓到 lib/viewServlets.jar這個包的

/org/eclipse/birt/report/resource目錄下

注意目錄和包路徑不要放錯了,我幾次都放錯位置而且把文件名也弄錯了。

接着就可以通過__locale=zh-CN的屬性來訪問中文的BIRT view

 

擴展報表其它格式文件的導出和顯示也可以直接從源碼入手,例如增加一個csv格式的render:

  1. import org.eclipse.birt.report.engine.api.IRenderOption;  
  2. import org.eclipse.birt.report.engine.api.RenderOption;  
  3. public class CSVRenderOption extends RenderOption   
  4. {  
  5. public static final String OUTPUT_FORMAT_CSV = "xls";  
  6. public CSVRenderOption()   
  7. {  
  8. super();  
  9. }  
  10. public CSVRenderOption(IRenderOption options)   
  11. {  
  12. super(options);  
  13. }  
  14. }  

在報表引擎那裏這樣調用的:

  1. // PDFIRenderOption pdfOptions;  
  2. pdfOptions = new PDFRenderOption();  
  3. pdfOptions.setOutputFormat(PDFRenderOption.OUTPUT_FORMAT_PDF);  
  4. pdfOptions.setOutputFileName("D:/birt/test.pdf");  
  5. task.setRenderOption(pdfOptions);  
  6. task.run();  
  7. // HTMLIRenderOption htmlOptions;  
  8. htmlOptions = new HTMLRenderOption();  
  9. ByteArrayOutputStream bos = new ByteArrayOutputStream();  
  10. htmlOptions.setOutputStream(bos);  
  11. htmlOptions.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_HTML);  
  12. task.setRenderOption(htmlOptions);  
  13. task.run();  
  14. browser.setText(bos.toString());  
  15. // CSVIRenderOption csvOptions;  
  16. csvOptions = new CSVRenderOption();  
  17. csvOptions.setOutputFileName("D:/birt/test.xls");  
  18. csvOptions.setOutputFormat(CSVRenderOption.OUTPUT_FORMAT_CSV);  
  19. task.setRenderOption(csvOptions);  
  20. task.run();  
  21. // destroy the tasktask.close();  
  22. engine.destroy();  

這樣會分別導出爲Excel文件,pdf文件,同時用HTML的形式顯示在browser上。
同時在plugin.xmlsillycatGen.product上面都要添加上這個包:
org.eclipse.birt.report.engine.emitter.prototype.excel

改變導出文件編碼的方式如下,以導出CSV數據爲例

找到輸出csv數據的代碼找到org.eclipse.birt.report.viewer/birt/WEB-INF/classes/org/eclipse/birt/report/service/ReportEngineService.java. 找到extractData這個函數,這個函數是負責輸出數據到csv文件的.這個函數裏面有兩個outputStream.write( buf.toString( ).getBytes( ParameterAccessor.UTF_8_ENCODE ) );把這兩行語句改爲outputStream.write( buf.toString( ).getBytes( "GBK" ) );

把修改了的文件進行編譯,把這個class文件替換viewservlets.jar包中的ReportEngineService文件.最後在把這個包放到lib.這樣問題得到解決.

當然,通過修改源代碼還能進行更多複雜的功能,比如增加報表預覽時的權限審覈,增加數據源,數據集,數據項等等。

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