1

我想问一下,如果有人有经验,从 Java 代码开始 BIRT 报告。BIRT 模板元素的数据源是动态的。因此,我必须通过 Java 将数据源(CSV 或 XML 文件)告知 BIRT 报告。也许有人知道网络上的一个很好的例子。

4

4 回答 4

2

关于如何将 BIRT 集成到 Java 应用程序中,请从阅读Report Engine API开始

BIRT 还提供了一个 API 来创建报告模板。请参阅设计引擎 API

于 2013-10-22T15:13:01.443 回答
1

即使答案被接受并且我同意有一个 API,当我需要这样做时,我不得不浪费一些宝贵的时间来让它工作,因此,这是我结束的代码(部分)实现以创建一个 Java Runnable jar,该 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 并不是那么重要,你可以猜到它做了什么,它返回了什么。

希望这会对某人有所帮助!

于 2015-01-29T16:01:41.057 回答
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( );

    }
}
于 2016-08-17T21:25:42.050 回答
0

您可以使用脚本数据源从数据库中获取 birt 报告中的动态数据。

去: http: //www.eclipse.org/birt/phoenix/examples/scripting/scripteddatasource/ 脚本数据源基本。

并进一步: http: //www.eclipse.org/birt/phoenix/deploy/reportScripting.php

于 2013-11-08T08:32:30.877 回答