Function Code of exporting EXCEL from Asp.net

Source: Internet
Author: User

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

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.