CopyCode The Code is as follows: // export from 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 an Excel export code used by many people on the Internet. However, many deficiencies are found during the use process, and other controls are not required.
Disadvantages:
1. I can only export the first page when using aspnetpager paging.
2. Use is not flexible, style control, and field control is not flexible.
Note:
1. <% @ page Language = "C #" autoeventwireup = "true" enableeventvalidation = "false" codefile = "default. aspx. cs" inherits = "_ default" %>
Add this sentence.
2. Add the following to the background file:
Public override void verifyrenderinginserverform (Control)
{
}
3. Using system. Io needs to be introduced;
Other Related articles :
Asp. net Operation Excel memo
Asp.net connection Excel Code
Asp.net read and display Excel Data Implementation Code
use C # to create dynamic excel in Asp.net
Asp.net operation excel implementation code