注:本文轉(zhuǎn)自 http://blog.csdn.net/xujiaqiang/archive/2008/01/19/2052789.aspx
感謝作者分享。
Birt報表設(shè)計步驟:
1、下載birt all in one 2.2.1.1,包含eclipse,解開下載文件
啟動eclipse,必須使用JDK1.5啟動eclipse,否則新建工程對話框中沒有birt的相關(guān)選項
2、新建report project和report
打開data explorer視圖,新建一個data sources,定義數(shù)據(jù)庫連接driver/url/用戶名/密碼,在"Manage Drivers..."中把jdbc的jar加進(jìn)來
3、新建Report Parameter,設(shè)置參數(shù)名稱、類型,參數(shù)值將來通過url傳遞或使用程序方式賦值
4、新建data sets,設(shè)置Query,即preparedStatement語句,設(shè)置data set的Pameters,即reparedStatement中的“?”,使之和report parameter關(guān)聯(lián); 如果data set的Parameter的值不是來自于Report Parameter,例如來自另外一個data set, 可不設(shè)置關(guān)聯(lián),而在報表內(nèi)容中設(shè)置和其它data set字段的關(guān)聯(lián)(見步驟8)
5、如果data set對應(yīng)的sql是動態(tài)生成的,可以設(shè)置“property Binding”,在Query Text中輸入sql和表達(dá)式,其中可以使用Report Parameter和javascript語句,運行時將執(zhí)行Property Binding中的Query Text,而不執(zhí)行Query中的preparedStatement,但是Query中的preparedStatement在設(shè)計報表時還是有用的,因為可以看到運行結(jié)果的字段列表,只要preparedStatement和Query Text返回相同的字段即可
6、將定義好的data set拖動到設(shè)計界面上,會自動在報表放置一個Table
7、如要對table的某列合計,從Palette視圖中拖一個“Aggregation”到設(shè)計界面
8、如要Master-Sleve報表,則在Table中再放置一個Table。例如一個data set是客戶基本信息,每個客戶一條記錄,另一個data set是客戶交易明細(xì),每個客戶可能有多條記錄,則先將客戶基本信息的data set拖置設(shè)計界面,會自動放置一個Table,然后將客戶交易明細(xì)的data set拖置前一個Table中適當(dāng)位置,即生成一個Table中的Table。選中交易明細(xì)的Table,打開Property Editor視圖,在binding欄中,選擇交易明細(xì)的data set,按“Dataset Parameter Binding...”,選擇“CUST_ID"字段,將其和客戶基本信息data set的CUST_ID關(guān)聯(lián)起來(row["CUST_ID"])
9、按需要調(diào)整報表的樣式,從eclipse工具欄中選擇“view report in web viewer”,如果設(shè)置了Report Parameter,會出現(xiàn)一個對話框,要求輸入Report Parameter的值,按確定后生成報表內(nèi)容
10、在web viewer中,鼠標(biāo)右建點按生成的報表,選“屬性”,獲取url,開一個空白IE,復(fù)制url到地址欄,在url最后用“&參數(shù)名=參數(shù)值”方式,把Report Parameter加進(jìn)url,按回車,直接出現(xiàn)報表結(jié)果,不再出現(xiàn)報表參數(shù)輸入對話框。還可以pdf/doc/html等格式預(yù)覽生成的報表
Birt報表部署步驟:
部署設(shè)計好的報表很簡單,只需要下載Birt runtime,將Birt runtime部署為一個web應(yīng)用,報表設(shè)計文件復(fù)制到該應(yīng)用的根目錄即可。為方便起見,以下將Birt runtime部屬為一個獨立的應(yīng)用,當(dāng)然也可以將Birt runtime整合到你的web應(yīng)用中
1、下載birt runtime 2.2.1.1,解開下載文件
2、將birt rumtime解開后的目錄下的部署為一個web應(yīng)用,使用JDK1.4.2或以上版本
3、將報表設(shè)計步驟中生成的設(shè)計文件(*.retdesing文件)復(fù)制到web應(yīng)用所在目錄
4、確保以下文件在WEB-INF/lib目錄下存在,如果沒有,從all in one中搜索復(fù)制過來
com.ibm.icu_3.6.1.v20070906.jar
coreapi.jar
engineapi.jar
scriptapi.jar
5、將jdbc的jar或zip文件復(fù)制到WEB-INF\platform\plugins\org.eclipse.birt.report.data.oda.jdbc_2.2.1.r22x_v20070919\drivers目錄下
6、刪除WEB-INF\platform\org.eclipse.birt.report.data.oda.sampledb_2.2.1.r22x_v20070919目錄(示例數(shù)據(jù)庫,沒什么用處),否則生成報表時有不必要的警告信息出現(xiàn)
7、啟動web server,修改設(shè)計步驟第10步獲得的url,使IP/端口/web應(yīng)用名稱符合你的Birt runtime部署,在IE中可以查看報表內(nèi)容
使用Birt提供API以程序方式生成報表結(jié)果,例如生成PDF格式的報表結(jié)果,代碼片段如下:
- import java.util.HashMap;
- import java.util.logging.Level;
-
- import java.io.OutputStream;
- import java.io.FileOutputStream;
- import java.io.ByteArrayOutputStream;
-
- import org.eclipse.<SPAN class=hilite1>birt</SPAN>.core.framework.Platform;
- import org.eclipse.<SPAN class=hilite1>birt</SPAN>.report.engine.api.EngineConfig;
- import org.eclipse.<SPAN class=hilite1>birt</SPAN>.report.engine.api.EngineConstants;
- import org.eclipse.<SPAN class=hilite1>birt</SPAN>.report.engine.api.EngineException;
- import org.eclipse.<SPAN class=hilite1>birt</SPAN>.report.engine.api.IReportEngine;
- import org.eclipse.<SPAN class=hilite1>birt</SPAN>.report.engine.api.IReportEngineFactory;
- import org.eclipse.<SPAN class=hilite1>birt</SPAN>.report.engine.api.IReportRunnable;
- import org.eclipse.<SPAN class=hilite1>birt</SPAN>.report.engine.api.IRunAndRenderTask;
- import org.eclipse.<SPAN class=hilite1>birt</SPAN>.report.engine.api.<SPAN class=hilite2>PDF</SPAN>RenderOption;
-
- public class <SPAN class=hilite2>PDF</SPAN>ReportServiceAccess
- {
-
- protected static boolean initStatus = false;
-
- private static IReportEngine engine = null;
-
- private static EngineConfig config = null;
-
- private static IReportRunnable design = null;
-
- private static <SPAN class=hilite2>PDF</SPAN>RenderOption ro = null;
-
-
- public void initilize()
- {
- if ( initStatus == true )
- return;
-
- try
- {
- config = new EngineConfig();
- config.setEngineHome( "C:/projects/<SPAN class=hilite1>birt</SPAN>/WEB-INF/platform" );
- config.setLogConfig( "C:/projects/<SPAN class=hilite1>birt</SPAN>/logs", Level.FINE );
-
- Platform.startup( config );
- IReportEngineFactory factory = ( IReportEngineFactory ) Platform
- .createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
- engine = factory.createReportEngine( config );
- engine.changeLogLevel( Level.WARNING );
-
- ro = new <SPAN class=hilite2>PDF</SPAN>RenderOption();
-
- config.getEmitterConfigs().put( "<SPAN class=hilite2>pdf</SPAN>", ro );
-
- initStatus = true;
-
- }
- catch ( Exception ex )
- {
- ex.printStackTrace();
- initStatus = false;
- }
- }
-
-
- public void release()
- {
- engine.shutdown();
- Platform.shutdown();
-
- initStatus = false;
- }
-
- protected OutputStream run( String filename, HashMap parameters ) throws EngineException
- {
- design = engine.openReportDesign( filename );
-
-
- IRunAndRenderTask task = engine.createRunAndRenderTask( design );
- HashMap contextMap = new HashMap();
- contextMap.put( EngineConstants.APPCONTEXT_<SPAN class=hilite2>PDF</SPAN>_RENDER_CONTEXT, ro );
- task.setAppContext( contextMap );
- task.setParameterValues( parameters );
- task.validateParameters();
-
- OutputStream os = new ByteArrayOutputStream();
- ro.setOutputStream( os );
- ro.setOutputFormat( "<SPAN class=hilite2>pdf</SPAN>" );
- task.setRenderOption( ro );
-
- task.run();
- task.close();
-
- return os;
- }
-
-
-
-
-
-
-
-
-
-
- public OutputStream call( String filename, HashMap parameters ) throws EngineException
- {
- initilize();
- OutputStream os = run( filename, parameters );
- release();
-
- return os;
- }
-
-
-
-
- public static void main( String[] args )
- {
- HashMap parameters = new HashMap();
-
- parameters.put( "begindate", "2004/01/01" );
- parameters.put( "enddate", "2007/12/31" );
- parameters.put( "sql", " where cust_id = 1234567" );
-
- ByteArrayOutputStream bos = null;
-
- <SPAN class=hilite2>PDF</SPAN>ReportServiceAccess ebr = new <SPAN class=hilite2>PDF</SPAN>ReportServiceAccess();
-
- String filename = "C:/projects/<SPAN class=hilite1>birt</SPAN>/batch_report.rptdesign";
- try
- {
- bos = ( ByteArrayOutputStream ) ebr.call( filename, parameters );
- OutputStream fis = new FileOutputStream( "c:/test.<SPAN class=hilite2>pdf</SPAN>" );
- bos.writeTo( fis );
- }
- catch ( Exception e )
- {
- e.printStackTrace();
- }
- }
-
- }