可將GridView的結果以報表顯示於網頁上並列印,也可存成 Excel Word 檔案格式。


使用的資料庫為「北風資料庫」,資料表為「Products」。


 


建立


檔是用於報表檔的資料來源。


加入新項目 / 資料集。



檔案置於「app_code」中



由伺服器總管中拖曳「products」資料表到設計視窗中



 


建立報表檔


報表檔為顯示格式。


加入新項目 / Crystal Report



使用預設值。



選取 專案資料 / ADO.NET 資料集 / Dataset1 / Products,移到右方,按「下一步」鈕。



配合 GridView 選取要顯示的欄位移到右方,按「完成」鈕。



安排報表格式。



 


建立< GridviewCrystal.aspx>


GridView 用於顯示資料,CrystalReportViewer用於顯示報表。


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridviewCrystal.aspx.cs" Inherits="GridviewCrystal" %>



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridviewCrystal.aspx.cs" Inherits="GridviewCrystal" %>


 


<%@ Register Assembly="CrystalDecisions.Web, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"


    Namespace="CrystalDecisions.Web" TagPrefix="CR" %>


 



 




   




   


       



            ">

           


            搜尋" />

            顯示報表" />

            隱藏報表" />

            存成Excel" />

            存成Word" />

           





            


               


                   


                       


                            


                       


                   


                   


                        


                           


                       


                   


                   


                       


                           


                       


                   


                    


                       


                           


                       


                   


               


           


           





           

                AutoDataBind="True" Height="1123px"


                Width="896px" />


       



       

            SelectCommand="SELECT * FROM [Products] WHERE ([ProductName] LIKE + @ProductName + '%')">


           


               

                    Type="String" />


           


       


        

            SelectCommand="SELECT * FROM [Products]">


   



  





 


< GridviewCrystal.aspx>程式碼


using System;


using System.Collections;


using System.Configuration;


using System.Data;


using System.Web;


using System.Web.Security;


using System.Web.UI;


using System.Web.UI.HtmlControls;


using System.Web.UI.WebControls;


using System.Web.UI.WebControls.WebParts;


 


public partial class GridviewCrystal : System.Web.UI.Page


{


    protected void Page_Load(object sender, EventArgs e)


    {


       


    }


    protected void btnSearch_Click(object sender, EventArgs e)


    {


        if (this.txbProductName.Text.Trim() == "")


        {


            //使用沒條件的SqlDataSource


            this.GridView1.DataSourceID = this.SqlDataSource2.ID;


        }


        else


        {


            //使用有條件的SqlDataSource


            this.GridView1.DataSourceID = this.SqlDataSource1.ID;


        }


        CrystalReportViewer1.Visible = false;


    }


    protected void btnReport_Click(object sender, EventArgs e)


    {


        CrystalReportViewer1.Visible = true;


 


        DataTable dt = new DataTable();


 


        if (this.GridView1.DataSourceID == this.SqlDataSource1.ID)


        {


            dt = ((DataView)this.SqlDataSource1.Select(DataSourceSelectArguments.Empty)).ToTable();


        }


        else


        {


            dt = ((DataView)this.SqlDataSource2.Select(DataSourceSelectArguments.Empty)).ToTable();


        }


 


        //建立 CrystalReport 物件 


        CrystalDecisions.CrystalReports.Engine.ReportDocument report = new CrystalDecisions.CrystalReports.Engine.ReportDocument();


 


        //讀入報表檔


        report.Load(Server.MapPath("CrystalReport.rpt"));


 


        //將資料置入報表


        report.SetDataSource(dt);


 


        //檢查按了那一個鈕


        Button obj=(Button) sender;


        //顯示報表


        if (obj.ID == "btnReport")


        {


            CrystalReportViewer1.ReportSource = report;


        }


        //存成Excel


        else if (obj.ID == "btnExcel")


        {


            report.ExportToHttpResponse(CrystalDecisions.Shared.ExportFormatType.Excel, Response, true, "report");


        }


        //存成Word


        else if (obj.ID == "btnWord")


        {


            report.ExportToHttpResponse(CrystalDecisions.Shared.ExportFormatType.WordForWindows, Response, true, "report");


        }


    }


    //隱藏報表


    protected void btnHide_Click(object sender, EventArgs e)


    {


        CrystalReportViewer1.Visible = false;


    }


}


 


執行結果



 



範例檔下載


http://cid-9afc1e58228b3fc9.skydrive.live.com/self.aspx/%e5%85%ac%e9%96%8b/testNorth.zip



 


arrow
arrow
    全站熱搜

    tsjeng 發表在 痞客邦 留言(0) 人氣()