[英]Start a BIRT report by Java (datasource is dynamic)
我想问一下,如果有人有一些经验,从Java代码开始BIRT报告。 BIRT 模板元素的数据源是动态的。 因此,我必须通过 Java 将数据源(CSV 或 XML 文件)告知 BIRT 报告。 也许有人在网上知道一个很好的例子。
关于如何将 BIRT 集成到 Java 应用程序中,从阅读 报表引擎 API 开始
BIRT 还提供了一个 API 来创建报告模板。 请参阅 设计引擎 API
即使答案被接受并且我同意有一个 API,当我需要这样做时,我不得不浪费一些宝贵的时间才能让它工作,因此,这是我最终得到的代码(部分)实现以创建一个 Java Runnable jar,它可以在控制台中获取一些参数,并生成 HTML、PDF 或两者的报告:
/**
* 0x01 is PDF,
* 0x02 is HTML
*/
static int supportedExportTypesFlag = 0x03;
public static void main(final String[] args) {
int result = -1;
IReportEngine engine = null;
EngineConfig config = null;
IReportRunnable design = null;
Object[] validationResults = ValidateArguments(args);
if (((Boolean)validationResults[0]).equals(Boolean.TRUE)) {
try {
config = new EngineConfig();
config.setLogConfig("./", Level.FINE);
Platform.startup(config);
IReportEngineFactory factory = (IReportEngineFactory) Platform
.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
engine = factory.createReportEngine(config);
engine.changeLogLevel(Level.WARNING);
design = engine.openReportDesign((String)validationResults[1]);
IRunAndRenderTask task = engine.createRunAndRenderTask(design);
byte[] paramStr = Files.readAllBytes(Paths.get((String)validationResults[3]));
InputStream iS = new ByteArrayInputStream(paramStr);
iS.mark(paramStr.length);
HashMap<String, Object> contextMap = new HashMap<String, Object>();
contextMap.put("org.eclipse.birt.report.data.oda.xml.inputStream", iS);
contextMap.put("org.eclipse.birt.report.data.oda.xml.closeInputStream", Boolean.FALSE);
task.setAppContext(contextMap);
int exportTypes = ((Integer)validationResults[2]).intValue();
if((exportTypes & 0x02) == 0x02)
{
final HTMLRenderOption HTML_OPTIONS = new HTMLRenderOption();
HTML_OPTIONS.setOutputFileName((String)validationResults[4] + ".html");
HTML_OPTIONS.setOutputFormat("html");
task.setRenderOption(HTML_OPTIONS);
task.run();
task.close();
}
iS.reset();
if((exportTypes & 0x01) == 0x01)
{
final PDFRenderOption PDF_OPTIONS = new PDFRenderOption();
PDF_OPTIONS.setOutputFileName((String)validationResults[4] + ".pdf");
PDF_OPTIONS.setOutputFormat("pdf");
task = engine.createRunAndRenderTask(design);
task.setAppContext(contextMap);
task.setRenderOption(PDF_OPTIONS);
task.run();
task.close();
}
iS.close();
} catch (IOException e) {
result = 1;
e.printStackTrace();
} catch (EngineException e) {
result = 2;
e.printStackTrace();
} catch (BirtException e) {
result = 3;
e.printStackTrace();
}
// Shutdown
engine.destroy();
Platform.shutdown();
// Bugzilla 351052
RegistryProviderFactory.releaseDefault();
result = 0;
}
System.exit(result);
}
我觉得这里的 ValidateArguments 不是那么重要,你可以猜到它做了什么,返回了什么。
希望这会帮助某人!
这是我集成在 Spring MVC 中的 java 代码,希望它有帮助:
public String executeReport(String path,HttpServletRequest request) throws EngineException
{
IReportEngine engine=null;
EngineConfig config = null;
try{
// start up Platform
config = new EngineConfig( );
config.setLogConfig("/logs", java.util.logging.Level.FINEST);
Platform.startup( config );
// create new Report Engine
IReportEngineFactory factory = (IReportEngineFactory) Platform
.createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
engine = factory.createReportEngine( config );
String format = "html";
ServletContext sc = request.getSession().getServletContext();
if( format == null ){
format="html";
}
// open the report design
IReportRunnable design = null;
design = engine.openReportDesign(path);
// create RunandRender Task
IRunAndRenderTask task = engine.createRunAndRenderTask(design);
// pass necessary parameter
task.setParameterValue("PARAMETER_NAME", "PARAMETER_VALUE");
task.validateParameters();
// set render options including output type
HTMLRenderOption options = new HTMLRenderOption();
ByteArrayOutputStream outs = new ByteArrayOutputStream();
options.setSupportedImageFormats("PNG;GIF;JPG;BMP;SWF;SVG");
options.setOutputStream(outs);
options.setImageHandler(new HTMLServerImageHandler());
options.setBaseImageURL(request.getContextPath()+"/images");
options.setImageDirectory(sc.getRealPath("/images"));
options.setEmbeddable(true);
options.setOutputFormat("html");
task.setRenderOption(options);
// run task
String output;
task.run();
output = outs.toString();
task.close();
engine.destroy();
return output;
}catch( Exception ex){
ex.printStackTrace();
return "Error";
}
finally
{
Platform.shutdown( );
}
}
您可以使用脚本数据源从数据库中获取 birt 报告中的动态数据。
前往: http : //www.eclipse.org/birt/phoenix/examples/scripting/scripteddatasource/获取脚本数据源基本信息。
以及进一步:http ://www.eclipse.org/birt/phoenix/deploy/reportScripting.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.