BIRT是一个Eclipse-based开放源代码报表系统。它主要是用在基于Java与J2EE的Web应用程序上。BIRT主要由两部分组成:一个是基于Eclipse的报表设计和一个可以加到你应用服务的运行期组件。BIRT同时也提供一个图形报表制作引擎。 BIRT拥有和Dreamweaver一般的操作界面,可以像画table一样画报表,也可以生成图片、导出Excel、html分页,样式比script设置简单,另外BIRT还有OLAP导航功能。
基本概念
- 数据源:数据的来源,或提供者。如xml数据源、jdbc数据源等。
- 数据集:数据集合,它必须与数据源关联,可以理解为查询的结果。
- 报表以及报表项,报表可视为是针对一组数据集的表现形式,而报表项则是这个表现形式的某个具体的单元。它们之间的关系,与窗体和控件的关系非常类似。报表、数据集、数据源三者间的关系:数据源 --- 数据集 --- 报表。
- 报表参数:查询参数的表现形式,使用它可以构建更灵活的报表。
- 模板和库:主要用于复用报表设计,提高报表开发的效率。
到eclipse网站下载可以运行的工程,解压后,把需要的文件及架包从下载的工程中直接拷贝过来就可以了
http://www.eclipse.org/downloads/download.php?file=/birt/downloads/drops/R-R1-2_2_2-200802271210/birt-runtime-2_2_2.zip
注意一定要注意版本问题:
MyEclipse中集成Birt:
我本身用的是MyEclipse6.0,MyEclipse6.0目前自带的是 eclipse3.3.的(MyEclipse 6.0.1 All In One,该安装包集成了JRE5、Eclipse3.3.1和MyEclipse6.0.1GA的自动安装包仅Windows平台),不用再另外下载 eclipse
就是说你的eclipse版本要和适当的birt版本配合,当前birt的最新稳定版是birt2.3。
如果使用的是eclipse3.3版本,你应该用birt-report-framework-2_2_2.zip,
如果使用的是eclipse3.4,你应该用birt-report-framework-2_3_0.zip。
myeclipse6.0自带的是eclipse3.3,所以只能下载birt-report-framework-2_2_2.zip这个版本
(下载地址:http://download.eclipse.org/birt/downloads/index2.2.2.php)
把下载下来的birt-report-framework-2_2_2.zip包解压,把解压缩之后得到的features文件夹下的所有文件Copy到myeclipse6.0安装目录下的..\myeclipse\eclipse\features下,
例如我的是:D:\MyEclipse_6.0\myeclipse\eclipse\features,
把解压缩之后得到的plugins文件下的所有文件Copy到..\myeclipse\eclipse\plugins下,
我的是D:\MyEclipse_6.0\myeclipse\eclipse\plugins,
重新启动myeclipse,OK!。
在实际研究birt的过程中,你可能遇到下面的一些问题:
1.出现java.lang.ClassCastException: org.apache.catalina.util.DefaultAnnotationProcessor的异常。
解决方法:
将tomcat下的conf下的context.xml文件加上:
<Context>
......
<Loader delegate="true"/> <!-- 加上这一句 -->
......
</Context>
2.出现Cannot open the connection for the driver: org.eclipse.birt.report.data.oda.jdbc的异常。
解决方法:
当你开发的报表连接的自己的数据库时。例如Oracle时,需要把Oracle的jdbc驱动包添加到
项目的\WEB-INF\platform\plugins\org.eclipse.birt.report.data.oda.jdbc\drivers目录下.
3.如果你使用的是Tomcat6.0,你需要把commons-logging.jar拷贝到WEB-INF/lib或者Tomcat的lib目录下。
安装Birt 其实非常的简单.只需要下载Birt-Runtime-version 文件就可以了.下载解压后,其中有一个目录是ReportEngine 的目录,所有的Birt 能够运行都需要这个目录下的程序.
另外一个就是WebViewerExample 目录,这是一个eclipse 的sample ,直接把它放到tomcat webapps目录下就可以了.
展示设计好的报表(部署)
WEB容器生成方式:
直接使用birt_runtime_2.1.2中的WebViewerExample
1. 将其复制到%TOMCAT%\webapps目录下,并将其更改名称为birtServer(名称只是个代号,不改也可以);
2. 如需生成PDF格式,在birtServer\WEB-INF\platform\plugins\com.lowagie.itext目录下新建文件夹lib,并将itext-1.3.jar和iTextAsian.jar拷入其中(我下载的文件中已经自动包含了这两个jar包);
3. 把所使用的数据库的JDBC驱动jar包复制到birtServer\WEB-INF\platform\plugins\org.eclipse.birt.report.data.oda.jdbc(后跟版本号)\drivers目录下;
4. 将设计好的报表,比如test.rptdesign放到BirtServer的根目录下,然后启动TOMCAT,即可通过
http://localhost:8080/birtServer/frameset?__report=test.rptdesign (相对路径) (注意report前面是两个下划线)
或者
http://localhost:8080/birtServer/frameset?__report=C:/test.rptdesign (绝对路径)
来访问。
如果要生成pdf文件,如下:
http://localhost:8080/birtServer/frameset?__report=test.rptdesign&__format=pdf
如果报表有输入参数,只需在上述url后拼接参数及值即可,如下:
http://localhost:8080/birtServer/frameset?__report=test.rptdesign&name=whj&date=2007-6-13
(如果没有正常显示,出现下列错误,则是由于缺少common-logging.jar包
环境已配置,输入 http://localhost:8086/Birt/,有信息提示 BirtViewer已经安装成功,点下面的链接去打开birt报表的时候
http://127.0.0.1:8086/birt/frameset?__report=maizi_report.rptdesign ,有错误信息提示:
HTTP Status 404 - Servlet ViewerServlet is not available
解决:少了apache-tomcat-5.5.26\bin\commons-logging-api-1.1.1.jar包。将这个拷到birt/WEB-INF/lib下就可以了。有些web server是不带这个包的,有些是自带的,如 tomcat,websphere等。在初始化servlet的时候,axis是必须要这个jar包的。对于resin = tomcat6.0 。)
5. 如果需要和现有WEB项目集成,可直接把WebViewerExample目录下所有文件及文件夹复制到现有项目的WebRoot下部署运行即可。
birt项目发布过程中需要注意的一些问题:
部署过程:birt官网下载birt-runtime-2_2_2.zip,解压后,将目录下birt.war发布到tomcat下。
1.如果使用的是tomcat6,需要在将commons-logging-api.jar复制到[tomcat_home]/webapps/birt/WEB-INF/lib下。
2.如果需要jdbc连接数据库,需要把相对应的jar包拷贝到[tomcat_home]/webapps/birt/WEB-INF\platform\plugins\org.eclipse.birt.report.data.oda.jdbc_version\drivers目录下
3.将*.rptdesign文件拷贝到[tomcat_home]/webapps/birt/report下,并通过url:http://localhost:8080/birt/frameset?__report=report/*.rptdesign 访问。这里省略了参数。
最近使用BIRT作为自己项目中的报告生成工具,看到论坛上有很多朋友在部署时有问题。希望这篇文章对你有所帮助,基本以BIRT1.0.1的开发者文档翻译为主。2.0刚刚发布,还没有使用过。
这里主要介绍的是部署一个已经存在的BIRT报告,也就是说你已经设计好的.reptdesign文件,本文并不介绍如何设计BIRT报告。
Deploying a BIRT report to an application server
这里BIRT说明了目前版本没有在高于JRE1.4的环境中测试过,如果你需要发布到JRE1.5的环境中,可能会有问题。以下内容使用TOMCAT为例
1.Placing the BIRT report viewer on the application server
拷贝ECLIPSE_INSTALL/plugins/org.eclipse.birt.report.viewer_1.0.1/birt到你的$TOMCAT_INSTALL/webapps,这个BIRT其实就是一个完整web应用,不需要再添加什么JAR包了。你也可以通过在TOMCAT中指定目录<Context />来添加,具体内容可以查看文档。
2.Viewing a report using a browser
添加完birt的应用后,启动你的TOMCAT,然后你就可以通过浏览器访问BIRT。这里有2种方式
http://localhost:8080/birt/run?parameter_list
http://localhost:8080/birt/frameset?parameter_list
2.1 Understanding the run servlet
如果使用run servlet,BIRT会显示一个单独的Web页面或者一个PDF文件
2.2 Understanding the frameset servlet
如果使用frameset servlet,BIRT将会显示3个Frame,一个显示输入报告参数的表单,一个显示报告,一个显示导航条。
2.3 Understanding the URL parameters
下表为servlet可以支持的参数,需要注意的是_frame参数仅对frame servlet有效,reportParam参数是指你自己的report中定义的参数。
Parameter
|
Values
|
Required/default
|
__report
|
path to report design file
|
Yes, no default
|
__format
|
html or pdf
|
Optional /default to html
|
__frame
|
true or false
|
Optional /default to true
|
__locale
|
locale
|
Optional /defaults to JVM locale
|
__navigation
|
hide or parameter
|
Optional /default to auto
|
__toolbar
|
true or false
|
Optional /default to true
|
__isnull
|
any user-defined report parameter values
|
Optional /no default value
|
reportParam
|
User-defined
|
As specified in the report design / As specified in the report design
|
2.3.1 Understanding the __report parameter
__report参数有3种定义方式
a.绝对路径
http://localhost:8080/birt/run?__report=C:\myReport.rptdesign
你的rptdesin文件存放在C:\myReport.rptdesign
b.相对路径,但是你的web.xml中没有定义BIRT_VIEWER_REPORT_ROOT
http://localhost:8080/birt/run?__report=Report\myReport.rptdesign
你的rptdesin文件存放在$TOMCAT_INSTALLl\webapps\birt\Report\myReport.rptdesign
c.相对路径,但是你的web.xml中定义了BIRT_VIEWER_REPORT_ROOT为C:\Reports
http://localhost:8080/birt/run?__report=myReport.rptdesign
你的rptdesin文件存放在C:\Reports\myReport.rptdesign
其他的参数都很简单,就不一一介绍了。
部署的过程其实很简单,我遇到的问题主要有
1.路径不对
2.自定义参数未定义
3.__locale参数未加,显示中文有问题
4.所有参数前面是两个下划线
(3)使用BIRT提供的Report Engine Api来调用报表并展现报表
1. 新建web项目webrpt;
2. 将birt-runtime-2_1_2\ReportEngine\lib下的所有文件拷入WebRoot下的WEB-INF\lib;
3. 在WEB-INF目录中新建文件夹platform,并将birt-runtime-2_1_2\ReportEngine下的plugins和configuration拷入其中;
4. 如需要PDF格式及其亚洲语言支持,需将itext-1.3.jar和iTextAsian.jar拷入platform\plugins\com.lowagie.itext文件夹的lib目录中;
5. 把所使用的数据库的JDBC驱动jar包复制到platform\plugins\org.eclipse.birt.report.data.oda.jdbc(后跟版本号)\drivers目录下;
6. 在webrpt的WebRoot目录下新建Reports和images文件夹。其中Reports文件夹中放置已设计完成的报表文件(*.rptdesign),把设计好的报表文件firstReport.rptdesign复制到WebRoot\Reports下;images将用来放置生成的html中包含的图片。设置完成后目录结构如下图

7. 使用Report Engine API开发调用及展示报表的相关程序。
1) BirtConfig.properties – 配置文件
logDirectory=d:/temp
logLevel=FINEST
2) BirtEngine.java – 用来初始化Report Engine
package com.glnpu.birt;
import java.io.InputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import javax.servlet.ServletContext;
import org.eclipse.birt.report.engine.api.EngineConfig;
import org.eclipse.birt.report.engine.api.IReportEngine;
import org.eclipse.birt.core.framework.PlatformServletContext;
import org.eclipse.birt.core.framework.IPlatformContext;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.report.engine.api.IReportEngineFactory;
public class BirtEngine {
private static IReportEngine birtEngine =null;
private static Properties configProps =new Properties();
private final static String configFile = "com/glnpu/birt/BirtConfig.properties";
public static synchronized void initBirtConfig() {
loadEngineProps();
}
public static synchronized IReportEngine getBirtEngine(ServletContext sc) {
if (birtEngine == null) {
EngineConfig config = new EngineConfig();
if (configProps != null) {
String logLevel = configProps.getProperty("logLevel");
Level level = Level.OFF;
if ("SEVERE".equalsIgnoreCase(logLevel)) {
level = Level.SEVERE;
} else if ("WARNING".equalsIgnoreCase(logLevel)) {
level = Level.WARNING;
} else if ("INFO".equalsIgnoreCase(logLevel)) {
level = Level.INFO;
} else if ("CONFIG".equalsIgnoreCase(logLevel)) {
level = Level.CONFIG;
} else if ("FINE".equalsIgnoreCase(logLevel)) {
level = Level.FINE;
} else if ("FINER".equalsIgnoreCase(logLevel)) {
level = Level.FINER;
} else if ("FINEST".equalsIgnoreCase(logLevel)) {
level = Level.FINEST;
} else if ("OFF".equalsIgnoreCase(logLevel)) {
level = Level.OFF;
}
config.setLogConfig(configProps.getProperty("logDirectory"),level);
}
config.setEngineHome("");
IPlatformContext context = new PlatformServletContext(sc);
config.setPlatformContext(context);
try {
Platform.startup(config);
} catch (BirtException e) {
e.printStackTrace();
}
IReportEngineFactory factory = (IReportEngineFactory) Platform.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
birtEngine = factory.createReportEngine(config);
}
return birtEngine;
}
public static synchronized void destroyBirtEngine() {
if (birtEngine == null) {
return;
}
birtEngine.shutdown();
Platform.shutdown();
birtEngine = null;
}
public Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}
private static void loadEngineProps() {
try {
// Config File must be in classpath
ClassLoader cl = Thread.currentThread().getContextClassLoader();
InputStream in = null;
in = cl.getResourceAsStream(configFile);
configProps.load(in);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3) WebReport.java - Servlet,用来处理参数及生成报表
package com.glnpu.birt;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.birt.report.engine.api.EngineConstants;
import org.eclipse.birt.report.engine.api.HTMLRenderContext;
import org.eclipse.birt.report.engine.api.HTMLRenderOption;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
import org.eclipse.birt.report.engine.api.IReportEngine;
public class WebReport extends HttpServlet {
private static final long serialVersionUID = 1L;
private IReportEngine birtReportEngine = null;
protected static Logger logger = Logger.getLogger("org.eclipse.birt");
public WebReport() {
super();
}
public void destroy() {
super.destroy();
BirtEngine.destroyBirtEngine();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String format=request.getParameter("format");//接收输出格式参数
if(format==null || format.equalsIgnoreCase("html")){
response.setContentType("text/html");//用HTML方式显示
}else{
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline; filename=test.pdf");
// response.setHeader("Content-Disposition", "attachment; filename=test.pdf");//下载
}
String reportName = request.getParameter("ReportName");//接收报表名称
ServletContext sc = request.getSession().getServletContext();
this.birtReportEngine = BirtEngine.getBirtEngine(sc);
// 设置图片路径
HTMLRenderContext renderContext = new HTMLRenderContext();
renderContext.setBaseImageURL(request.getContextPath() + "/images");
renderContext.setImageDirectory(sc.getRealPath("/images"));
logger.log(Level.FINE, "image directory " + sc.getRealPath("/images"));
HashMap contextMap = new HashMap();
contextMap.put(EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT,renderContext);
IReportRunnable design;
try {
// Open report design
design = birtReportEngine.openReportDesign(sc.getRealPath("/Reports")+ "/" + reportName);
// create task to run and render report
IRunAndRenderTask task = birtReportEngine.createRunAndRenderTask(design);
task.setAppContext(contextMap);
// 设置输入参数,要对应报表输入参数的类型
// 如果报表有输入参数,可以按如下组织为Map,然后set
// DateFormat dFormat=new SimpleDateFormat("yyyy-MM-dd");
// HashMap paramMap = new HashMap();
// paramMap.put("OrderDate1", dFormat.parse("1996-07-04"));
// paramMap.put("OrderDate2", dFormat.parse("1996-07-15"));
// task.setParameterValues(paramMap);
// 设置输出选项
HTMLRenderOption options = new HTMLRenderOption();
if(format==null || format.equalsIgnoreCase("html")){
options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_HTML);//HTML
}else{
options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_PDF);//PDF
}
options.setOutputStream(response.getOutputStream());
task.setRenderOption(options);
// run report
task.run();
task.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
public void init() throws ServletException {
BirtEngine.initBirtConfig();
}
}
4) Test.jsp – 用来输入要访问的报表名称,选择浏览格式,提交到WebReport.
<%@ page language="java" pageEncoding="GBK"%>
<html>
<head>
</head>
<body>
<form name="myform" action="./webReport" method="get">
<table>
<tr>
<td>报表名称(带后缀名.rptdesign)</td>
<td><input name="ReportName" type="text"></td>
<td>
<input name="format" type="radio" value="html" checked="checked">HTML
<input name="format" type="radio" value="pdf">PDF
</td>
<td><button type="submit">查看</button></td>
</tr>
</table>
</form>
</body>
</html>
5) 修改web.xml文件如下,配置servlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>WebReport</servlet-name>
<servlet-class>com.glnpu.birt.WebReport</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WebReport</servlet-name>
<url-pattern>/webReport</url-pattern>
</servlet-mapping>
</web-app>
8. 部署并运行webrpt项目,在IE下输入http://localhost:8080/webrpt/test.jsp,在页面输入testreport.rptdesign,选择输出格式后点查看按钮;

