【问题标题】:Dynamically add reports as subreports to a master report in BIRT将报告作为子报告动态添加到 BIRT 中的主报告
【发布时间】:2011-03-23 09:43:21
【问题描述】:

我正在开发一个报告应用程序,用户可以在其中从 100 个报告的列表中选择(和订购)报告并请求主报告。此主报告应按确切顺序包含所有选定的报告,并附有一个目录,列出主报告中包含的(子)报告和正确的页码。

BIRT 是如何实现的?在此之前我使用的是 Pentaho,并且能够通过在运行时(即以编程方式)将每个用户选择的报告作为子报告添加到主报告中来完成相同的操作,这实际上是一个占位符报告。

现在我知道 BIRT 有子报告的概念,但我无法理解 BIRT DE API 来完成之前使用 Pentaho 创建主报告所做的事情。那么,我该怎么做呢?

How do I combine multiple BIRT reports 看来,2008 年的 BIRT 似乎无法做到这一点。现在还是这样吗?我不能获取独立报告并将它们作为子报告添加到另一个报告中吗?

【问题讨论】:

    标签: java reporting birt


    【解决方案1】:

    经过一番麻烦,我想出了如何实现这一点。基本上,我们必须以编程方式提取每个报告中的所有报告项、数据集等,并将它们粘贴到新的主报告中。在每个孩子重新报告后,我确保插入分页符,以便下一个报告将出现在下一页上。一个粗略的代码是:-

    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();
        }
    }
    

    }

    【讨论】:

      【解决方案2】:

      与其尝试在运行时动态地将报表拼凑在一起,不如构建一个包含所有 100 个组件的主报表可能更容易。然后用值填充每个组件(或子报表)的书签属性。最后将每个组件的可见性属性默认设置为“false”。

      在运行时,当用户选择他们想要查看的子报表时,您可以将所需的子报表作为参数传入,此时您可以切换可见性属性,以便只显示所需的子报表。

      这应该比编写大量编译代码容易得多,并且让您可以灵活地随时添加和删除子报表,而无需编写任何代码。

      祝你好运!

      【讨论】:

      • 我最初确实考虑过这种方法,但出于 2 个原因放弃了它:- 1) 它不能满足按用户选择的顺序生成报告的重要要求,以及 2)报告通常是由技术人员创建的,但不是技术人员。这种方法使报告的设计阶段变得非常复杂,这是我不想要的。我可以在代码中提高这种复杂性。
      • 请您在这里举个例子 - 关于如何构建包含组件的主报告?就像如何在没有任何分页符的情况下将所有报告合并到一个主报告中一样?我无法在 birt 样本中的任何地方找到多页报告...谢谢!!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多