我正在开发一个报告应用程序,用户可以从 100 个报告的列表中选择(和订购)报告并请求主报告。该主报告应按准确的顺序包含所有选定的报告,并在目录中列出主报告中包含的(子)报告和正确的页码。
如何在 BIRT 中实现这一点?在此之前我使用的是 Pentaho,并且能够通过在运行时(即以编程方式)将每个用户选择的报告作为子报告添加到主报告中来完成相同的任务,这实际上是一个占位符报告。
现在我知道 BIRT 有子报告的概念,但我无法理解 BIRT DE API 来完成之前使用 Pentaho 创建主报告的工作。那么,我该怎么做呢?
来自How do I combine multiple BIRT reports ,这在 2008 年用 BIRT 好像是不可能的。现在还是这样吗?我不能获取独立报告并将其作为子报告添加到另一个报告中吗?
最佳答案
经过一番折腾,我想出了如何实现这一目标。基本上,我们必须以编程方式提取每个报告中的所有报告项目、数据集等,并将它们粘贴到新的主报告中。在每次子报告之后,我都会确保插入分页符,以便下一个报告出现在下一页上。一个粗略的代码是:-
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();
}
}
}
关于java - 将报表作为子报表动态添加到 BIRT 中的主报表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5403276/