Sign In
Register
Friends
     
Home
Articles
Source Codes
Interview Questions
Forums
Survey
Submit Articles
Start Discussion
Invite friends
Archive
Members
Click Here to Sign Up
Resources
Forums
Members
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 Member
Click here to login
Register For FREE
Top Resources
ASP.net MVC Interview Questions Answers
Interview Questions
Serial Number in SSRS
Articles
How to Print a Crystal Report direct to printer
Articles
Get Started Developing for Android Apps with Eclipse
Articles
Razor View Engine Interview Questions Answers
Interview Questions
.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
Difference between Encapsulation and Abstraction
Interview Questions
Explaining SDLC -System Development Life Cycle
Articles
SPIRE PDF Library
Articles
Infosys Interview Questions
Interview Questions
Html5 interview questions and answers
Interview Questions
Dynamic Menu using HTML List Tag and CSS in ASP.Net
Articles
SharePoint 2010 interview Questions Answers
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:
5165
Code For Export Data From DataGrid to Excel Sheet By Rakesh Sinha
Previous Article:
Sql Server Database Backup and Restore through C#
Next Article:
Oracle Providers for ASP.NET
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
public static void ExportDataGridToExcel(DataGrid dgrdExport,Page pg)
{
try
{
pg.Response.Clear();
pg.Response.Buffer= true;
pg.Response.ContentType = "application/vnd.ms-excel";
pg.Response.Charset = "";
pg.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new
System.Web.UI.HtmlTextWriter(oStringWriter);
ClearControls(dgrdExport);
dgrdExport.RenderControl(oHtmlTextWriter);
pg.Response.Write(oStringWriter.ToString());
pg.Response.End();
}
catch(System.Exception ex)
{
throw ex;
}
}
//Code For Clear Controls from form
private static void ClearControls(Control control)
{
for (int cnt=control.Controls.Count -1; cnt>=0; cnt--)
{
ClearControls(control.Controls[cnt]);
}
if (!(control is TableCell))
{
if (control.GetType().GetProperty("SelectedItem") != null)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
try
{
literal.Text = (string)control.GetType().GetProperty("SelectedItem").GetValue(control,null);
}
catch
{
}
control.Parent.Controls.Remove(control);
}
else
if (control.GetType().GetProperty("Text") != null)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
literal.Text = (string)control.GetType().GetProperty("Text").GetValue(control,null);
control.Parent.Controls.Remove(control);
}
}
return;
}
Please enable JavaScript to view the
comments powered by Disqus.
comments powered by
Disqus
Previous Article:
Sql Server Database Backup and Restore through C#
Next Article:
Oracle Providers for ASP.NET
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: 2799 | Submitted on: 9/21/2012 3:05:29 AM
Oracle Interview Questions part 2
View: 1916 | Submitted on: 1/7/2011 3:45:40 AM
Oracle Interview Questions part 1
View: 1947 | Submitted on: 1/7/2011 3:43:40 AM
Oracle Interview Questions
View: 1998 | Submitted on: 1/7/2011 3:36:49 AM
Submit Articles
|
All Post of This User..
Keyword Cloud
.NET Framework
ADO.NET
AJAX
ASP.NET
ASP.NET AJAX
ASP.NET MVC
Azure
Best Practices
BizTalk Server
C++
CSharp
CSS
Error and Resolution
IIS
JAVA
JavaScript
JQwery
JSP
LINQ
Management
ODP.NET
OOPS
Oracle
Others
Pattern and Practices
PHP
SAP
SEO
SharePoint
Silverlight
Sql Server
UML
UNIX
VB
VB.NET
VS 2010
WCF
Web Analytics
Windows Forms
WPF
WWF
XML
XSLT
Advertise
About Us
Private Policy
Terms of use
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