Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
When i export my gridview to excel, certain controls gets exported when i try to do so (i.e.Button and Checkbox), how can i remove/hide those controls from appearing in my excel sheet?

Here is the code that i am using to export my Gridview to excel.
Response.Clear(); Response.AddHeader("content-disposition", "attachment;filename=Untitled.xls"); Response.Charset = ""; // If you want the option to open the Excel file without saving then // comment out the line below // Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.ContentType = "application/vnd.xls"; System.IO.StringWriter stringWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter); worklistGridView.RenderControl(htmlWriter); Response.Write(stringWriter.ToString()); Response.End();
Posted
Updated 16-Mar-11 19:56pm
v3
Comments
Prerak Patel 17-Mar-11 1:20am    
How do you export?
Raj.rcr 17-Mar-11 1:32am    
could u plz post ur code? so that some one can try to rectify this issue.
Nischal Bhatt 17-Mar-11 1:46am    
here is the code that i am using to export my gridview to excel.
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=Untitled.xls");
Response.Charset = "";
// If you want the option to open the Excel file without saving then
// comment out the line below
// Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
worklistGridView.RenderControl(htmlWriter);
Response.Write(stringWriter.ToString());
Response.End();

you can use open source library called iTextSharp. With this you can export your gridview data.
 
Share this answer
 
This is what i found, it's considered as the best practice while exporting a GridView to Excel and it works like a charm.
have a look at the link...
http://www.koders.com/csharp/fid8E1506538F1D8C8626B4EDA7F7D6163DACF2C3C4.aspx?s=gridview#L15[^]
 
Share this answer
 
v2

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