Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want my gridview data to be exported to excel.
My Gridview and the button used for exporting is inside update panel.
I have used <triggers><asp:asynPost...>button>...
but this is not solving my problem
I am using following code for exporting inside button click.

Response.Clear();
Response.ContentType = "application/vnd.xls";
Response.AddHeader("content-disposition",
"attachment;filename=contacts.xls");

StringWriter swriter = new StringWriter();
HtmlTextWriter hwriter = new HtmlTextWriter(swriter);

HtmlForm frm = new HtmlForm();
this.gv_productsRequests.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(this.gv_productsRequests);
frm.RenderControl(hwriter);

Response.Write(swriter.ToString());
Response.End();


Any help will be appreciated..
Posted
Updated 6-Apr-12 1:46am
v2
Comments
sravani.v 6-Apr-12 7:47am    
added <pre> tags

You can try this custom tool
Export to Excel
 
Share this answer
 
Hi ....

You can try this below code.
C#
private void ExportGridView()
    {
        string attachment = "attachment; filename=Employee details.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();
    }



C#
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]);
           }
       }



C#
protected void Expbtn_Click(object sender, EventArgs e)
    {
        PrepareGridViewForExport(GridView1);
        ExportGridView();

   }
 
Share this answer
 
v3
Comments
sravani.v 6-Apr-12 7:47am    
added <pre> tags
Its just the same code i had shared.
Just add in the first line of your source view--EnableEventValidation="false"
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 14-Mar-13 1:45am    
Are you talking to yourself? :-)
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900