XReport - Build Report from DB or XML






1.82/5 (9 votes)
Aug 11, 2006

32665

1046
An article on introduce XReport
Introduction
XReport is an New Report tool ,it can read data from DB or XML Document . use this report tool , people can develop mostly report without programming code . so It 's finally aim is , Do it best to reduce go on errands for report development.
Background (optional)
Use old report tools , peopel have to write many code for report , it bring on long time go on errands , spend many time to write and test code . append code to existent software system , increase project cost . It is time to change this complexion.
Using the code
XRepot write in 100% C# , use DOTNET framework 1.1 , It include Report Designer and Report engine . Report engine can used in ASP.NET or WinForm Application .
In ASP.NET application ,you can use report engine like this :
// // Use XReport engine in ASP.NET // private void Page_Load(object sender, System.EventArgs e) { XDesigner.Report.ReportBuilder myBuilder = this.Session["builder"] as XDesigner.Report.ReportBuilder ; if( myBuilder == null ) { myBuilder = new XDesigner.Report.ReportBuilder(); using( System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection()) { conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\我的程序\Designer\out\demomdb.mdb"; conn.Open(); myBuilder.DBConnection = conn ; // 加载模板 myBuilder.Load( @"D:\我的程序\Designer\out\订单明细.xrp"); // 刷新报表 myBuilder.Refresh(); } this.Session["builder"] = myBuilder ; } XDesigner.Report.ReportHtmlBuilder rb = myBuilder.CreateHtmlBuilder(); rb.Indent = true; string strUrl = this.Request.Url.AbsoluteUri ; int index = strUrl.IndexOf('?'); if( index > 0 ) strUrl = strUrl.Substring( 0 , index ); rb.ImgSrcFormatString = strUrl + "?imageindex={0}"; rb.Refresh(); if( this.Request.QueryString["imageindex"] != null ) { rb.SaveReportImage( Convert.ToInt32( this.Request.QueryString["imageindex"]) , this.Response.OutputStream , System.Drawing.Imaging.ImageFormat.Png ); return ; } if( this.Request.QueryString["out"] == "doc") { // 输出为Word文档 rb.SaveWordDocument( this.Response.OutputStream ); this.Response.AppendHeader("Content-Disposition","attachment;filename=" + System.Web.HttpUtility.UrlEncode( myBuilder.ReportTitle ) + ".doc"); } else if( this.Request.QueryString["out"] == "xls") { // 输出为 Excel 文档 rb.SaveExcelDocument( this.Response.OutputStream ); this.Response.AppendHeader("Content-Disposition","attachment;filename=" + System.Web.HttpUtility.UrlEncode( myBuilder.ReportTitle ) + ".xls"); } else rb.Save( this.Response.Output ); }
In console application , you can use report engine like this
.
/// <summary> /// 应用程序的命令参数 /// </summary> public static System.Collections.Specialized.NameValueCollection myArgs = new System.Collections.Specialized.NameValueCollection(); /// <summary> /// 程序入口点 /// </summary> [STAThread] static void Main() { try { // 从命令行中加载参数 AnalyseArgs( System.Environment.GetCommandLineArgs()); // 从参数文件中加载参数 string strFileName = myArgs["commandfile"]; if( strFileName != null && System.IO.File.Exists( strFileName )) { System.Collections.ArrayList myList = new System.Collections.ArrayList(); using( System.IO.StreamReader myReader = new System.IO.StreamReader( strFileName , System.Text.Encoding.GetEncoding( 936 ))) { string strLine = myReader.ReadLine(); while( strLine != null ) { myList.Add( strLine ); strLine = myReader.ReadLine(); } myReader.Close(); } AnalyseArgs( myList ); } bool bolPrompt = ! ( myArgs["prompt"] == "0"); System.Data.IDbConnection myConn = null; string strConn = myArgs["connection"] ; if( strConn != null && strConn.Length > 0 ) { myConn = new System.Data.OleDb.OleDbConnection( strConn ); myConn.Open(); } XDesigner.Report.ReportBuilder builder = new XDesigner.Report.ReportBuilder(); builder.DBConnection = myConn; foreach( string strKey in myArgs.Keys ) { if( strKey.StartsWith("var:")) builder.SetVariable( strKey.Substring( 4 ) , myArgs[ strKey ] ); } strFileName = myArgs["filename"] ; if( strFileName != null && System.IO.File.Exists( strFileName )) { builder.Load( strFileName ); builder.Refresh(); } if( myConn != null) myConn.Close(); if( myArgs["print"] == "1") { string strPageIndex = myArgs["pageindex"]; if( strPageIndex != null && strPageIndex.Length > 0 ) builder.PrintSpecialPage( bolPrompt , Convert.ToInt32( strPageIndex )); else builder.PrintDocument( bolPrompt ); } } catch( Exception ext) { System.Windows.Forms.MessageBox.Show( null , "系统错误:" + ext.Message , "系统错误" , System.Windows.Forms.MessageBoxButtons.OK , System.Windows.Forms.MessageBoxIcon.Exclamation ); } }//static void Main() private static void AnalyseArgs( System.Collections.IEnumerable args ) { if( args == null ) return ; foreach( string strArg in args ) { if( strArg != null && strArg.Length > 0 ) { int index = strArg.IndexOf('='); if( index > 0 ) { string strName = strArg.Substring( 0 , index ); string strValue = strArg.Substring( index + 1 ); myArgs[ strName.Trim() ] = strValue.Trim(); } } } }
Points of Interest
Learn more information about XReport , please visit http://www.xdesigner.cn/xreport/default-eng.htm or Email yyf9989@hotmail.com .