Copy Code code as follows:
Exported by GRIDVIW to Excel
public static void Toexcel (System.Web.UI.Control ctl)
{
HttpContext.Current.Response.AppendHeader ("Content-disposition", "Attachment;filename=excel.xls");
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType = "Application/ms-excel";//image/jpeg;text/html;image/gif;vnd.ms-excel/ MSWord
Ctl. Page.enableviewstate = false;
System.IO.StringWriter tw = new System.IO.StringWriter ();
System.Web.UI.HtmlTextWriter HW = new System.Web.UI.HtmlTextWriter (TW);
Ctl. RenderControl (HW);
HttpContext.Current.Response.Write (TW. ToString ());
HttpContext.Current.Response.End ();
}
This is a network of many people use the export of Excel code, but the use of the process found many deficiencies, it does not introduce other controls.
Disadvantages:
1, I adopt aspnetpager pagination can only export the first page.
2, the use of very inflexible, the control of the style, the control of the field is not flexible.
you need to note in use:
1, <%@ Page language= "C #" autoeventwireup= "true" enableeventvalidation= "false" codefile= "Default.aspx.cs" Inherits = "_default"%>
Add the sentence.
2, in the background file plus:
public override void Verifyrenderinginserverform (Control control)
{
}
3, the need to introduce using System.IO;
Other related articles:
Asp. NET Operation Excel Memo
asp.net code to connect Excel
asp.net read and display the implementation code for Excel data
Building dynamic Excel with C # in asp.net
asp.net to manipulate Excel implementation code