Join Dotnetcodes DotnetCodes.com is online Discussion Forum for Software professionals . It lets you find friends around the world and Create professional network that share similar interests as you. Get help on ur projects by industry specialists. Also get answers to all ur technical/placement related querries.Get an edge over others.
Already MemberClick here to login
ASP.net MVC Interview Questions Answers Interview Questions
Get Started Developing for Android Apps with Eclipse Articles
Serial Number in SSRS Articles
.Net framework 4.0 Interview Questions Answers Interview Questions
SQL server reporting services Interview Questions (SSRS) part 1 Articles
Whats New in ASP.NET 4.0 Part 2 Articles
How to Print a Crystal Report direct to printer Articles
Difference between Encapsulation and Abstraction Interview Questions
Explaining SDLC -System Development Life Cycle Articles
Html5 interview questions and answers Interview Questions
SharePoint 2010 interview Questions Answers Interview Questions
Infosys Interview Questions Interview Questions
Dynamic Menu using HTML List Tag and CSS in ASP.Net Articles
Populate or bind Dropdownlist in Asp.net using Jquery and Json Articles
ASP.NET MVC Overview Interview Questions
Submit Articles | More Articles..

Export DataGrid to Excel in C#

Posted By: Jaishree On:8/12/2010 12:28:04 AM in:Source Codes Category:CSharp Hits:4149
Code For Export Data From DataGrid to Excel Sheet By Rakesh Sinha

Code For Export Data From DataGrid to Excel Sheet 

Here is the source code for export records from Grid to Excel

Parameter Info for below code:

  • dgrdExport is DataGrid ID/Name
  • pg is Page id/this

//Code For Export Data From DataGrid to Excel Sheet 

  1. public static void ExportDataGridToExcel(DataGrid dgrdExport,Page pg)
  2. {
  3. try
  4. {
  5. pg.Response.Clear();
  6. pg.Response.Buffer= true;
  7. pg.Response.ContentType = "application/vnd.ms-excel";
  8. pg.Response.Charset = "";
  9. pg.EnableViewState = false;
  10. System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
  11. System.Web.UI.HtmlTextWriter oHtmlTextWriter = new                 System.Web.UI.HtmlTextWriter(oStringWriter);
  12.               ClearControls(dgrdExport);
  13. dgrdExport.RenderControl(oHtmlTextWriter);
  14. pg.Response.Write(oStringWriter.ToString());
  15. pg.Response.End();
  16. }
  17. catch(System.Exception ex)
  18. {
  19. throw ex;
  20. }

  21. }

//Code For Clear Controls from form

  1. private static void ClearControls(Control control)
  2. {
  3. for (int cnt=control.Controls.Count -1; cnt>=0; cnt--)
  4. {
  5. ClearControls(control.Controls[cnt]);
  6. }
  7. if (!(control is TableCell))
  8. {
  9. if (control.GetType().GetProperty("SelectedItem") != null)
  10. {
  11. LiteralControl literal = new LiteralControl();
  12. control.Parent.Controls.Add(literal);
  13. try
  14. {
  15. literal.Text = (string)control.GetType().GetProperty("SelectedItem").GetValue(control,null);
  16. }
  17. catch
  18. {
  19. }
  20. control.Parent.Controls.Remove(control);
  21. }

  22. else

  23. if (control.GetType().GetProperty("Text") != null)
  24. {
  25. LiteralControl literal = new LiteralControl();
  26. control.Parent.Controls.Add(literal);
  27. literal.Text = (string)control.GetType().GetProperty("Text").GetValue(control,null);
  28. control.Parent.Controls.Remove(control);
  29. }
  30. }
  31. return;
  32. }
comments powered by Disqus
User Profile
Jaishree
Sr. Engineer
Delhi , India
Email :You must Log In to access the contact details.
Latest Post from :Jaishree
Function to Convert Text String to Title Case – Proper Case
View: 903 | Submitted on: 3/6/2013 11:23:44 PM
Uploading an image directly into the database and retreive in the PictureBox control in Windows Forms Application
View: 1872 | Submitted on: 9/21/2012 3:05:29 AM
Oracle Interview Questions part 2
View: 1253 | Submitted on: 1/7/2011 3:45:40 AM
Oracle Interview Questions part 1
View: 1095 | Submitted on: 1/7/2011 3:43:40 AM
Oracle Interview Questions
View: 1192 | Submitted on: 1/7/2011 3:36:49 AM
Submit Articles | All Post of This User..

All rights reserved to dotnetcodes. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
Best viewed at 1024 x 768 resolution with Internet Explorer 5.0 or Mozila Firefox 3.5 or Google Crome and higher