Forum Sementara Putera.com

Would you like to react to this message? Create an account in a few clicks or log in to continue.
Forum Sementara Putera.com

Bersama kita perkemaskan forum ini sementara forum asal dalam pemulihan.

Forum putera dah kembali. Masalah sudah berjaya diselesaikan. Sila lawati http://forum.putera.com/tanya


    mintak tolong bgaimane eksport gridview to excel (gune .net c# vs2008 n mssql server2008)

    avatar
    kowmeng
    Ahli Baharu
    Ahli Baharu


    Gender : Male Number of posts : 50
    Location : KL
    Job/hobbies : knowledge seekers/aku job hunter!
    Registration date : 04/03/2009

    mintak tolong bgaimane eksport gridview to excel (gune .net c# vs2008 n mssql server2008) Empty mintak tolong bgaimane eksport gridview to excel (gune .net c# vs2008 n mssql server2008)

    Post by kowmeng Tue Jan 19, 2010 2:03 pm

    assalam...saya ade code utk eksport gridview to excel tp ade error yg sy xfaham..ni coding saya..FYI sy baru lg dlm c#..huhuh..sy gune vs2008 mssqlserver2008..dlm page design ade gune Ajax toolkit..sy mahu report dari gridview bile click buttonexport dier bole pegi ke excel format...mintak tolong ya..

    coding ni dlm page aspx.Cs

    using System;
    using System.Data;
    using System.IO;
    using System.Web.UI;
    using App_Code.Utility;// namespace ni bile declare jadi kelabu n bgtaw "using directive is not required"
    using System.Web.Security;//namespace ni bile declare jadi kelabu n bgtaw "using directive is not required"
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;//namespace ni bile declare jadi kelabu n bgtaw "using directive is not required"
    using System.Web.UI.HtmlControls;//namespace ni bile declare jadi kelabu n bgtaw "using directive is not required"




    protected void Button3_Click(object sender, EventArgs e)//ni buttonexport.
    {

    PrepareGridViewForExport(GridView1);

    ExportGridView();

    }

    private void ExportGridView()
    {

    string attachment = "attachment; filename=Contacts.xls";

    Response.ClearContent();

    Response.AddHeader("content-disposition", attachment);

    Response.ContentType = "application/ms-excel";

    StringWriter sw = new StringWriter();

    HtmlTextWriter htw = new HtmlTextWriter(sw);

    GridView1.RenderControl(htw);

    Response.Write(sw.ToString());

    Response.End();

    }



    public override void VerifyRenderingInServerForm(Control control)
    {

    }

    private void PrepareGridViewForExport(Control gv)
    {

    LinkButton lb = new LinkButton();

    Literal l = new Literal();

    string name = String.Empty;

    for (int i = 0; i < gv.Controls.Count; i++)
    {

    if (gv.Controls[i].GetType() == typeof(LinkButton))
    {

    l.Text = (gv.Controls[i] as LinkButton).Text;

    gv.Controls.Remove(gv.Controls[i]);

    gv.Controls.AddAt(i, l);

    }

    else if (gv.Controls[i].GetType() == typeof(DropDownList))
    {

    l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text;

    gv.Controls.Remove(gv.Controls[i]);

    gv.Controls.AddAt(i, l);

    }

    else if (gv.Controls[i].GetType() == typeof(CheckBox))
    {

    l.Text = (gv.Controls[i] as CheckBox).Checked ? "True" : "False";

    gv.Controls.Remove(gv.Controls[i]);

    gv.Controls.AddAt(i, l);

    }

    if (gv.Controls[i].HasControls())
    {

    PrepareGridViewForExport(gv.Controls[i]);

    }

    }
    avatar
    kowmeng
    Ahli Baharu
    Ahli Baharu


    Gender : Male Number of posts : 50
    Location : KL
    Job/hobbies : knowledge seekers/aku job hunter!
    Registration date : 04/03/2009

    mintak tolong bgaimane eksport gridview to excel (gune .net c# vs2008 n mssql server2008) Empty Re: mintak tolong bgaimane eksport gridview to excel (gune .net c# vs2008 n mssql server2008)

    Post by kowmeng Wed Jan 20, 2010 10:28 am

    Sad xde ke owang nk tolong prob aku nih..huhu!

      Current date/time is Mon Nov 18, 2024 9:42 am