This article reproduced:http://mattberseth.com/blog/2007/04/export_gridview_to_excel_1.html
Http://geekswithblogs.net/azamsharp/archive/2005/12/21/63843.aspx
Reference: Http://forums.asp.net/t/1221467.aspx
Export GridView to Excel
Using system;using system.data;using system.configuration;using system.io;using system.web;using System.Web.Security ; using system.web.ui;using system.web.ui.webcontrols;using system.web.ui.webcontrols.webparts;using system.web.ui.htmlcontrols;///<summary>/////</summary>public class gridviewexportutil{//< Summary>/////</summary>//<param name= "FileName" ></param>//<param name= "GV" & gt;</param> public static void Export (String fileName, GridView gv) {HttpContext.Current.Response.Clea R (); HttpContext.Current.Response.AddHeader ("Content-disposition", String. Format ("attachment; Filename={0} ", FileName)); HttpContext.Current.Response.ContentType = "Application/ms-excel"; using (stringwriter SW = new StringWriter ()) {using (HtmlTextWriter HTW = new HtmlTextWriter (SW)) {//Create a table to contain the grid tableTable = new Table (); Include the Gridline settings table. GridLines = GV. GridLines; Add the header row to the table if (GV. HeaderRow = null) {Gridviewexportutil.preparecontrolforexport (GV. HeaderRow); Table. Rows.Add (GV. HeaderRow); }//Add each of the data rows to the table foreach (GridViewRow row in GV. Rows) {gridviewexportutil.preparecontrolforexport (row); Table. Rows.Add (row); }//Add the footer row to the table if (GV. Footerrow = null) {Gridviewexportutil.preparecontrolforexport (GV. Footerrow); Table. Rows.Add (GV. Footerrow); }//render the table into the HTMLWriter table. RenderControl (HTW); Render theHTMLWriter into the response HttpContext.Current.Response.Write (SW. ToString ()); HttpContext.Current.Response.End (); }}}///<summary>//Replace any of the contained controls with literals///</summary> <param name= "Control" ></param> private static void Preparecontrolforexport (Control control) { for (int i = 0; i < control. Controls.Count; i++) {Control current = control. Controls[i]; If (current is LinkButton) {control. Controls.remove (current); Control. Controls.addat (i, new LiteralControl (current as LinkButton). Text)); } else if (current is ImageButton) {control. Controls.remove (current); Control. Controls.addat (i, new LiteralControl (current as ImageButton). AlternateText)); } else if (current is HyperLink) { Control. Controls.remove (current); Control. Controls.addat (i, new LiteralControl (current as HyperLink). Text)); } else if (current is DropDownList) {control. Controls.remove (current); Control. Controls.addat (i, new LiteralControl (current as DropDownList). Selecteditem.text)); } else if (current is CheckBox) {control. Controls.remove (current); Control. Controls.addat (i, new LiteralControl (current as CheckBox). Checked? "True": "False")); } if (current. Hascontrols ()) {gridviewexportutil.preparecontrolforexport (current); } } }}
This article reproduced: http://www.webpronews.com/aspnet-export-a-datatable-to-excel-2006-11
ASP.NET:Export a DataTable to Excel
Back in February I wrote a post on what to export DataTables to XML and Excel and I still get a lot of search engine Traffi C to that post.
People has been asking me to simplify the example and only concentrate on the Excel export, so this ' s what I'll do now.
All spreadsheet applications (Excel, Calc etc.) understand semicolon separated files natively, so everyone can use this me Thod–you don ' t even has to use Excel for it to work.
public static void Exporttospreadsheet (DataTable table, string name)
{
HttpContext context = HttpContext.Current;
Context. Response.Clear ();
foreach (DataColumn column in table. Columns)
{
Context. Response.Write (column. ColumnName + ";");
}
Context. Response.Write (Environment.NewLine);
foreach (DataRow row in table. Rows)
{
for (int i = 0; i < table. Columns.count; i++)
{
Context. Response.Write (Row[i]. ToString (). Replace (";", String. Empty) + ";");
}
Context. Response.Write (Environment.NewLine);
}
Context. Response.ContentType = "Text/csv";
Context. Response.appendheader ("Content-disposition", "attachment; Filename= "+ name +". csv ");
Context. Response.End ();
}
Then just call this method and pass the DataTable and the filename as parameters.
Exporttospreadsheet (table, "products");