java 代码设置birt报表参数

前提是报表中设置好了报表参数。

在执行报表的时候可以传递具体的值给报表参数。代码如下:

         HashMap parameter = new HashMap();
         parameter.put("parameter", String.valueOf("1"));
         //Set parameters for the report
         task.setParameterValues(parameter);

例子ExecuteReport.java

package com.youleyang.learn.birt;
import java.util.HashMap;
import java.util.logging.Level;
 
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.engine.api.EngineConfig;
import org.eclipse.birt.report.engine.api.EngineConstants;
import org.eclipse.birt.report.engine.api.EngineException;
import org.eclipse.birt.report.engine.api.HTMLActionHandler;
import org.eclipse.birt.report.engine.api.HTMLEmitterConfig;
import org.eclipse.birt.report.engine.api.HTMLRenderContext;
import org.eclipse.birt.report.engine.api.HTMLRenderOption;
import org.eclipse.birt.report.engine.api.HTMLServerImageHandler;
import org.eclipse.birt.report.engine.api.IReportEngine;
import org.eclipse.birt.report.engine.api.IReportEngineFactory;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
import org.eclipse.birt.report.engine.api.PDFRenderOption;
 
public class ExecuteReport {
    static void executeReport() throws EngineException {
//        final HashMap<String, Integer> PARAMETERS = new HashMap<String, Integer>();
//
//        final String NAME = "Top Count";
//        final Integer VALUE = new Integer(4);
//
//        PARAMETERS.put(NAME, VALUE);
//
//        IReportEngine engine = null;
//        EngineConfig config = null;
//
//        try {
//
//           // Configure the Engine and start the Platform
//           config = new EngineConfig();
//           config.setEngineHome("D:/birt/birt-runtime-4_2_1/ReportEngine");
//           // set log config using (null, Level) if you do not want a log file
//           config.setLogConfig("D:/birt/birt1/logs", Level.FINE);
//
//           Platform.startup(config);
//           final IReportEngineFactory FACTORY = (IReportEngineFactory) Platform.
//                 createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
//
//           engine = FACTORY.createReportEngine(config);
//           engine.changeLogLevel(Level.WARNING);
//
//        } catch(Exception ex) {
//           ex.printStackTrace();
//        }
//
//        // Configure the emitter to handle actions and images
//        final HTMLEmitterConfig EMITTER_CONFIGURATION = new HTMLEmitterConfig();
//
//        EMITTER_CONFIGURATION.setActionHandler(new HTMLActionHandler());
//        HTMLServerImageHandler imageHandler = new HTMLServerImageHandler();
//        EMITTER_CONFIGURATION.setImageHandler(imageHandler);
//        config.getEmitterConfigs().put("html", EMITTER_CONFIGURATION); // $NON-NLS-1$
//
//        // Open the report design
//        final IReportRunnable DESIGN =
//              engine.openReportDesign("D:/birt/birt1/test.rptdesign");
//
//        // Create task to run and render the report:
//        final IRunAndRenderTask TASK = engine.createRunAndRenderTask(DESIGN);
//
//        // Set Render context to handle URL and image locations
//        final HTMLRenderContext RENDER_CONTEXT = new HTMLRenderContext();
//        // Set the Base URL for all actions
//        RENDER_CONTEXT.setBaseURL("http:// localhost/");
//        // Tell the Engine to prepend all images with this URL - Note this requires using
//        // the HTMLServerImageHandler
//        RENDER_CONTEXT.setBaseImageURL("http:// localhost/myimages");
//        // Tell the Engine where to write the images to
//        RENDER_CONTEXT.setImageDirectory("D:/birt/birt1/myimages");
//        // Tell the Engine what image formats are supported.  Note you must have SVG in the string
//        // to render charts in SVG.
//        RENDER_CONTEXT.setSupportedImageFormats("JPG;PNG;BMP;SVG");
//        final HashMap<String, HTMLRenderContext> CONTEXT =
//              new HashMap<String, HTMLRenderContext>();
//        CONTEXT.put(EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT, RENDER_CONTEXT);
//        TASK.setAppContext(CONTEXT);
//        // Set PARAMETERS for the report
//        TASK.setParameterValues(PARAMETERS);
//        // Alternatively set each separately
//        // TASK.setParameterValue("Top Count", new Integer(12));
//        TASK.validateParameters();
//
//        // Add a scriptable object, which will allow the report developer to put
//        // script in the report that references this Java object, e.g. in script
//        // pFilter.myjavamethod()
//        // final ProcessFilter PF = new ProcessFilter();
//        // TASK.addScriptableJavaObject("pFilter", PF);
//
//        // Set rendering options - such as file or stream output,
//        // output format, whether it is embeddable, etc
//        final HTMLRenderOption HTML_OPTIONS = new HTMLRenderOption();
//
//        // Remove HTML and Body tags
//        // HTML_OPTIONS.setEmbeddable(true);
//
//        // Set output location
//        HTML_OPTIONS.setOutputFileName("D:/birt/birt1/output.html");
//
//        // Set output format
//        HTML_OPTIONS.setOutputFormat("html");
//        TASK.setRenderOption(HTML_OPTIONS);
//
//        // run the report and destroy the engine
//        // Note - If the program stays resident do not shutdown the Platform or the Engine
//        TASK.run();
//        TASK.close();
//        engine.shutdown();
//        Platform.shutdown();
//        System.out.println("Finished");
 
 IReportEngine engine = null;
    EngineConfig config = null;
 
    try {
        config = new EngineConfig();         
        //config.setBIRTHome("D://birt//birt-runtime-osgi-4_2_1//ReportEngine");
        config.setBIRTHome("D://birt//birt-runtime-4_2_1//ReportEngine");
        config.setLogConfig("D:/birt/birt1/test", Level.FINEST);
        Platform.startup(config);
        final IReportEngineFactory FACTORY = (IReportEngineFactory) Platform
            .createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
        engine = FACTORY.createReportEngine(config);      
 
        // Open the report design
        IReportRunnable design = null;
        design = engine.openReportDesign("D:/eclipse-reporting-juno-SR1-win32-x86_64/workspace/BIRT/parameter.rptdesign");
        IRunAndRenderTask task = engine.createRunAndRenderTask(design);       
        // task.setParameterValue("Top Count", (new Integer(5)));
        // task.validateParameters();
 
//        final HTMLRenderOption HTML_OPTIONS = new HTMLRenderOption();      
//        HTML_OPTIONS.setOutputFileName("D:/birt/birt1/output/Parmdisp.html");
//        HTML_OPTIONS.setOutputFormat("html");
        // HTML_OPTIONS.setHtmlRtLFlag(false);
        // HTML_OPTIONS.setEmbeddable(false);
        // HTML_OPTIONS.setImageDirectory("C:\\test\\images");
 
         PDFRenderOption PDF_OPTIONS = new PDFRenderOption();
         PDF_OPTIONS.setOutputFileName("D:/birt/birt1/output/Parmdisp.pdf");
         PDF_OPTIONS.setOutputFormat("pdf");
 
        //task.setRenderOption(HTML_OPTIONS);
         task.setRenderOption(PDF_OPTIONS);
         HashMap parameter = new HashMap();
         parameter.put("parameter", String.valueOf("1"));
         //Set parameters for the report
         task.setParameterValues(parameter);
        task.run();
        task.close();
        engine.destroy();
    } catch(final Exception EX) {
        EX.printStackTrace();
    } finally {
       Platform.shutdown();
    }

    }
 
    /**
     * @param ARGUMENTS
     */
    public static void main(final String[] ARGUMENTS) {
        try {
           executeReport();
           System.out.println("OK");
        } catch (final Exception EX) {
           EX.printStackTrace();
        }
    }
   
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值