Export GridView Only to Excel in asp.net
-
Dear All, I am exporting grid view to excel using following code
void Export()
{
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();
}but what happens, It exports all the control to excel. I have button, listbox and other controls above my GridView but when exporting I want to export only my gridview to Excel using above code all the page is exported, any ideas how to export only gridview to excel?
Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan
-
Dear All, I am exporting grid view to excel using following code
void Export()
{
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();
}but what happens, It exports all the control to excel. I have button, listbox and other controls above my GridView but when exporting I want to export only my gridview to Excel using above code all the page is exported, any ideas how to export only gridview to excel?
Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan
Your code looks clean to me. It should work. Have you overridden
VerifyRenderingInServerForm
method?public override void VerifyRenderingInServerForm(Control control) { // Confirms that an HtmlForm control is rendered for the // specified ASP.NET server control at run time. }
-
Dear All, I am exporting grid view to excel using following code
void Export()
{
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();
}but what happens, It exports all the control to excel. I have button, listbox and other controls above my GridView but when exporting I want to export only my gridview to Excel using above code all the page is exported, any ideas how to export only gridview to excel?
Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan
This is a working sample, Export Gridview in ASP.NET[^]
Arun Jacob http://codepronet.blogspot.com/
-
Your code looks clean to me. It should work. Have you overridden
VerifyRenderingInServerForm
method?public override void VerifyRenderingInServerForm(Control control) { // Confirms that an HtmlForm control is rendered for the // specified ASP.NET server control at run time. }
I have already done that but no success.
Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan
-
This is a working sample, Export Gridview in ASP.NET[^]
Arun Jacob http://codepronet.blogspot.com/
thanks for your reply. I have done the same thing, but all controls within the page are rendered and exported to excel file. I want to export only the gridview not other controls.
Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan