5.3、运用BIRT API创建Table

5.3、使用BIRT API创建Table

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

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

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

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

	public static final String BIRT_HOME = "D:/DeveloperTools/birt-runtime-2_3_1/ReportEngine";
	
	public static void createReport() throws Exception {
		DesignConfig designConfig = new DesignConfig();
		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);
		
		TableHandle tableHandle = elementFactory.newTableItem(null, 3, 1, 1, 0);
		tableHandle.setWidth("100%");
		designHandle.getBody().add(tableHandle);
		
		List tableHeaders = tableHandle.getHeader().getContents();
		RowHandle headerRowHandle = (RowHandle) tableHeaders.get(0);
		
		CellHandle headerCellHandle = (CellHandle)headerRowHandle.getCells().get(0);
		LabelHandle labelHandle = elementFactory.newLabel(null);
		labelHandle.setText("姓名");
		headerCellHandle.getContent().add(labelHandle);
		
		headerCellHandle = (CellHandle)headerRowHandle.getCells().get(1);
		labelHandle = elementFactory.newLabel(null);
		labelHandle.setText("电话");
		headerCellHandle.getContent().add(labelHandle);
		
		headerCellHandle = (CellHandle)headerRowHandle.getCells().get(2);
		labelHandle = elementFactory.newLabel(null);
		labelHandle.setText("邮编");
		headerCellHandle.getContent().add(labelHandle);
		
		List tableDetails = tableHandle.getDetail().getContents();
		RowHandle detailRowHandle = (RowHandle) tableDetails.get(0);
		CellHandle detailCellHandle = (CellHandle)detailRowHandle.getCells().get(0);
	   labelHandle = elementFactory.newLabel(null);
		labelHandle.setText("张三");
		detailCellHandle.getContent().add(labelHandle);
		
		detailCellHandle = (CellHandle)detailRowHandle.getCells().get(1);
		labelHandle = elementFactory.newLabel(null);
		labelHandle.setText("39796666");
		detailCellHandle.getContent().add(labelHandle);
		
		detailCellHandle = (CellHandle)detailRowHandle.getCells().get(2);
		labelHandle = elementFactory.newLabel(null);
		labelHandle.setText("343206");
		detailCellHandle.getContent().add(labelHandle);
		

		SharedStyleHandle styleHandle = elementFactory.newStyle("MyStyle");
		styleHandle.getBorderTopWidth().setValue(1);
		styleHandle.getBorderBottomWidth().setValue(1);
		styleHandle.getBorderLeftWidth().setValue(1);
		styleHandle.getBorderRightWidth().setValue(1);
		styleHandle.setTextAlign(DesignChoiceConstants.TEXT_ALIGN_CENTER);
		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);
		
		for(int i = 0;i<tableHandle.getColumnCount();i++) {
			ColumnHandle colHandle = (ColumnHandle)tableHandle.getColumns().get(i);
			colHandle.setStyle(styleHandle);
		}
		
		File file = new File("c:/temp/TableReport.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目录下面创建一个TableReport.rptdesign文件,预览一下结果为:


5.3、运用BIRT API创建Table