运行结果如下图(因为没有样式限制,所以不是很美观):

如何构建一个Birt报表应用程序:
构建一个报表的必须工作:
创建和配置报表引擎
单个报表引擎可以通过创建多个设计器来生成多个报表。通过Platform.createFactoryObject( )方法创建一个ReportEngine对象。设置BIRT目录,指向需要的plug-in和库。
打开报表文档(*.rptdesign 或 *.rptdocument)
通过ReportEngine对象的openReportDesign( )打开一个报表设计模版,参数是文件路径名或InputStream,返回一个IReportRunnable对象。
通过ReportEngine对象的openReportDocument( )打开一个报表文档,参数是文件路径名,返回一个IReportDocument对象。
连接数据源
通过创建数据连接或使用应用程序提供的连接为数据集提供数据源。
报表生成的准备工作
用一个IRenderOption对象设置输出格式,输出文件名,输出流或输出位置,和一些特定格式设置。HTMLRenderOption支持HTML格式的输出,输出PDF格式使用RenderOption
生成报表
使用IRunAndRenderTask对象生成IReportRunnable类型的报表,使用IRenderTask对象创建IReportDocument类型的报表。
也可以访问部署在应用程序服务器上的report viewer servlet来生成报表。
销毁引擎
当我们不再需要使用报表引擎时将它销毁。
可选的任务:
获取参数
如果报表中设置了参数,引擎要使用传入的参数生成报表,如果没有传入参数则使用默认值。
创建报表文档文件(*.rptdocument)
报表文档文件已二进制的形式存储的,如果应用程序使用报表设计文件,要通过IRunTask对象生成一个IReportDocument对象的报表文档。
从报表文档导出数据。
使用IDataExtractionTask对象在报表文档中导出一项或多项的值。
开发环境要求
把需要的类库都引入classpath或开发工具中。配置BIRT目录,目录中包含了从报表设计文件生成报表所需要的plug-in和类库。BIRT的报表引擎包在他的ReportEngine子目录里提供了完整的BIRT home。如果你想动手修改BIRT源码的话,一定要保证源码的版本与BIRT home中的plug-in和类库的版本是一致的。
类库和plug-in
BIRT home的ReportEngine\lib子目录和ReportEngine\plugins中包含了报表应用程序需要的用到的类库和plug-in,你可以根据自己项目的情况删除掉不用的包。
jdbc驱动
BIRT home的plugins/org.eclipse.birt.report.data.oda.jdbc_...文件夹用于存放连接数据库所需要的驱动程序,你可以把需要的驱动程序包放到该目录下(引入classpath中好像是不起作用的…)
通过api修改报表设计模版
报表应用程序在生成报表前可以修改报表设计模版。如:添加删除数据项,更深入的定制报表等。Design Engine API提供了修改设计模版(包括修改脚本)Design Engine的主要接口在org.eclipse.birt.model.api包中。