我想问一下,如果有人有经验,可以从Java代码开始BIRT报告。 BIRT模板元素的数据源是动态的。因此,我必须通过Java将数据源(CSV或XML文件)告诉BIRT报告。也许有人在网上知道一个很好的例子。
答案 0 :(得分:2)
如何在Java应用程序中集成BIRT,首先阅读Report Engine API
BIRT还提供了一个用于创建报告模板的API。见Design Engine API
答案 1 :(得分:1)
即使答案被接受并且我同意有一个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不是那么重要,你可以猜测它的作用和返回的内容。
希望这会对某人有所帮助!
答案 2 :(得分:1)
这是我在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( );
}
}
答案 3 :(得分:0)
您可以使用脚本数据源从数据库中获取birt报告中的动态数据。
选择:http://www.eclipse.org/birt/phoenix/examples/scripting/scripteddatasource/ 用于脚本数据源基础。
并进一步:http://www.eclipse.org/birt/phoenix/deploy/reportScripting.php