Click here to Skip to main content
Licence 
First Posted 10 Aug 2006
Views 18,596
Bookmarked 20 times

XReport - Build Report from DB or XML

By | 10 Aug 2006 | Article
An article on introduce XReport

Sample Image - XReport.png

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>
/// &#24212;&#29992;&#31243;&#24207;&#30340;&#21629;&#20196;&#21442;&#25968;
/// </summary>
public static System.Collections.Specialized.NameValueCollection myArgs = 
            new System.Collections.Specialized.NameValueCollection();

/// <summary>
/// &#31243;&#24207;&#20837;&#21475;&#28857;
/// </summary>
[STAThread]
static void Main() 
{
    try
    {
        // &#20174;&#21629;&#20196;&#34892;&#20013;&#21152;&#36733;&#21442;&#25968;
        AnalyseArgs( System.Environment.GetCommandLineArgs());
        // &#20174;&#21442;&#25968;&#25991;&#20214;&#20013;&#21152;&#36733;&#21442;&#25968;
        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 , "&#31995;&#32479;&#38169;&#35823;:" + ext.Message , "&#31995;&#32479;&#38169;&#35823;" , 
                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 .

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

fu yuans

Web Developer
sinosoft
China China

Member

yfyuan of Sinosoft , come from CHINA , 2008 Microsoft MVP,Use GDI+,XML/XSLT, site:http://www.sinoreport.net/

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionWhere is the source code of the report engine? Pinmemberseandx14:06 18 Feb '09  
GeneralLanguage Pinmembermajid2423:40 21 Oct '06  
GeneralRe: Language Pinmemberpita200010:46 28 Mar '07  
Generalwill be nice, ... PinmemberLautas3:45 11 Aug '06  
will be nice is the default language are English Big Grin | :-D
 
magsoft

GeneralLooks really nice but.. PinmemberLaubi21:12 10 Aug '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 11 Aug 2006
Article Copyright 2006 by fu yuans
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid