Export the gridview data to excel

Source: Internet
Author: User

Recently I want to sort out some word libraries, so I am too lazy to find them manually. I am afraid that my hand will be blank, so I decided to write it.ProgramComplete.

First, use the dataGridviewThe control is displayed on the page. The effect is as follows:

 

next we will gridview Excel , the method is as follows:

 

  ///   <Summary>
/// Export EXCEL from gridview
///   </Summary>
///   <Param name = "CTL"> Gridview Control ID </Param>
///   <Param name = "FILENAME"> Export Excel name </Param>
Private   Void Toexcel (control CTL, String Filename)
{
Httpcontext. Current. response. charset =   " UTF-8 " ;
Httpcontext. Current. response. contentencoding = System. Text. encoding. utf8;
Httpcontext. Current. response. contenttype =   " Application/MS-Excel " ;
Httpcontext. Current. response. appendheader ( " Content-Disposition " , " Attachment; filename = "   +   ""   + Filename );
CTL. Page. enableviewstate =   False ;
System. Io. stringwriter TW =   New System. Io. stringwriter ();
Htmltextwriter HW =   New Htmltextwriter (TW );
CTL. rendercontrol (HW );
Httpcontext. Current. response. Write (TW. tostring ());
Httpcontext. Current. response. End ();
}

 

 

Add an event on the button to exportExcel. Note that becauseGridviewThere are paging and sorting settings. to display the complete data, you must cancel the paging sorting and export the data before resuming the settings.CodeAs follows:

 

Protected   Void Btnexport_click ( Object Sender, eventargs E)
{
Gvword. allowpaging =   False ;
Gvword. allowsorting =   False ;
Display ();
Toexcel (gvword, " Word.xls " );
Gvword. allowsorting =   True ;
Gvword. allowpaging =   True ;
Display ();
}

 

 

However, an error is reported after running:

Type" Gridview "Control" Gridview1 "Must be placed Runat = Server .

 

Add rewrite method:

 

  Public   Override   Void Verifyrenderinginserverform (Control)
{
// Base. verifyrenderinginserverform (control );
}

 

 

The effect is as follows:

 

Now, the program can help me sort it out, saving a lot of trouble.

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.