利用BIRT ReportEngine API开发报表

[url]http://my.oschina.net/kzhou/blog/9172[/url]
birt报表部署方式有两种,一种是通过官方提供的WebViewerExample webapp去部署

另一种是通过ReportEngine API自行开发部署程序;

采用第一种的好处是不需要编写额外的代码,直接就能部署,比较方便,但是限制很多

如果采用第二种的话就很灵活了。

本日志主要记录采用ReportEngine API来进行报表的部署;

编码前准备:

下载birt-runtime-version.zip(www.eclipse.org有下载)

解压,其中ReportEngine目录存放着所有所需的东西

准备数据库驱动

编写birt报表文件

利用下边的代码就可以执行报表文件并生成目标html文件

package com;

import java.io.IOException;
import java.util.HashMap;
import java.util.logging.Level;

import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.engine.api.EngineConfig;
import org.eclipse.birt.report.engine.api.EngineException;
import org.eclipse.birt.report.engine.api.HTMLActionHandler;
import org.eclipse.birt.report.engine.api.HTMLEmitterConfig;
import org.eclipse.birt.report.engine.api.HTMLRenderOption;
import org.eclipse.birt.report.engine.api.HTMLServerImageHandler;
import org.eclipse.birt.report.engine.api.IReportEngine;
import org.eclipse.birt.report.engine.api.IReportEngineFactory;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
import org.eclipse.birt.report.model.api.CellHandle;
import org.eclipse.birt.report.model.api.DesignConfig;
import org.eclipse.birt.report.model.api.DesignElementHandle;
import org.eclipse.birt.report.model.api.ElementFactory;
import org.eclipse.birt.report.model.api.GridHandle;
import org.eclipse.birt.report.model.api.IDesignEngine;
import org.eclipse.birt.report.model.api.IDesignEngineFactory;
import org.eclipse.birt.report.model.api.ImageHandle;
import org.eclipse.birt.report.model.api.LabelHandle;
import org.eclipse.birt.report.model.api.ReportDesignHandle;
import org.eclipse.birt.report.model.api.RowHandle;
import org.eclipse.birt.report.model.api.SessionHandle;
import org.eclipse.birt.report.model.api.activity.SemanticException;

import com.ibm.icu.util.ULocale;

