利用BIRT ReportEngine API开发表格

利用BIRT ReportEngine API开发报表
http://my.oschina.net/kzhou/blog/9172
birt报表部署方式有两种,一种是通过官方提供的WebViewerExample webapp去部署

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

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

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

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

编码前准备:

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

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

准备数据库驱动

编写birt报表文件

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

package com;

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;

@SuppressWarnings("deprecation")
public class ExecuteReport {

	@SuppressWarnings("unchecked")
	static void executeReport() 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("d:/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 + "sample.rptdesign");

		// 创建报表任务
		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\\a.html");

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

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

	/**
	 * 
	 * @param args
	 */

	public static void main(String[] args) {

		try {
			executeReport();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}