birt java api_「Birt」birt api生成报表 | 学步园

public class PDFReportServiceAccess {

/** */

/** 初始化的状态 */

protected static boolean initStatus = false;

private static IReportEngine engine = null;

private static EngineConfig config = null;

private static IReportRunnable design = null;

//private static PDFRenderOption ro = null;

private static PDFRenderOption ro = null;    //   doubley0

/** */

/** 初始化资源 */

public void initilize() {

if (initStatus == true)

return;

try {

config = new EngineConfig();

config.setEngineHome("E://Program Files1//apache-tomcat-5.5.26//webapps//birt//WEB-INF//platform"); // birt    doubley1

// runtime

// web应用中的报表引擎目录

config.setLogConfig("E://Program Files1//apache-tomcat-5.5.26//webapps//birt//logs", Level.FINE);        //doubley2

Platform.startup(config);

IReportEngineFactory factory = (IReportEngineFactory) Platform

.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);

engine = factory.createReportEngine(config);

engine.changeLogLevel(Level.WARNING);

ro = new PDFRenderOption();

ro.setBaseURL("http://localhost:8080/birt"); // birt runtime

// web应用

config.getEmitterConfigs().put("pdf", ro); // 生成pdf格式       doubley3

initStatus = true;

} catch (Exception ex) {

ex.printStackTrace();

initStatus = false;

}

}

/** */

/** 释放资源 */

public void release() {

engine.shutdown();

Platform.shutdown();

initStatus = false;

}

protected OutputStream run(String filename, HashMap parameters)

throws EngineException {

design = engine.openReportDesign(filename);

// Create task to run and render the report,

IRunAndRenderTask task = engine.createRunAndRenderTask(design);

HashMap contextMap = new HashMap();

contextMap.put(EngineConstants.APPCONTEXT_PDF_RENDER_CONTEXT, ro);

task.setAppContext(contextMap);

task.setParameterValues(parameters);

task.validateParameters();

OutputStream os = new ByteArrayOutputStream();

ro.setOutputStream(os);

ro.setOutputFormat("pdf");       //doubley4

task.setRenderOption(ro);

task.run();

task.close();

return os;

}

/** */

/**

* 生成PDF格式报表,以OutputStream格式返回

*

* @param filename

*            报表设计文件名全路径

* @param parameters

*            报表参数

* @return ByteArrayOutputStream

* @throws EngineException

*/

public OutputStream call(String filename, HashMap parameters)

throws EngineException {

initilize();

OutputStream os = run(filename, parameters);

release();

return os;

}

/** */

/**

* @param args

*/

public static void main(String[] args) {

HashMap parameters = new HashMap();

// 三个Report Parameters,名称必须在报表设计文件中预先定义好

//parameters.put("begindate", "2004/01/01");

//parameters.put("enddate", "2007/12/31");

//parameters.put("sql", " where cust_id = 1234567");

int temp=7;

parameters.put("mystr1", "7");           //doubley5

//parameters.put("mystr1", temp);

ByteArrayOutputStream bos = null;

PDFReportServiceAccess ebr = new PDFReportServiceAccess();

String filename = "E://Program Files1//apache-tomcat-5.5.26//webapps//birt//new_report1.rptdesign";        //doubley6

try {

bos = (ByteArrayOutputStream) ebr.call(filename, parameters);

OutputStream fis = new FileOutputStream("c:/test.pdf");

bos.writeTo(fis);

} catch (Exception e) {

e.printStackTrace();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值