2011-03-23 55 views
2

我正在開發報告應用程序,用戶可以從100個報告列表中選擇(並排序)報告並索取主報告。該主報告應該包含所有選定的報告,並且列出主報告中包含的(子)報告和正確的頁碼。動態地將報告作爲子報告添加到BIRT中的主報告中

我該如何做到這一點是BIRT?我在此之前使用過Pentaho,並且能夠在運行時將每個用戶選擇的報告作爲子報告(即以編程方式)添加到主報告中,從而實現同樣的功能,該報告實際上是佔位符報告。

現在我知道BIRT有子報表的概念,但我無法理解BIRT DE API來完成之前用Pentaho完成的創建主報表的操作。那麼,我該怎麼做?

How do I combine multiple BIRT reports看來,這在2008年的BIRT中是不可能的。這仍然是這種情況嗎?我不能獨立報告並將它們作爲子報告添加到其他報告中嗎?

回答

4

經過一番麻煩,我想出瞭如何實現這一點。基本上,我們必須以編程方式提取每個報告中的所有報告項目,數據集等,並將其粘貼在新的主報告中。在每個孩子重新使用後,我確保插入一個分頁符,以便下一個報告出現在下一頁。這樣做的一個粗略的代碼是: -

public class ReportGen { 
private static ReportDesignHandle reportDesignHandle; 
private static ElementFactory elementFactory; 
private static ReportDesignHandle reportDesignHandle1; 

public static void main(String[] args) { 
    executeReport(); 
} 

public static void executeReport() { 

    IReportEngine engine = null; 
    EngineConfig config = null; 

    try { 
     config = new EngineConfig(); 
     config.setBIRTHome("/home/vineeth/Softwares/birt-runtime-2_6_2/ReportEngine"); 
     config.setLogConfig("/home/vineeth/Softwares", Level.FINEST); 
     Platform.startup(config); 
     IReportEngineFactory factory = (IReportEngineFactory) Platform 
       .createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY); 
     engine = factory.createReportEngine(config); 

     IReportRunnable design = null; 
     //Open the report design 
     design = engine.openReportDesign("/home/vineeth/cash_flow_summary.rptdesign"); 
     SessionHandle sessionHandle = DesignEngine.newSession(null); 
     reportDesignHandle = sessionHandle.createDesign(); 
     elementFactory = reportDesignHandle.getElementFactory(); 
     reportDesignHandle1 = (ReportDesignHandle) design.getDesignHandle(); 
     DesignElementHandle cashflow = reportDesignHandle1.findElement("cashflow"); 
     DesignElementHandle designElementHandle = reportDesignHandle1.getBody().get(0); 
     if (designElementHandle instanceof ExtendedItemHandle) { 
      ExtendedItemHandle itemHandle = (ExtendedItemHandle) designElementHandle; 
      ExtendedItem item = (ExtendedItem) itemHandle.getElement(); 

      ExtendedItem newItem1 = (ExtendedItem) item.doClone(CopyForPastePolicy.getInstance()); 
      newItem1.setName("cf1"); 
      newItem1.setProperty(DesignChoiceConstants.CHOICE_PAGE_BREAK_AFTER, DesignChoiceConstants.PAGE_BREAK_AFTER_ALWAYS); 
      ExtendedItemHandle newItemHandle1 = new ExtendedItemHandle(reportDesignHandle.getModule(), newItem1); 
      reportDesignHandle.getBody().add(newItemHandle1); 

      ExtendedItem newItem2 = (ExtendedItem) item.doClone(CopyForPastePolicy.getInstance()); 
      newItem2.setName("cf2"); 
      newItem2.setProperty(DesignChoiceConstants.CHOICE_PAGE_BREAK_AFTER, DesignChoiceConstants.PAGE_BREAK_AFTER_ALWAYS); 
      ExtendedItemHandle newItemHandle2 = new ExtendedItemHandle(reportDesignHandle.getModule(), newItem2); 
      reportDesignHandle.getBody().add(newItemHandle2); 

      ExtendedItem newItem3 = (ExtendedItem) item.doClone(CopyForPastePolicy.getInstance()); 
      newItem3.setName("cf3"); 
      newItem3.setProperty(DesignChoiceConstants.CHOICE_PAGE_BREAK_AFTER, DesignChoiceConstants.PAGE_BREAK_AFTER_ALWAYS); 
      ExtendedItemHandle newItemHandle3 = new ExtendedItemHandle(reportDesignHandle.getModule(), newItem3); 
      reportDesignHandle.getBody().add(newItemHandle3); 

      DataSourceHandle dataSourceHandle = (DataSourceHandle) reportDesignHandle1.getDataSources().get(0); 
      DataSource ds = (DataSource) dataSourceHandle.copy(); 
      DataSourceHandle newDSHandle = null; 
      if (ds instanceof OdaDataSource) { 
       newDSHandle = new OdaDataSourceHandle(reportDesignHandle.getModule(), ds); 
      } 
      reportDesignHandle.getDataSources().add(newDSHandle); 


      DataSetHandle dataSetHandle = (DataSetHandle) reportDesignHandle1.getDataSets().get(0); 
      OdaDataSet copyDataSetHandle = (OdaDataSet) dataSetHandle.copy(); 
      DataSetHandle copyDSHandle = new OdaDataSetHandle(reportDesignHandle.getModule(), copyDataSetHandle); 
      reportDesignHandle.getDataSets().add(copyDSHandle); 
     } 
     //reportDesignHandle.getBody().add(reportDesignHandle1.getBody().get(0)); 
     //reportDesignHandle.getBody().add(reportDesignHandle1.getBody().get(0)); 
     IReportRunnable newDesign = engine.openReportDesign(reportDesignHandle); 
     IRunAndRenderTask task = engine.createRunAndRenderTask(newDesign); 
     HTMLRenderOption options = new HTMLRenderOption(); 
     options.setOutputFileName("/home/vineeth/Softwares/out.html"); 
     options.setOutputFormat("html"); 
     task.setRenderOption(options); 
     task.run(); 
     task.close(); 
     engine.destroy(); 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } finally { 
     Platform.shutdown(); 
    } 
} 

}

1

與其試圖在運行時動態地將報告拼湊在一起,可能更容易構建包含所有100個組件的主報告。然後用值填充每個組件(或子報表)的書籤屬性。最後將每個組件的可見性屬性默認設置爲「false」。

在運行時,當用戶選擇他們想要查看的子報表時,您可以將所需的子報表作爲參數傳遞,此時您可以切換可見性屬性,以便只顯示所需的子報表。

這應該比編寫大量編譯代碼容易得多,並且可以隨時隨地添加和刪除子報表,而無需編寫任何代碼。

祝你好運!

+0

我沒想到這種做法起初,但放棄了它有2個原因: - 1)它不會滿足於生成報告的重要要求訂單是由用戶選擇的; 2)報告最常被創建,但技術上不是那麼專業。這種方法給報告的設計階段帶來了很多複雜性,這是我不想要的。我很高興能夠在代碼中提高這種複雜性。 – Vineeth 2011-03-23 19:46:38

+0

所有優點。對不起,這個想法沒有足夠緊密與您的需求... – MystikSpiral 2011-03-23 22:11:34

+0

請你可以在這裏舉一個例子 - 至於如何建立一個主要報告的組件?至於如何將一份主報告中的所有報告保留在沒有任何分頁符的情況下?我無法在birt樣本中的任何地方找到多頁報告...謝謝! – Anna 2012-05-02 04:33:18