asp.net export Excel's functional code _ Practical tips

Source: Internet
Author: User
Tags httpcontext
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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.