12.1 報表的工作模式以及相應的部署和配置
雖然爲了示例的需要,在前文的第二章講過BIRT的配置,以及在文本類報表,網格報表,圖表中用過birt jsp tag,也做過一些說明。但報表的工作模式,不僅僅包含這兩種。實際上,BIRT有五種工作模式,
第一種是用jsp或者javascript訪問url,這個url遵循特定的格式,例如url/frameset?__report=&__paraname=paravalue;
第二種方式是在jsp頁面中嵌入birt tag;
第三種模式是在servlet或者java class中打開用報表設計器設計的報表,導出一個html,pdf或者其它格式的文檔的輸出到既定的目錄;
第四種是擴展報表展示器birt web viewer,也即是改寫birt web viewer的servlet,配置訪問路徑,並用它來展示先前在報表設計器中設計的報表;
第五種模式即是在java class中利用報表設計API,生成報表設計文檔,並用報表引擎API打開並生成html,pdf或者其它格式的文檔。
本章先詳細說明前文用到的兩種工作模式,後面的三種,我們會在第十三章及之後的章節詳細描述。
第一種工作模式:url
Url的一個例子如下:
http://localhost:8080/birt/frameset?__report=test.rptdesign&__bookmark=bk&__isreportlet=true
說明:
http://localhost:8080:代表訪問的服務器地址
/birt :代表訪問的app的根目錄
/frameset: 代表訪問的servlet的模式,在web.xml中有配置的說明
__report=test.rptdesign&__bookmark=bk&__isreportlet=true 代表訪問的參數,其中birt保留的參數以兩個下劃線開頭,例如__report指定報表的名稱;__bookmark指定報表的書籤;報表參數不需要以__開頭。
報表訪問servlet的模式,主要有以下幾種:
frameset ---- 採用Ajax框架,可以顯示工具條,導航條和TOC面板,實現複雜的操作,如分頁處理,導出數據,導出報表,打印等。該模式下會自動生成report document文件(預覽report design文件)到特定的目錄(用戶可以用參數指定,也可以定義在web.xml裏)。採用Ajax,速度較慢。
run ---- 也採用Ajax框架,但不實現frameset的複雜功能,不會生成臨時的report document文件(預覽report design文件),也不支持分頁,這個主要是應用在BIRT Designer裏的preview tab裏,可以支持cancel操作,其它不怎麼常用。採用Ajax,速度較慢。
preview --- 沒有用到Ajax框架,直接調用底層Engine API對報表進行render,把生成的報表內容直接輸出到瀏覽器。這種模式和run模式調用的是相同的Engine API,唯一區別在於run採用Ajax獲取報表內容,而preview直接輸出到瀏覽器。如果要支持分頁,用戶需要在URL上定義__page和__pagerange參數。需要特別說明的是,在這幾種預覽模式中,preview的速度是最快的。
download --- 用於導出報表數據,當你使用frameset工具條裏的導出數據功能時,會用到這個模式。
這個模式會直接調用瀏覽器的下載功能彈出下載框或者調用默認的下載工具打開下載目錄地址指定輸入框。
output --- 該模式類似於frameset,會自動生成report document文件(預覽report design文件),區別在於output不採用Ajax,而是將生成的報表內容直接輸出到瀏覽器。不支持分頁。
下面說說最主要的保留參數的含義:
“__report” 定義要預覽的rptdesign文件路徑,支持相對路徑和絕對路徑,相對路徑是相對於web.xml中定義的工作目錄。
“__document” 定義要預覽的rptdocument文件路徑,同樣支持相對和絕對路徑。相對路徑是相對於web.xml中定義的工作目錄。在同時定義了__report和__document參數時,以__document爲優先,如未找到相應的document文件,纔會從design文件生成document文件(frameset/output)或是直接去render這個design文件(preview/run)。
“__title” 定義報表顯示的標題。
“__showtitle” 是否顯示frameset模式下上方的標題部分。true | false
“__toolbar” 是否顯示frameset模式下的工具條。true | false
“__navigationbar” 是否顯示frameset模式下的導航條。true | false
“__parameterpage” 是否強制彈出或不彈出報表參數對話框。true | false
“__format” 輸出報表的格式,默認爲html。現在支持:pdf | doc | xls | postscript | ppt
“__locale” 設置Locale信息,如 __locale=zh_CN, 注意必須是國家加語言。
“__bookmark” 置頁面要定位的書籤名字。
“__page” 指定要輸出的報表頁數,這個依賴於報表的分頁設計(page break)。
“__pagerange” 指定要輸出的報表頁數範圍。如1,3,5-9。
“__clean” BIRT裏臨時生成的一些文件都是和session相關的,比如臨時document文件,還有image文件。這些文件也可以通過session進行管理,這個參數就是指定是否需要在session timeout的時候清除這些臨時文件。默認值是true。
Document文件夾用於緩存frameset的文檔,session退了之後由系統自動刪除,也可由用戶刪除文件夾,對程序不造成影響。
Logs文件夾用於生成系統出現錯誤時的日誌文件。可以刪除。
Report文件夾用於放置rptdesign文件,也可以放置在根目錄,由url指定
Scriptlib文件夾用於放置用戶自定義的腳本,也可以放置在根目錄,由報表配置
WEB-INF文件夾用於配置報表運行時環境的各個參數,第二章已經有非常詳細的描述,這兒強調一下JNDI的配置
1.tomcat配置jndi連接池
在 {tomcat目錄}\webapps\{項目名}\WEB-INF 中新增context.xml文件,配置代碼如下
- <?xml version="1.0" encoding="UTF-8"?><Context> <Resource auth="Container" name="jdbc/travel_agency" type="javax.sql.DataSource" maxIdle="5" maxWait="-1" driverClassName="com.mysql.jdbc.Driver" username="itravel" password="709394" url="jdbc:mysql://192.168.1.100/travel_agency?useUnicode=true&characterEncoding=UTF-8" maxActive="10"/></Context>
2.在web.xml中加入配置
- <resource-ref> <description>Database Source</description> <res-ref-name>jdbc/travel_agency</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth></resource-ref>
如果weblogic已經配置了連接池(jdbc/myjndi),那麼只需要在\{應用名}\WEB-INF 下新建context.xml,用於配置報表訪問JNDI,內容如下:
- <resource-ref>
- <description>Database Source</description>
- <res-ref-name>jdbc/myjndi</res-ref-name>
- <res-type>javax.sql.DataSource</res-type>
- <res-auth>Container</res-auth>
- </resource-ref>
第二種工作模式jsp tag:
JSP birt tag的一個例子在第五章
- <birt:viewer id="birtViewer1"
- reportDesign="orders.rptdesign"
- baseURL="<%= request.getContextPath( )%>"
- pattern="run"
- height="367"
- width="645"
- format="html"
- frameborder="false"
- isHostPage="false"
- isReportlet="true"
- showParameterPage="false"
- >
- </birt:viewer>
在\webapps\WebViewerExample\WEB-INF\tlds\birt.tld中有詳細的定義。內容的一小部分如下文所示:
- <name>viewer</name>
- <tag-class>org.eclipse.birt.report.taglib.ViewerTag</tag-class>
- <body-content>JSP</body-content>
- <description>
- The viewer tag is to specify how to import and control BIRT Report Viewer into JSP page.
- This tag will use Ajax framework to retrieve report content.
- </description>
- <attribute>
- <name>id</name>
- <required>true</required>
- <rtexprvalue>true</rtexprvalue>
- <description>
- Specifies the current viewer id. It should be unique.
- It can contain number,letter or underline.
- </description>
- </attribute>
- <attribute>
- <name>pattern</name>
- <required>false</required>
- <rtexprvalue>true</rtexprvalue>
- <description>
- Specifies which servlet pattern to be used to preview report.
- Default value is frameset.
- ( frameset or run )
- </description>
- </attribute>
在此說明如下:
birt標籤分爲: viewer, report, param,parameterPage,paramDef這四個標籤。
一、viewer標籤
示例:
<birt:viewer id="1"
pattern="run"
reportDesign="test1.rptdesign"
position="absolute"
width="800"
height="500"
left="50"
top="50"
format="html"
svg="false">
<birt:param name="sample" value="abc"/>
<birt:param name="Cust" value="<%=110%>"/>
</birt:viewer>
id: 必須指定,
pattern: 不一定 要指定,指定運行使用的servlet, 默認爲frameset
reportDesign: 指定報表文件名
title : 可選,指定報表的標題,其實這不是設計報表的標題,而是報表運行時顯示在報表上面的標題。
isHostPage: 可選,默認爲false,指定報表是否全屏顯示,此屬性設置,則width,height,scrolling 屬性作廢。
scrolling :可選,是否出現滾動條
position: 可選,報表相對於頁面的位置,值爲static | absolute | fixed | relative,如果設置了isHostPage,該屬性作廢。
style : 可選,報表顯示的樣式,沒多說的。,如果設置了isHostPage,該屬性作廢
width,heigth,top,left: 分別設置報表顯示的寬,高,上邊距,左邊距
frameborder :可選,設置報表所在的iframe是否顯示邊框,默認不顯示。值爲yes/no
reportDocument : 可選,指定報表文檔,只支持file:url,這樣的格式。
reportlet : 可選,使用reportDocument ,可指定這個屬性,只是指定reportlet 實例的id, 另frameset不支持此屬性。
bookmark : 可選,指定目標書籤名稱
locale : 指定Locale信息,指定語言環境
svg : 可選,指定是否支持輸出svg格式文檔。
format : 可選,指定報表的輸出格式
pageOverflow : 可選,當輸出格式爲pdf時,指定頁面溢出樣式,0 表示自動,1表示實際大小,2表示適合頁面的大小
pageNum: 可選,指定是否顯示頁碼
pageRange: 指定頁面範圍。當運行文檔文件時指定這個屬性。
resourceFolder : 指定相關的資源文件,默認識別web.xml定義的資源文件夾。
showTitle: 是否顯示報表標題,默認爲true
showToolBar: 是否顯示工具條,默認爲true
showNavigationBar: 是否顯示導航條,默認爲true,但是如果allowPageBreak設置爲false,該屬性作廢。
showParameterPage: 是否顯示參數對話框。默認爲true,
二、report標籤
和view標籤差不多,但是report沒有使用ajax支持,而是直接將內容顯示在頁面。 內部應該使用了preview。
另有一個特別的屬性reportContainer,有兩個可選值,iframe,和div,默認爲iframe. 指定使用report的容器。
其他屬性和view都差不多。
<birt:report id="2"
reportDesign="test.rptdesign"
position="absolute"
width="400"
height="300"
left="900"
top="100"
format="pdf"
reportContainer="iframe">
<birt:param name="sample" value="abc"/>
</birt:report>
三、param
報表參數相關的標籤。
name: 指定報表參數名稱
value; 參數值
pattern: 參數格式
displayText: 顯示文字
isLocale: 指定參數是否本地化字符
四、parameterPage
配置參數頁面。
id : 必選。指定參數頁面的id
name : 指定參數頁面name,如果isCustom設置爲true,則此屬性一定要寫,且唯一。
isCustom : 指定是否使用用戶自定義參數頁面。默認爲false;
target : 指定請求打開窗口的位置。
其他屬性與view相似,給個例子:
<birt:parameterPage id="parameterPage"
pattern="run"
reportDesign="test.rptdesign"
target="myiframe"
width="600"
height="400"
format="html"/>
<iframe name="myiframe" width="600" height="400"></iframe>
五、paramDef
這個標籤用來生成參數參數頁面定義的參數的html代碼。
有以下幾個屬性
id,name,value,pattern,displayText,isLocale,title,style,cssClass.
<birt:parameterPage
id="report2"
name="page2"
reportDesign="test.rptdesign"
isCustom="true" //指定自定義頁面
pattern="frameset">
id: <birt:paramDef id="id" name="id" title="這是設置id的區域"/>
<br><br>
sex: <birt:paramDef id="sex" name="sex"/>
<br><br>
<input type="submit" name="submit" value="Sumbit form"/>
<br><br>
</birt:parameterPage>
假如你的text報表裏有兩個參數,一個參數爲id,另一個參數爲sex,這時候假如id是一列值,sex是checkbox.
那麼這個參數頁面就會把這個id要選擇的值顯示出來,把sex這個checkbox也顯示出來。
第三種工作模式——在servlet或者java class中打開用報表設計器設計的報表,導出一個html,pdf或者其它格式的文檔的輸出到既定的目錄;需要新建JAVA project或者java web project,導入report engine庫,也就是birt-runtime-3_7_2\ReportEngine\lib目錄下的包,如下:
爲了便於大家理解,我把啓動平臺,打開報表,關閉平臺分別放置在一個類的三個方法中,並做了相應的註釋:
- package birt;
- import org.eclipse.birt.core.framework.Platform;
- import org.eclipse.birt.report.engine.api.EngineConfig;
- import org.eclipse.birt.report.engine.api.HTMLRenderOption;
- import org.eclipse.birt.report.engine.api.IReportEngine;
- import org.eclipse.birt.report.engine.api.IReportEngineFactory;
- import org.eclipse.birt.report.engine.api.IReportRunnable;
- import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
- public class HtmlReport
- {
- IReportEngine engine=null;
- EngineConfig config = null;
- HtmlReport ()
- {
- }
- //用於運行報表
- public void runReport()
- {
- try{
- IReportRunnable design = null;
- //Open the report design
- design = engine.openReportDesign("d:\\CustomerOrdersFinal.rptdesign");
- //Create task to run and render the report,
- IRunAndRenderTask task = engine.createRunAndRenderTask(design);
- HTMLRenderOption options = new HTMLRenderOption();
- options.setOutputFileName("D:\\TEST.html");
- options.setOutputFormat("html");
- options.setEmbeddable(true);
- task.setRenderOption(options);
- task.run();
- task.close();
- }catch( Exception ex){
- ex.printStackTrace();
- }
- }
- //用於啓動報表平臺
- public void startPlatform(){
- try{
- config = new EngineConfig( );
- config.setBIRTHome("");//低版本的需要在此指定BIRT engine home地址
- Platform.startup( config );
- IReportEngineFactory factory = (IReportEngineFactory) Platform
- .createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
- engine = factory.createReportEngine( config );
- }catch( Exception e){
- e.printStackTrace();
- }
- }
- //用於停止報表平臺
- public void stopPlatform(){
- engine.destroy();
- Platform.shutdown();
- }
- public static void main (String[] args)
- {
- HtmlReport html = new HtmlReport ();
- html.startPlatform();
- System.out.println("Started");
- html.runReport();
- html.stopPlatform();
- System.out.println("Finished");
- }
- }
其中已經設計好的報表佈局如下:
運行報表,我們會在D盤下看到生成的TEST.html文件,打開內容如下:
第四種工作模式,擴展birt web viewer:
首先我們需要改寫birt engine,內容如下:
- package birt;
- import java.util.logging.Level;
- import javax.servlet.ServletContext;
- import org.eclipse.birt.core.exception.BirtException;
- import org.eclipse.birt.core.framework.IPlatformContext;
- import org.eclipse.birt.core.framework.Platform;
- import org.eclipse.birt.core.framework.PlatformServletContext;
- import org.eclipse.birt.report.engine.api.EngineConfig;
- import org.eclipse.birt.report.engine.api.HTMLActionHandler;
- import org.eclipse.birt.report.engine.api.HTMLEmitterConfig;
- import org.eclipse.birt.report.engine.api.HTMLServerImageHandler;
- import org.eclipse.birt.report.engine.api.IReportEngine;
- import org.eclipse.birt.report.engine.api.IReportEngineFactory;
- public class BirtEngine {
- private static IReportEngine birtEngine = null;
- public static synchronized void initBirtConfig() {
- }
- public static synchronized IReportEngine getBirtEngine(ServletContext sc) {
- EngineConfig config = new EngineConfig();
- config.setLogConfig(sc.getRealPath("\\logs"), Level.FINE);
- config.setEngineHome("");
- IPlatformContext context = new PlatformServletContext(sc);
- config.setPlatformContext(context);
- //Register new image handler 解決了讀取圖片時的路徑問題
- HTMLEmitterConfig emitterConfig = new HTMLEmitterConfig( );
- emitterConfig.setActionHandler( new HTMLActionHandler( ) );
- HTMLServerImageHandler imageHandler = new HTMLServerImageHandler( );
- emitterConfig.setImageHandler( imageHandler );
- config.getEmitterConfigs( ).put( "html", emitterConfig ); //-NLS-1$
- try {
- Platform.startup(config);
- } catch (BirtException e) {
- e.printStackTrace();
- }
- IReportEngineFactory factory = (IReportEngineFactory) Platform
- .createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
- birtEngine = factory.createReportEngine(config);
- return birtEngine;
- }
- public static synchronized void destroyBirtEngine() {
- if (birtEngine == null) {
- return;
- }
- birtEngine.shutdown();
- Platform.shutdown();
- birtEngine = null;
- }
- public Object clone() throws CloneNotSupportedException {
- throw new CloneNotSupportedException();
- }
- }
然後我們在servlet中調用這個自定義的birt engine;
- package birt;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- import java.util.logging.Logger;
- import javax.servlet.ServletContext;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.eclipse.birt.report.engine.api.EngineConstants;
- import org.eclipse.birt.report.engine.api.HTMLRenderContext;
- import org.eclipse.birt.report.engine.api.HTMLRenderOption;
- import org.eclipse.birt.report.engine.api.IGetParameterDefinitionTask;
- import org.eclipse.birt.report.engine.api.IParameterDefnBase;
- import org.eclipse.birt.report.engine.api.IReportEngine;
- import org.eclipse.birt.report.engine.api.IReportRunnable;
- import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
- import org.eclipse.birt.report.engine.api.IScalarParameterDefn;
- public class birtServlet extends HttpServlet {
- private static final long serialVersionUID = 1L;
- private IReportEngine birtReportEngine = null;
- protected static Logger logger = Logger.getLogger("org.eclipse.birt");
- public birtServlet () {
- super();
- }
- public void destroy() {
- super.destroy();
- BirtEngine.destroyBirtEngine();
- }
- public void doGet(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- HashMap parmDetails = new HashMap();
- // get report name and launch the engine
- resp.setContentType("text/html");
- // resp.setContentType( "application/pdf" );
- // resp.setHeader ("Content-Disposition","inline; filename=test.pdf");
- //得到參數並放入paramsList集合
- String reportName = req.getParameter("ReportName");
- String bm = new String(req.getParameter("bm").getBytes("ISO8859_1"),"GB2312");
- String sj1 = req.getParameter("sj1");
- String sj2 = req.getParameter("sj2");
- List paramsList = new ArrayList();
- if (bm != null && !bm.equals("") && !bm.equals("null"))
- paramsList.add(bm);
- paramsList.add(sj1);
- paramsList.add(sj2);
- ServletContext sc = req.getSession().getServletContext();
- this.birtReportEngine = BirtEngine.getBirtEngine(sc);
- // 設置圖片地址
- HTMLRenderContext renderContext = new HTMLRenderContext();
- renderContext.setBaseImageURL(req.getContextPath() + "\\report\\images");
- renderContext.setImageDirectory(sc.getRealPath("\\report\\images"));
- //logger.log(Level.FINE, "image directory "
- // + sc.getRealPath("\\report\\images"));
- //System.out.println(renderContext.getBaseImageURL());
- //System.out.println(renderContext.getImageDirectory());
- HashMap<String, HTMLRenderContext> contextMap = new HashMap<String, HTMLRenderContext>();
- contextMap.put(EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT,
- renderContext);
- IReportRunnable design;
- try {
- // Open report design
- design = birtReportEngine.openReportDesign(sc
- .getRealPath("")
- + "\\" + reportName);
- //以下是將報表裏的參數和傳遞進來的參數匹配然後放到parmDetails集合中
- IGetParameterDefinitionTask taskParam = birtReportEngine.createGetParameterDefinitionTask( design );
- Collection params = taskParam.getParameterDefns( true );
- Iterator iter = params.iterator();
- Iterator iterList = paramsList.iterator();
- // Iterate over all parameters
- while ( iter.hasNext() && iterList.hasNext() )
- {
- IParameterDefnBase param = (IParameterDefnBase) iter.next( );
- // Parameters are not in a group
- IScalarParameterDefn scalar = (IScalarParameterDefn) param;
- if(scalar.getName().equals("bm")){
- String paramList = (String)iterList.next();
- parmDetails.put( scalar.getName(),paramList);
- //System.out.println(scalar.getName() + " " + paramList);
- }else{
- Date paramList = new Date();
- parmDetails.put( scalar.getName(),paramList);
- //System.out.println(scalar.getName() + " " + paramList);
- }
- }
- taskParam.close();
- //
- // create task to run and render report
- IRunAndRenderTask task = birtReportEngine
- .createRunAndRenderTask(design);
- task.setAppContext(contextMap);
- task.setParameterValues(parmDetails);
- // set output options
- HTMLRenderOption options = new HTMLRenderOption();
- options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_HTML);
- // options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_PDF);
- options.setOutputStream(resp.getOutputStream());
- task.setRenderOption(options);
- // 運行報表
- task.run();
- task.close();
- } catch (Exception e) {
- e.printStackTrace();
- throw new ServletException(e);
- }
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- }
- public void init() throws ServletException {
- }
- }
最後,配置birt engine的訪問地址,在web.xml中配置如下:
- <servlet>
- <servlet-name>birtServlet </servlet-name>
- <servlet-class>birt.birtServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>birtServlet </servlet-name>
- <url-pattern>/myBirtFrame</url-pattern>
- </servlet-mapping>
這樣,我們可以以這樣的方式訪問自己的birt viewer:
http://localhost:8080/birt/myBirtFrame?ReportName=CustomerOrdersFinal.rptdesign
注意:如果是在linux或者unix環境運行,則以上地址的”\\”都必須改成”/”,windows和linux的區別,大家都懂的。
以上參數的訪問方式也是需要完善的,後面的章節會詳細講解如何擴展birt web viewer。
第五種方式,運用報表設計API產生報表,運行並輸出報表。
下面是一個簡單的案例
- package birt;
- import java.io.IOException;
- import org.eclipse.birt.core.framework.Platform;
- import org.eclipse.birt.report.model.api.CellHandle;
- import org.eclipse.birt.report.model.api.DesignConfig;
- import org.eclipse.birt.report.model.api.DesignElementHandle;
- import org.eclipse.birt.report.model.api.ElementFactory;
- import org.eclipse.birt.report.model.api.GridHandle;
- import org.eclipse.birt.report.model.api.IDesignEngine;
- import org.eclipse.birt.report.model.api.IDesignEngineFactory;
- import org.eclipse.birt.report.model.api.ImageHandle;
- import org.eclipse.birt.report.model.api.LabelHandle;
- import org.eclipse.birt.report.model.api.ReportDesignHandle;
- import org.eclipse.birt.report.model.api.RowHandle;
- import org.eclipse.birt.report.model.api.SessionHandle;
- import org.eclipse.birt.report.model.api.activity.SemanticException;
- import com.ibm.icu.util.ULocale;
- public class SimpleCreate {
- public static void main( String[] args )
- {
- try
- {
- buildReport( );
- }
- catch( IOException e )
- {
- e.printStackTrace();
- }
- catch( SemanticException e )
- {
- e.printStackTrace();
- }
- }
- // This method shows how to build a very simple BIRT report with a
- // minimal set of content: a simple grid with an image and a label.
- static void buildReport() throws IOException, SemanticException
- {
- // Create a session handle. This is used to manage all open designs.
- // Your app need create the session only once.
- //Configure the Engine and start the Platform
- DesignConfig config = new DesignConfig( );
- config.setProperty("BIRT_HOME", "E:\\birt漢化包\\birt-runtime-3_7_2\\ReportEngine");
- IDesignEngine engine = null;
- try
- {
- Platform.startup( config );
- IDesignEngineFactory factory = (IDesignEngineFactory) Platform.createFactoryObject( IDesignEngineFactory.EXTENSION_DESIGN_ENGINE_FACTORY );
- engine = factory.createDesignEngine( config );
- }
- catch( Exception ex )
- {
- ex.printStackTrace();
- }
- SessionHandle session = engine.newSessionHandle( ULocale.ENGLISH ) ;
- // Create a new report design.
- ReportDesignHandle design = session.createDesign( );
- // The element factory creates instances of the various BIRT elements.
- ElementFactory efactory = design.getElementFactory( );
- // Create a simple master page that describes how the report will appear when printed.
- //
- // Note: The report will fail to load in the BIRT designer unless you create a master page.
- DesignElementHandle element = efactory.newSimpleMasterPage( "Page Master" );
- design.getMasterPages( ).add( element );
- // Create a grid and add it to the "body" slot of the report design.
- GridHandle grid = efactory.newGridItem( null, 2 /* cols */, 1 /* row */ );
- design.getBody( ).add( grid );
- // Note: Set the table width to 100% to prevent the label
- // from appearing too narrow in the layout view.
- grid.setWidth( "100%" );
- // Get the first row.
- RowHandle row = (RowHandle) grid.getRows( ).get( 0 );
- // Create an image and add it to the first cell.
- ImageHandle image = efactory.newImage( null );
- CellHandle cell = (CellHandle) row.getCells( ).get( 0 );
- cell.getContent( ).add( image );
- image.setURL( "\"https://img-my.csdn.net/uploads/201301/09/1357690895_3463.png\"" );
- // Create a label and add it to the second cell.
- LabelHandle label = efactory.newLabel( null );
- cell = (CellHandle) row.getCells( ).get( 1 );
- cell.getContent( ).add( label );
- label.setText( "Hello, world!" );
- // Save the design and close it.
- design.saveAs( "d:\\sample.rptdesign" );
- design.close( );
- System.out.println("Finished");
- // We're done!
- }
- }
運行,我們即可在D盤看到sample.rptdesign
在設計器中預覽這個報表,我們看到:
打開並展示報表見第三種報表運行模式,後面章節會更詳細的講解如何運用報表API生成報表設計文件,並運行產生輸出。