欢迎来到Doc100.Net免费学习资源知识分享平台!
您的位置:首页 > 程序异常 >

利用birt api生成表格rptdesign文件

更新时间: 2014-01-05 03:07:24 责任编辑: Author_N1

 

利用BIRT API生成报表rptdesign文件
package com;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.engine.api.HTMLRenderOption;
import org.eclipse.birt.report.engine.api.IReportEngine;
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
import org.eclipse.birt.report.model.api.CellHandle;
import org.eclipse.birt.report.model.api.DesignConfig;
import org.eclipse.birt.report.model.api.DesignElementHandle;
import org.eclipse.birt.report.model.api.ElementFactory;
import org.eclipse.birt.report.model.api.GridHandle;
import org.eclipse.birt.report.model.api.IDesignEngine;
import org.eclipse.birt.report.model.api.IDesignEngineFactory;
import org.eclipse.birt.report.model.api.ImageHandle;
import org.eclipse.birt.report.model.api.LabelHandle;
import org.eclipse.birt.report.model.api.ReportDesignHandle;
import org.eclipse.birt.report.model.api.RowHandle;
import org.eclipse.birt.report.model.api.SessionHandle;
import org.eclipse.birt.report.model.api.activity.SemanticException;

import com.ibm.icu.util.ULocale;

/**
 * Simple BIRT Design Engine API (DEAPI) demo.
 */
public class SimpleCreate {
	public static IReportEngine birtReportEngine = null;
	
	public static void main(String[] args) {
		birtReportEngine = BirtEngine.getBirtEngine();
		try {
			buildReport();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SemanticException e) {
			e.printStackTrace();
		}
	}

	static void buildReport() throws IOException, SemanticException {
		DesignConfig config = new DesignConfig();
		config.setProperty("BIRT_HOME", Utils.BIRT_HOME);
		IDesignEngine engine = null;
		try {
			Platform.startup(config);
			IDesignEngineFactory factory = (IDesignEngineFactory) Platform
					.createFactoryObject(IDesignEngineFactory.EXTENSION_DESIGN_ENGINE_FACTORY);
			engine = factory.createDesignEngine(config);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
		SessionHandle session = engine.newSessionHandle(ULocale.ENGLISH);
		ReportDesignHandle design = session.createDesign();
		ElementFactory efactory = design.getElementFactory();
		
		DesignElementHandle element = efactory.newSimpleMasterPage("Page Master");
		design.getMasterPages().add(element);
		
		GridHandle grid = efactory.newGridItem(null, 2, 1);
		design.getBody().add(grid);
		grid.setWidth("100%");
		
		RowHandle row = (RowHandle) grid.getRows().get(0);
		ImageHandle image = efactory.newImage(null);
		image.setURL("\"1.jpg\"");
		
		CellHandle cell = (CellHandle) row.getCells().get(0);
		cell.getContent().add(image);
		
		LabelHandle label = efactory.newLabel(null);
		label.setText("Hello, world!");
		cell = (CellHandle) row.getCells().get(1);
		cell.getContent().add(label);
		
		design.saveAs(Utils.SAVE_HOME + "sample.rptdesign");
		design.close();
		System.out.println("Finished");
		
		/*IRunAndRenderTask task = null;
		OutputStream out = null;
		try {
			//task = birtReportEngine.createRunAndRenderTask(design);
			//task.setAppContext(contextMap);
			//task.setParameterValues(context.getRptParamters());
			
			HTMLRenderOption options = new HTMLRenderOption();
			options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_HTML);// HTML
			options.setHtmlPagination(true);
			

			out = new FileOutputStream(new File("D:\\dev-workspace\\workspace-v4.0\\BIRT\\temp\\a.html"));
			// 设置输出选项
			options.setOutputStream(out);
			task.setRenderOption(options);

			// 运行报表
			task.run();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			out.close();
			if (task != null) {
				task.close();
			}
		}*/
		
	}
}
上一篇:上一篇
下一篇:下一篇

 

随机推荐程序问答结果

 

 

如对文章有任何疑问请提交到问题反馈,或者您对内容不满意,请您反馈给我们DOC100.NET论坛发贴求解。
DOC100.NET资源网,机器学习分类整理更新日期::2014-01-05 03:07:24
如需转载,请注明文章出处和来源网址:http://www.doc100.net/bugs/t/18599/
本文WWW.DOC100.NET DOC100.NET版权所有。