yuan yongfu

yuan yongfu

  • NA
  • 1
  • 3.1k

XReport

Aug 11 2006 11:51 PM

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.

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.

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();
            }
        }
    }
}

Learn more information about XReport , please visit http://www.xdesigner.cn or Email [email protected] .