在使用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文件,预览一下结果为: