利用BIRT ReportEngine API开发报表

 

birt报表部署方式有两种,一种是经过官方提供的WebViewerExample webapp去部署html

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

采用第一种的好处是不须要编写额外的代码,直接就能部署,比较方便,可是限制不少web

若是采用第二种的话就很灵活了。数据库

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

编码前准备:服务器

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

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

准备数据库驱动webapp

编写birt报表文件jsp

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

package report;



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.EngineConstants;

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.HTMLRenderContext;

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;



public class ExecuteReport {



	static void executeReport() throws EngineException {

		HashMap<String, String> parameters = new HashMap<String, String>();



		//报表的参数名称

    String name = "byCondition";

    //报表的参数值,未来能够作个jsp传进来

		String pvalue = "3";

    //封装成map

		parameters.put(name, pvalue);



		IReportEngine engine = null;

		EngineConfig config = null;

		try {



			// 设置Engine而且启动报表平台

			config = new EngineConfig();

			config.setEngineHome("E:/TDDOWNLOAD/birt-runtime-2_6_1/birt-runtime-2_6_1/ReportEngine");

			// 设置报表日志保存的位置和等级( 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("D:/Java/eclipse/workspace/TestReport/WebContent/change_bettiems.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("C:/test/2.1/output.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();

		}

	}

}
相关文章
相关标签/搜索