您的位置:首页 > 编程语言 > Java开发

开始在CSDN写博客

2008-07-16 15:16 246 查看
天天在CSDN上分享一些“朋友”的技术文档,所以通过这次做了一个开源的birt报表的实例,自己也想给大家分享一下自己学习的心得。

1.学习目的:

由于公司需要做一个项目,项目是给一个上海的客户做的,其中项目中要做一个报表,客户可能是为了省钱,所以用了一个开源的的报表,也就是birt,自己只好开始学习了(网上对这个的资料比较上,而且都是一些乱人乱写,老有错的,费了我好大的功夫啊),痛苦的学习了一个礼拜终于把他搞定了一点,但是还有好多要学习的,比如在java中给她传参数,还没有做呢!!!

2.学习环境

公司,比较安静(呵呵,正好做这个项目要回公司,以前在客户那里做,吵死,锻炼的自己比较喜欢那样了,指出我们公司是做外包的)。

3.硬、软件环境

电脑,最好是好一点配置的,内存1G,软件:eclipse,myeclipse,(两个的原因是一个设计报表,一个写web,可能我还没有找到把他们整合在一块吧,以后慢慢在改),数据库,

4.birt简介

BIRT是一个Eclipse-based开放源代码报表系统。它主要是用在基于Java与J2EE的Web应用程序上。BIRT主要由两部分组成:一个是基于Eclipse的报表设计和一个可以加到你应用服务的运行期组件。BIRT同时也提供一个图形报表制作引擎。 BIRT拥有和Dreamweaver一般的操作界面,可以像画table一样画报表,也可以生成图片、导出Excel、html分页,样式比script设置简单,另外BIRT还有OLAP导航功能。
5.基本概念

数据源:数据的来源,或提供者。如xml数据源、jdbc数据源等。

数据集:数据集合,它必须与数据源关联,可以理解为查询的结果。

报表以及报表项,报表可视为是针对一组数据集的表现形式,而报表项这是这个表现形式的某个具体的单元。它们之间的关系,与窗体和控件的关系非常类似。报表、数据集、数据源三者间的关系:数据源 --- 数据集 --- 报表。

报表参数:查询参数的表现形式,使用它可以构建更灵活的报表。

模板和库:主要用于复用报表设计,提高报表开发的效率。

6、BIRT报表设计器的安装与配置

BIRT的下载地址是:http://download.eclipse.org/birt/downloads/ 有两种下载方式:
1) All-in-One 版本(birt-report-designer-all-in-one-2.1.2.zip),它包含了运行BIRT系统所需的组件,可直接应用。
2) Framework插件版本(birt-report-framework-2.1.2.zip),安装的必要环境为Java 1.4.2/1.5 JDK/JRE; Eclipse SDK 3.2, GEF 3.2 and EMF 3.2。
安装步骤如下:打开eclipse文件夹,在文件夹中再新建一文件夹(如:birt),然后打开birt-report-framework-2.1.2.zip把其中的eclipse文件夹放入新建的birt文件夹中。然后在同文件夹的links文件夹中,新建一个 .link文件(如:birt.link),且修改其(birt.link)里的路径(如:path=d: //eclipse//birt)即已完成。
7、使用BIRT报表设计器设计报表

4.2.1 设计一个简单的报表

(1)创建一个报表项目

选择“文件-à新建-à项目”,出现一新项目对话框。打开"业务智能和报表工具”(Business Intelligence and Reporting Tools)组,并选择"报表项目" 。

点击下一步(next),输入 My firstBirt 作为项目名字

点击“完成”。
点击 “是” ,提示你切换到报表设计视图。

(2)创建一个报表

选择 文件->新建->报表 。 新建报表(new report )对话框出现。
在Tree View中选择我们之前创建的项目:My firstBirt
接下来在文件名字段输入: first.rptdesign
点击 下一步。
你可以在弹出的对话框中,任选一种报表模式,我这里以空白报表为例。

点击 完成。

(3)建立数据源

打开数据资源管理器。
选择数据源节点。
用鼠标右键单击打开弹出菜单。
选择新建数据源。 新建数据源对话出现。
数据源类型的创建:
1[/i])、选择 Classic Models Inc. 示例数据库 [/i]
在数据源名称输入框, 输入"example" 。
点击 下一步。
点击 完成。
作为将来的参考, 我们回顾一下您将如何改变数据源:
打开数据资源管理器
打开数据源节点。
双击数据源。
数据源编辑器被打开。 您能做所有有需要的更改。
点击 确定 保存您的变动。
2[/i])、选择JDBC数据库[/i]
在数据源名称输入框, 输入"sample" 。
点击 下一步。

