5.1、使用BIRT API创建List.doc
在使用BIRT API前,得先配置一下BIRT的环境,也就是Birt -Runtime,使用是需要用到BIRT-runtime文件夹下的ReportEngine文件夹下的内容。
/** * 使用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);//创建ListListHandle 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();}}}