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

          birt中動態數據的實現

          Posted on 2008-01-23 13:54 Raul Gong 閱讀(2789) 評論(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 小攤
          可以給我一份嗎?呵呵
          主站蜘蛛池模板: 昔阳县| 友谊县| 普兰县| 寿阳县| 古浪县| 富民县| 奈曼旗| 鲁山县| 图片| 军事| 濉溪县| 岳阳县| 恩施市| 南川市| 曲水县| 高州市| 绵阳市| 连平县| 旺苍县| 柘城县| 扬中市| 津南区| 紫阳县| 武冈市| 收藏| 和平县| 曲周县| 襄城县| 辽阳市| 犍为县| 汉沽区| 龙川县| 保定市| 武义县| 红原县| 普格县| 澜沧| 怀来县| 上虞市| 大余县| 驻马店市|