在管理驱动程序中添加JAR包,然后测试连接,成功后,确定即可。
注意:我用的是SQL SERVER数据库,在管理驱动程序中要导入3个sql serverJAR包(msbase.jar,mssqlserver.jar,msutil.jar),大家也可以用其他数据库,只不过要注意JAR包和数据库URL就可以了。

(4)建立数据集

打开数据资源管理器
选择数据集节点。
用鼠标右键单击打开弹出菜单。
选择新建数据集。 新建数据集对话将出现。
“在数据集名称”输入框,输入"examples"。
确保数据源下拉框显示的是我们之前创建的 example 数据源。
确保数据集类型下拉框选中的是"SQL 查询语句" 。

点击下一步,进入“查询”对话框。

这是创造和改变 SQL query 的地方。 一旦您有基本的query, 您能增加WHERE 子句, 调整所选择的字段集,等等。 使用 SQL 编辑器增加计算字段,由数据库引擎负责计算。 使用SQL 别名子句给这样的字段命一个名字。我写了一个SQL的查询语句:select * from CLASSICMODELS.PRODUCTS
点击 完成,弹出一编辑数据集对话框,在“输出列”单元编辑其显示名称

在“预览结果”单元,可查看要显示的全部信息。
点击 确定即可。

(5)建立表项

把鼠标移到palette,即可弹出一浮动框,选中“表”项,

用鼠标拖动“表”项,移到编辑区,即可弹出“插入表”对话框,

修改其属性后,(注意“数据集”要选上已有的数据集)点击确定

此表包含了页眉(表头),明细数据行(数据),页脚行。
拖动左边数据集“examples”下的列到表的相应明细数据行

拖完后点击“预览”,在编辑区就可以看到报表数据

这样一个简单的报表就做好了,(设计器的操作很多,在这里就不一一作介绍了)。

4.2.2 给报表添加统计图表

1. 添加新数据集chart,用于统计图表;

2. 从"Palette"面板拖入"图表"项到报表中,此时会弹出"新建图表"窗口,选择图表类型;

3. 在"选择数据"页,使用数据集chart,同时选中username列,将其拖入"类别(x)系列"。对于统计列,同样将其拖入"类别y系列"。这里还可以做数据筛选等操作;

4. 在"图表格式"页,分别为x和y系列,填写相应的显示名称。还可以做一些显示细节的调整;

8.使用BIRT提供的Report Engine Api来调用报表并展现报表(重点,遇到好多问题)

1. 新建web项目webrpt;
2. 将birt-runtime-2_1_2/ReportEngine/lib下的所有文件拷入WebRoot下的WEB-INF/lib;(下载地址:http://www.eclipse.org/downloads/download.php?file=/birt/downloads/drops/R-R1-2_3_0-200806191030/birt-runtime-2_3_0.zip
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中包含的图片。
1. 使用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,用来处理参数及生成报表
[/b]
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();
}
}
[/b]
4) Test.jsp –[/b] 用来输入要访问的报表名称,选择浏览格式,提交到WebReport.
[/b]
<%@ 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>

(按照我的一部一部做,应该是没有问题的,版本一定要对哦,要不会报错的,还有那些包也要拷到相应的位置,这样就不会报出错了!)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息