Export Gridview to excel

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • johnlim20088
    New Member
    • Jan 2007
    • 74

    Export Gridview to excel

    Hi,

    Hi, currently I have a code below on my Listcontact.asp x file to export my gridview data to excel:-

    It is work success, and export the 'Contacts.xls' to my folder.

    But have following problem->
    1) when i open with wordpad, it show html tag with the value.
    2) when i open with excel, it look ok, BUT i don't want the colour and button to be export.

    CAN i format it as normal line by line record?

    private void PrepareGridView ForExport(Contr ol gv)
    {
    LinkButton lb = new LinkButton();
    Literal l = new Literal();

    string name = String.Empty;

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

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

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

    gv.Controls.Rem ove(gv.Controls[i]);

    gv.Controls.Add At(i, l);

    }

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

    l.Text = (gv.Controls[i] as DropDownList).S electedItem.Tex t;

    gv.Controls.Rem ove(gv.Controls[i]);

    gv.Controls.Add At(i, l);

    }

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

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

    gv.Controls.Rem ove(gv.Controls[i]);

    //gv.Controls.Add At(i, l);

    }

    if (gv.Controls[i].HasControls())
    {
    PrepareGridView ForExport(gv.Co ntrols[i]);
    }


    }
    }


    private void ExportTerminati onLog()
    {

    string attachment = "attachment ; filename=Contac ts.xls";
    Response.ClearC ontent();
    Response.AddHea der("content-disposition", attachment);
    Response.Conten tType = "applicatio n/Excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter( sw);
    GridView1.Rende rControl(htw);
    Response.Write( sw.ToString());
    Response.End();
    }

    public override void VerifyRendering InServerForm(Co ntrol control)
    {
    }
Working...