java 如何通过 BIRT API 设置参数并将参数传递给 BIRT 报告设计器创建的 BIRT 报告?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10747427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you set and pass a parameter to a BIRT report created by the BIRT Report Designer through the BIRT API?
提问by Dale
I've created a simple report that takes a single parameter. This parameter is used in the query and executes fine when directly executed in the report designer. By the way I'm not using javascript or any scripting for this report. I've seen some people trying to pass parameters using scripts and/or javascripts for answers here, however this is not what I'm doing. I pass all my parameters in through java. Continuing, in this report I'm listing active/inactive items. I pass in an 'N' for listing inactive items and a 'Y' for active items. When I try to pass in a parameter through the API, I always get a list of active items regardless to what I pass in. By the way 'Y' is the default value of the parameter passed in. (I am overriding the defaults in the code below) The problem I'm having is that the report doesn't seem to want to take the parameter I set. Yes the value changes in my variable passed in but the report doesn't reflect the change. My code is below. I've tried to follow the advice of this link and how to set the parameters.
我创建了一个采用单个参数的简单报告。该参数在查询中使用,直接在报表设计器中执行时执行良好。顺便说一下,我没有为这份报告使用 javascript 或任何脚本。我看到有些人试图在这里使用脚本和/或 javascripts 传递参数以获取答案,但这不是我正在做的。我通过java传递我的所有参数。继续,在本报告中,我将列出活动/非活动项目。我传入一个“N”来列出非活动项目,一个“Y”代表活动项目。当我尝试通过 API 传入参数时,无论我传入什么,我总是会得到一个活动项目列表。顺便说一下,'Y' 是传入参数的默认值。(我覆盖了默认值下面的代码)我的问题 我所拥有的是报告似乎不想采用我设置的参数。是的,我传入的变量中的值发生了变化,但报告并未反映该变化。我的代码如下。我已尝试遵循此链接的建议以及如何设置参数。
http://www.eclipsezone.com/eclipse/forums/t67723.html
http://www.eclipsezone.com/eclipse/forums/t67723.html
If you go to the link go down to #4 and see the list of tasks to do. This is what I have tried to follow. I feel I may be missing something. If you've got this going could you give me some advice to what I'm missing? Thanks much!
如果您转到链接,请转到 #4 并查看要执行的任务列表。这就是我试图遵循的。我觉得我可能错过了一些东西。如果你这样做了,你能给我一些关于我遗漏的建议吗?非常感谢!
-Dale
-戴尔
public class ReportGenerator {
public static void main(String args[]) throws Exception{
ReportGenerator rg = new ReportGenerator();
rg.executeReport("N");
}
@SuppressWarnings({ "unchecked", "deprecation" })
public void executeReport(String activeIndicator) throws EngineException {
IReportEngine engine=null;
EngineConfig config = null;
try{
config = new EngineConfig( );
config.setBIRTHome("C:\birt-rcp-report-designer-3_7_2\ReportEngine");
config.setLogConfig("c:/temp/test", Level.FINEST);
Platform.startup( config );
IReportEngineFactory factory = (IReportEngineFactory) Platform.createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
engine = factory.createReportEngine( config );
IReportRunnable reportDesign = null;
reportDesign = engine.openReportDesign("C:\workspace\SimpleReport\ReportTemplates\ItemListingReport.rptdesign");
IRunAndRenderTask task = engine.createRunAndRenderTask(reportDesign);
IGetParameterDefinitionTask parameterDefinitionTask = engine.createGetParameterDefinitionTask(reportDesign);
parameterDefinitionTask.evaluateDefaults();
HashMap<String, String> params = parameterDefinitionTask.getDefaultValues();
params.put("aIndicator", activeIndicator);
parameterDefinitionTask.setParameterValues(params);
ConnectionHelper connectionHelper = new ConnectionHelper();
task.getAppContext().put("OdaJDBCDriverPassInConnection", connectionHelper.getConnection());
PDFRenderOption options = new PDFRenderOption();
options.setOutputFormat("pdf");
options.setOutputFileName("C:\workspace\SimpleReport\output\itemListingReport.pdf");
task.setRenderOption(options);
task.run();
task.close();
engine.destroy();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
Platform.shutdown();
}
}
}
回答by Nick Wilson
You need to set the parameters on the IRunAndRenderTask:
您需要在 IRunAndRenderTask 上设置参数:
IRunAndRenderTask task =
engine.createRunAndRenderTask(reportRunnable);
Map< String, Object > birtParams = ...;
task.setParameterValues( birtParams );