Posted on 2008-01-23 13:54
Raul Gong 閱讀(2779)
評論(13) 編輯 收藏 所屬分類:
eclipse 、
birt
首先,本文講的方法適合的環境是:使用xml作為birt的數據源,但不同場合需要不同的xml文件,但制作.rptdesign文件時只運行輸入一個xml文件,怎樣實現動態數據源呢?以下將解決這個問題。
同時,本文所講的方法,可以稍加改造,適合所有需要管理器向.rptdesign文件傳入參數的情況。例如,你不僅需要動態數據源,還可以需要動態的報表標題、某處的文字、動態的參數、等等。
在這里,我只說思路代碼,具體細節,見我提供的實例工程。
這里,我以插件工程為例。首先,建立一個工程,然后是建一個.rptdesign,在其中的數據源,選xml,然后使用如下的xml文件的路徑:
<ROOT>
<FACTOR NAME="Maintainability">
<PECENT NAME="Execllent" VALUE="0.00"/>
<PECENT NAME="Good" VALUE="0.75"/>
<PECENT NAME="Fair" VALUE="0.25"/>
<PECENT NAME="Poor" VALUE="0.00"/>
</FACTOR>
</ROOT>
注意在outline視圖中選中 Data Source ,然后在屬性窗口中選中 Event Handler ,在里面填入類的地址:
net.rual.learn.eventhandler.FunTableHandler
然后在相應位置建立這個FunTableHandler類,繼承至DataSourceEventAdapter類,覆蓋beforeOpen方法,如下:
package net.raul.lern.eventhandler;

import org.eclipse.birt.report.engine.api.script.IReportContext;
import org.eclipse.birt.report.engine.api.script.eventadapter.DataSourceEventAdapter;
import org.eclipse.birt.report.engine.api.script.instance.IDataSourceInstance;


/** *//**
* @author Vincent
*
*/

public class FunTableHandler extends DataSourceEventAdapter
{


@Override
public void beforeOpen(IDataSourceInstance dataSource,

IReportContext reportContext)
{
// TODO Auto-generated method stub

String xmlFile = (String) reportContext.getReportRunnable()
.getReportEngine().getConfig().getProperty("user.datasource");
// xmlFile =
// "E:/c.xml";
dataSource.setExtensionProperty("FILELIST", xmlFile);
super.beforeOpen(dataSource, reportContext);
}

}
然后編寫管理器類:
public String executeReport(String repPath, String xmlFilePath,

String FileName, String FunName) throws EngineException
{
String outPutFilePath = proPath + "/report/" + FunName + "_"
+ FileName.substring(0, FileName.length() - 4) + ".html";

// Engine Configuration - set and get temp dir, BIRT home, Servlet
// context
EngineConfig config = new EngineConfig();
// config.setEngineHome(
// "E:/work/eclipses/eclipse4birt/birt-runtime-2_2_1_1/ReportEngine" );
config.setEngineHome(getEnvirStr("BIRT_HOME") + "/ReportEngine");
config.setProperty("user.projectclasspath",
"E:/work/workspaces/kaudit/kaudit.071224/com.zte.audit.ui/bin");
config.setProperty("user.datasource", xmlFilePath);
// config.setProperty("user.funname", "main");
// 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(repPath);
// Create task to run the report - use the task to execute and run the
// report,
IRunAndRenderTask task = engine.createRunAndRenderTask(design);
task.getAppContext().put(EngineConstants.APPCONTEXT_CLASSLOADER_KEY,
FunTableHandler.class.getClassLoader());
// IReportRunnable runnable = engine.openReportDesign( source );
// 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);
// contextMap
// .put(EngineConstants.PROJECT_CLASSPATH_KEY,
// "E:/work/workspaces/kaudit/kaudit.071224/com.zte.audit.ui/bin");
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:/customer.html");
options.setOutputFileName(outPutFilePath);
options.setOutputFormat("html");
task.setRenderOption(options);
// run the report and destroy the engine
task.run();
// task.addScriptableJavaObject(arg0, arg1)
engine.destroy();

return outPutFilePath;
}


public static String getEnvirStr(String name)
{
// System.getenv("BIRT_HOME");
return System.getenv(name);
}

因為時間很緊,我沒有辦法寫得很詳細,等過年放假的時候,我好好整理,再放上來。