5.1、使用BIRT API创建List.doc

   在使用BIRT API前,得先配置一下BIRT的环境,也就是Birt -Runtime,使用是需要用到BIRT-runtime文件夹下的ReportEngine文件夹下的内容。

   如果都准备好了,那就开始吧

   先新建一个Java项目,比如JavaBirtExample,添加依赖的jar文件,因为BIRT基于OSGI的,所以org.eclipse.equinox.common这个文件是一定要的,如果要用到图表的话还需要emf对应的文件。

/**
 * 使用BIRT API创建BIRT List。
 * @author 刘尧兴
 * <p>2009-2-18</p>
 */
public class CreateListReport {

	/** Birt runtime 文件路径     */
	public static final String BIRT_HOME = "D:/DeveloperTools/birt-runtime-2_3_1/ReportEngine";
	
	public static void createReport() throws Exception {
		//创建BIRT设计配置文件。
		DesignConfig designConfig = new DesignConfig();
		//指定BIRT Runtime的文件存放地址
		designConfig.setBIRTHome(BIRT_HOME);
		
		IDesignEngine designEngine = null;
		try {
			Platform.startup(designConfig);
			String extensionId = IDesignEngineFactory.EXTENSION_DESIGN_ENGINE_FACTORY;
			IDesignEngineFactory factory = (IDesignEngineFactory)Platform.createFactoryObject(extensionId);
			designEngine = factory.createDesignEngine(designConfig);
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		SessionHandle sessionHandle = designEngine.newSessionHandle(ULocale.CHINESE);
		
		ReportDesignHandle designHandle = sessionHandle.createDesign();
		
		ElementFactory elementFactory = designHandle.getElementFactory();
		
		//创建设计面板
		DesignElementHandle elementHandle = elementFactory.newSimpleMasterPage("Page Master");
		designHandle.getMasterPages().add(elementHandle);
		
		//创建List
		ListHandle listHandle = elementFactory.newList(null);
		listHandle.setWidth("100%");
		designHandle.getBody().add(listHandle);

		//创建标签 
		LabelHandle labelHandle = elementFactory.newLabel(null);
		labelHandle.setText("List's Header Text");
		listHandle.getHeader().add(labelHandle);
		
		//创建标签
		labelHandle = elementFactory.newLabel(null);
		labelHandle.setText("List's Detail Text");
		listHandle.getDetail().add(labelHandle);
		
		//创建标签
		labelHandle = elementFactory.newLabel(null);
		labelHandle.setText("List's Footer Text");
		listHandle.getFooter().add(labelHandle);
		
		//创建样式
		SharedStyleHandle styleHandle = elementFactory.newStyle("MyStyle");
		styleHandle.getBorderTopWidth().setValue(1);
		styleHandle.getBorderBottomWidth().setValue(1);
		styleHandle.getBorderLeftWidth().setValue(1);
		styleHandle.getBorderRightWidth().setValue(1);
		styleHandle.setProperty(IStyleModel.BORDER_TOP_STYLE_PROP, DesignChoiceConstants.LINE_STYLE_GROOVE);
		styleHandle.setProperty(IStyleModel.BORDER_BOTTOM_STYLE_PROP, DesignChoiceConstants.LINE_STYLE_GROOVE);
		styleHandle.setProperty(IStyleModel.BORDER_LEFT_STYLE_PROP, DesignChoiceConstants.LINE_STYLE_GROOVE);
		styleHandle.setProperty(IStyleModel.BORDER_RIGHT_STYLE_PROP, DesignChoiceConstants.LINE_STYLE_GROOVE);
		designHandle.getStyles().add(styleHandle);
		
		File file = new File("c:/temp/ListReport.rptdesign");
		if(!file.exists()) 
			file.createNewFile();
		designHandle.saveAs(file.toString());
		designHandle.close();
		System.out.println("创建成功!");
	}
	
	public static void main(String[] args) {
		try {
			createReport();
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

   执行之后,会在c:/temp目录下面创建一个ListReport.rptdesign文件,预览一下结果为:



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值