/**
* Simple BIRT Design Engine API (DEAPI) demo.
* 不保存到文件,就直接解析和生成
*/
@SuppressWarnings("deprecation")
public class BIRT {
public static IReportEngine birtReportEngine = null;

public static void makeDesign(ReportDesignHandle design, ElementFactory efactory) throws Exception {
DesignElementHandle element = efactory.newSimpleMasterPage("Page Master");
design.getMasterPages().add(element);

GridHandle grid = efactory.newGridItem(null, 2, 1);
design.getBody().add(grid);
grid.setWidth("100%");

RowHandle row = (RowHandle) grid.getRows().get(0);
ImageHandle image = efactory.newImage(null);
image.setURL("\"1.jpg\"");

CellHandle cell = (CellHandle) row.getCells().get(0);
cell.getContent().add(image);

LabelHandle label = efactory.newLabel(null);
label.setText("Hello, world!");
cell = (CellHandle) row.getCells().get(1);
cell.getContent().add(label);
}

static ReportDesignHandle buildReport() throws IOException, SemanticException {
DesignConfig config = new DesignConfig();
config.setProperty("BIRT_HOME", Utils.BIRT_HOME);
IDesignEngine engine = null;
try {
Platform.startup(config);
IDesignEngineFactory factory = (IDesignEngineFactory) Platform.createFactoryObject(IDesignEngineFactory.EXTENSION_DESIGN_ENGINE_FACTORY);
engine = factory.createDesignEngine(config);
} catch (Exception ex) {
ex.printStackTrace();
}

SessionHandle session = engine.newSessionHandle(ULocale.ENGLISH);
ReportDesignHandle design = session.createDesign();
ElementFactory efactory = design.getElementFactory();

try {
makeDesign(design, efactory);
} catch (Exception e) {
e.printStackTrace();
return null;
}

return design;
}

@SuppressWarnings({ "unchecked" })
public static void executeReport(ReportDesignHandle reportDesignHaddle) throws EngineException {

HashMap<String, String> parameters = new HashMap<String, String>();
String name = "byCondition";
String pvalue = "3";
parameters.put(name, pvalue);

IReportEngine engine = null;
EngineConfig config = null;
try {

// 设置Engine并且启动报表平台
config = new EngineConfig();
config.setEngineHome(Utils.BIRT_HOME);

// 设置报表日志保存的位置和等级( null, Level ) 如果你不需要日志可以设置为null
config.setLogConfig("c:/birt/logs", Level.FINE);

// 平台初始化,启用
Platform.startup(config);
IReportEngineFactory factory = (IReportEngineFactory) Platform.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
engine = factory.createReportEngine(config);
engine.changeLogLevel(Level.WARNING);

} catch (Exception ex) {
ex.printStackTrace();

}

// 设置发起者的一些操作,比如显示图片,报表生成到html页面,很关键的部分

HTMLEmitterConfig emitterConfig = new HTMLEmitterConfig();
emitterConfig.setActionHandler(new HTMLActionHandler());

HTMLServerImageHandler imageHandler = new HTMLServerImageHandler();
emitterConfig.setImageHandler(imageHandler);

config.getEmitterConfigs().put("html", emitterConfig); //$NON-NLS-1$

IReportRunnable design = null;
// 打开设计好的报表,取绝对路径,最好使用context.getRealPath();这种方法实现,官方这个比较呆
// design = engine.openReportDesign(Utils.SAVE_HOME + fileName + ".rptdesign");
//不保存到文件,就直接解析和生成
design = engine.openReportDesign(reportDesignHaddle);

// 创建报表任务
IRunAndRenderTask task = engine.createRunAndRenderTask(design);

// // 设置报表的路径和图片显示的路径
// HTMLRenderContext renderContext = new HTMLRenderContext();
// // 为所有的actions设置Base URL,这个不写就是默认服务器URL的
// renderContext.setBaseURL("http://localhost/");
// // 设置所有图片显示的URL - 如果之前没有emitterConfig.setImageHandler( imageHandler
// // );的话会造成显示的URL是本地的绝对路径,其实http://localhost不写也是可以的,会自动添加服务器的URL
// renderContext.setBaseImageURL("http://localhost/myimages");
// // 设置所有图片存放的位置,最好使用context.getRealPath();
// renderContext.setImageDirectory("C:/xampplite/htdocs/myimages");
// // 设置图片支持的格式,据官方说必须有SVG,我没写也没出错
// renderContext.setSupportedImageFormats("JPG;PNG;BMP;SVG");
// HashMap<String, HTMLRenderContext> contextMap = new HashMap<String, HTMLRenderContext>();
// contextMap.put(EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT,
// renderContext);
// task.setAppContext(contextMap);

// 设置参数
task.setParameterValues(parameters);

// 要所有的参数一条一条的写入,例如: task.setParameterValue("Top Count", new
// Integer(12));
task.validateParameters();

// 增加scrpit参考下面的例子
// pFilter.myjavamethod()
// ProcessFilter pf = new ProcessFilter();
// task.addScriptableJavaObject("pFilter", pf);
// 设置rendering操作- 例如file or stream output, output format, whether it is
// embeddable, etc
HTMLRenderOption options = new HTMLRenderOption();

// 例如:Remove HTML and Body tags
// options.setEmbeddable(true);
// 设置输出本地文件
options.setOutputFileName("D:\\dev-workspace\\workspace-v4.0\\BIRT\\temp\\test.html");

// 设置输出文件格式
options.setOutputFormat("html");
task.setRenderOption(options);

// 运行report任务,然后关闭
// 如果要长期留驻的话可以不关闭,我建议不关闭engine和Platform,要不每次打开报表都要等好久……
task.run();
task.close();
engine.shutdown();
Platform.shutdown();
}

/**
* 不保存到文件,就直接解析和生成
* @param args
*/
public static void main(String[] args) {
birtReportEngine = BirtEngine.getBirtEngine();
try {
ReportDesignHandle reportDesignHaddle = buildReport();
System.out.println("产生完成");
executeReport(reportDesignHaddle);
System.out.println("导出完成");
reportDesignHaddle.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值