posts - 20, comments - 16, trackbacks - 0, articles - 0

birt中动态数据的实现

Posted on 2008-01-23 13:54 Raul Gong 阅读(2736) 评论(13)  编辑  收藏 所属分类: eclipsebirt

首先,本文讲的方法适合的环境是:使用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);
    }



因为时间很紧,我没有办法写得很详细,等过年放假的时候,我好好整理,再放上来。

Feedback

# re: birt中动态数据的实现[未登录]  回复  更多评论   

2008-03-26 11:04 by cxh
写的好,继续呀

# re: birt中动态数据的实现[未登录]  回复  更多评论   

2008-05-12 18:16 by cxh
老大,例子程序传一份,让俺也学习学习

# re: birt中动态数据的实现  回复  更多评论   

2008-05-19 12:32 by Raul Gong
@cxh
好的,你的邮箱是多少?你也可以加我的msn哈:
vincent.gong@hotmail.com

# re: birt中动态数据的实现  回复  更多评论   

2008-07-04 13:48 by RogerTu
BIRT开发团队就在国内,关于BIRT的问题,推荐BIRT官方中文论坛http://www.actuatechina.com/index.php,工程师们的响应还是赞的

# re: birt中动态数据的实现  回复  更多评论   

2008-09-27 10:26 by new birt
老大给我一份,行不嘛
jinsui_sc@163.com
谢谢!

# re: birt中动态数据的实现  回复  更多评论   

2008-10-21 14:09 by jm
多谢!读了后帮助很大。

# re: birt中动态数据的实现  回复  更多评论   

2009-02-04 14:04 by fzl
能否给发一份完整的实例,谢谢!
邮箱:fanzengl@eastsoft.com.cn

# re: birt中动态数据的实现[未登录]  回复  更多评论   

2009-04-10 00:49 by test
http://www.javaworld.com/javaworld/jw-06-2008/jw-06-osgi3.html?page=5

# re: birt中动态数据的实现[未登录]  回复  更多评论   

2009-04-10 01:02 by test
http://developer.51cto.com/java/

# re: birt中动态数据的实现[未登录]  回复  更多评论   

2009-05-09 00:45 by test
flex
http://www.jeromeloo.cn/?page_id=180
http://livedocs.adobe.com/flex/3/html/help.html
http://www.jeromeloo.cn/wp-demo/SpringGraph/SpringGraphDemo.htm
http://www.diybl.com/course/1_web/webjs/200877/131260.html
l

# re: birt中动态数据的实现[未登录]  回复  更多评论   

2009-05-09 00:52 by test
http://www.adobe.com/cfusion/exchange/index.cfm?event=extensionDetail&extid=1048510#

# re: birt中动态数据的实现[未登录]  回复  更多评论   

2009-09-09 10:54 by test
http://www.cnblogs.com/wyqtomorrow/archive/2007/05/19/752275.html

# re: birt中动态数据的实现  回复  更多评论   

2012-07-26 09:22 by 小摊
可以给我一份吗?呵呵

只有注册用户登录后才能发表评论。


网站导航: