1. Add the green font code to the page <% @ page Language = "C #" codefile = "111. aspx. CS "inherits =" 111 "enableeventvalidation =" false "%> 2. Methods in the class file /// <summary> /// export the Excel button /// </Summary>/ // <Param name = "sender"> </param> // <Param name = "E"> </param> protected void lbtoexcel_click (Object sender, eventargs e) {// Note: gvdata is the name of the gridview control gvdata. allowpaging = false; gvdata. databind (); // name of the exported file var filename = string. format ("{0: yyyymmddhhmmss}", datetime. now); // specific export implementation logic exportcontrol (gvdata, "Excel", filename); gvdata. allowpaging = true; gvdata. columns [0]. visible = true;} // <summary> // export the web control or page information (with the file name parameter) /// </Summary> /// <Param name = "Source"> Control instance </param> /// <Param name = "documenttype"> export type: excel or word </param> /// <Param name = "FILENAME"> Save the file name </param> Public void exportcontrol (system. web. UI. control source, string documenttype, string filename) {// sets the HTTP header information. The encoding format is if (documenttype = "Excel") {// prevents garbled characters, adding this line can prevent garbled httpcontext when there is only one line of data. current. response. write ("<meta http-equiv = Content-Type content = text/html; charset = UTF-8>"); // Excel httpcontext. current. response. appendheader ("content-disposition", "attachment; filename =" + httputility. urlencode (filename + ". xls ", system. text. encoding. utf8); httpcontext. current. response. contenttype = "application/MS-excel";} else if (documenttype = "word") {// prevents garbled characters, adding this line can prevent garbled httpcontext when there is only one line of data. current. response. write ("<meta http-equiv = Content-Type content = text/html; charset = UTF-8>"); // word httpcontext. current. response. appendheader ("content-disposition", "attachment; filename =" + httputility. urlencode (filename + ". doc ", system. text. encoding. utf8); httpcontext. current. response. contenttype = "application/MS-word";} httpcontext. current. response. charset = "UTF-8"; httpcontext. current. response. contentencoding = system. text. encoding. utf8; // close the control's view status Source. page. enableviewstate = false; // initialize htmlwriter system. io. stringwriter writer = new system. io. stringwriter (); system. web. UI. htmltextwriter htmlwriter = new system. web. UI. htmltextwriter (writer); source. rendercontrol (htmlwriter); // output httpcontext. current. response. write (writer. tostring (); httpcontext. current. response. end () ;}// rewrite a method. The public override void verifyrenderinginserverform (Control) must be added. {// base. verifyrenderinginserverform (control );}