birt 高级
第十五章 高级特性-开放源码:BIRT报表CVS构建
15.1 使用CVS构建BIRT报表设计器和引擎
BIRT报表提供了在设计阶段开放脚本编辑器,添加javascript和java事件机制来扩展报表的使用,另外还提供了包括数据源,数据项,聚合函数,数据展示,数据导出等等一系列的扩展接口,这对于一般的用户来实现丰富多彩的报表已经足矣。不过BIRT报表作为开源社区报表类别使用率最大的报表框架,开放源代码以及组织良好的源码和类库功不可没。这对于想深度定制报表功能的用户是个很大的福音。
使用CVS构建BIRT报表设计器和引擎在官网上有一个详细的介绍,地址如下:
http://www.eclipse.org/birt/phoenix/build/buildInstructions.php
大概的步骤如下:
首先用Eclipse连上BIRT的CVS
填写地址路径用户名端口等信息:
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 |
图表引擎扩展插件包含设备的渲染器实现(的Swing,SWT,PNG,GIF,JPG,BMP) |
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功能 |
导入iText的1.3和 iTextAsian到com.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:
import org.eclipse.birt.report.engine.api.IRenderOption; import org.eclipse.birt.report.engine.api.RenderOption; public class CSVRenderOption extends RenderOption { public static final String OUTPUT_FORMAT_CSV = "xls"; public CSVRenderOption() { super(); } public CSVRenderOption(IRenderOption options) { super(options); } }
在报表引擎那里这样调用的:
// PDFIRenderOption pdfOptions; pdfOptions = new PDFRenderOption(); pdfOptions.setOutputFormat(PDFRenderOption.OUTPUT_FORMAT_PDF); pdfOptions.setOutputFileName("D:/birt/test.pdf"); task.setRenderOption(pdfOptions); task.run(); // HTMLIRenderOption htmlOptions; htmlOptions = new HTMLRenderOption(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); htmlOptions.setOutputStream(bos); htmlOptions.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_HTML); task.setRenderOption(htmlOptions); task.run(); browser.setText(bos.toString()); // CSVIRenderOption csvOptions; csvOptions = new CSVRenderOption(); csvOptions.setOutputFileName("D:/birt/test.xls"); csvOptions.setOutputFormat(CSVRenderOption.OUTPUT_FORMAT_CSV); task.setRenderOption(csvOptions); task.run(); // destroy the tasktask.close(); engine.destroy();
这样会分别导出为Excel文件,pdf文件,同时用HTML的形式显示在browser上。
同时在plugin.xml和sillycatGen.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下.这样问题得到解决.
当然,通过修改源代码还能进行更多复杂的功能,比如增加报表预览时的权限审核,增加数据源,数据集,数据项等等。