abeetle 发表于 2013-2-7 16:01:19

在plugin中用engine api引入BIRT已经设计好的rptdesign文件

<div class="postText">这个方法直接放到rcp下运行不了,几天过去才发现是个多么愚蠢的错误:在plugin.xml的dependiences添加org.eclipse.birt.report.viewer, org.eclipse.birt.core, org.eclipse.birt.report.engine, org.eclipse.birt.report.model等你需要的所有包后记得在自己的run--->configure-->plug-ins 里面把所有的birt都打上勾,否则运行会出错的
 
1,用birt设计rptdesign文件
(1)下载 birt framwork plugin放到eclipse目录下
(2)创建一个报表工程并制作一个报表(即rptdesign文件)
2,安装birt engine
下载 birt runtime 到任意目录,
3,创建一个java project 设置build 属性,使之包含birt 的core和report.engine,并把Report engine目录下的jar加到jar属性中
工程代码如下,其中几个路径根据自己配置设置:

<div style="border-right: #cccccc 1px solid; padding-right: 5px; border-top: #cccccc 1px solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 98%; padding-top: 4px; border-bottom: #cccccc 1px solid; background-color: #eeeeee;">import java.util.HashMap;

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

public class ExecuteReport {

 static void executeReport() throws EngineException
 {
  //Engine Configuration - set and get temp dir, BIRT home, Servlet context
  EngineConfig config = new EngineConfig();
  config.setEngineHome( "C:/birtruntime/birt-runtime-2_0_0/Report Engine" ); 
        
  //Create the report engine
  ReportEngine engine = new ReportEngine( config );
  
  
  //Open a report design - use design to modify design, retrieve embedded images etc. 
  IReportRunnable design = engine.openReportDesign("D:/LiuYanbin/working/TestBIRT/customers.rptdesign"); 
  
  //Create task to run the report - use the task to execute and run the report,
  IRunAndRenderTask task = engine.createRunAndRenderTask(design); 
  
  //Set Render context to handle url and image locataions
  HTMLRenderContext renderContext = new HTMLRenderContext();
  renderContext.setImageDirectory("image");
  HashMap contextMap = new HashMap();
  contextMap.put( EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT, renderContext );
  task.setAppContext( contextMap );
  
  //Set rendering options - such as file or stream output, 
  //output format, whether it is embeddable, etc
  HTMLRenderOption options = new HTMLRenderOption();
  options.setOutputFileName("D:/LiuYanbin/working/TestBIRT/customers.html");
  options.setOutputFormat("html");
  task.setRenderOption(options);
  
  //run the report and destroy the engine
  task.run();
  
  engine.destroy();
 } 
 /**
  * @param args
  */
 public static void main(String[] args) {
  try
  {
   executeReport( );
  }
  catch ( Exception e )
  {
   e.printStackTrace();
  }
 }

}
页: [1]
查看完整版本: 在plugin中用engine api引入BIRT已经设计好的rptdesign文件