发表时间:2009-02-18
最后修改:2009-02-18
在使用BIRT API前,得先配置一下BIRT的环境,也就是Birt -Runtime,使用是需要用到BIRT-runtime文件夹下的ReportEngine文件夹下的内容。
如果都准备好了,那就开始吧
先新建一个Java项目,比如JavaBirtExample,添加依赖的jar文件,因为BIRT基于OSGI的,所以org.eclipse.equinox.common这个文件是一定要的,如果要用到图表的话还需要emf对应的文件。
/**
* 使用BIRT API创建BIRT Grid 。
* @author 刘尧兴
* <p>2009-2-18</p>
*/
public class CreateGridReport {
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);
GridHandle gridHandle = elementFactory.newGridItem(null, 2, 1);
gridHandle.setWidth("100%");
designHandle.getBody().add(gridHandle);
RowHandle rowHandle = (RowHandle)gridHandle.getRows().get(0);
CellHandle cellHandle = (CellHandle)rowHandle.getCells().get(0);
LabelHandle labelHandle = elementFactory.newLabel(null);
labelHandle.setText("第一列,第一行");
cellHandle.getContent().add(labelHandle);
cellHandle = (CellHandle)rowHandle.getCells().get(1);
labelHandle = elementFactory.newLabel("第二列,第一行");
labelHandle.setText("第二列,第一行");
cellHandle.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.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<gridHandle.getColumnCount();i++) {
ColumnHandle colHandle = (ColumnHandle)gridHandle.getColumns().get(i);
colHandle.setStyle(styleHandle);
}
File file = new File("c:/temp/GridReport.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目录下面创建一个GridReport.rptdesign文件,预览一下结果为: