# Region Excel export Button Function protected void btnoutput_click (Object sender, eventargs e) {Export ("application/MS-excel", "disc knot table .xls "); // call the export method} # endregion # region export Excel method private void Export (string filetype, string filename) {// clear the page gridview1.allowpaging = false; gridview_bindbydate (); response. clear (); response. buffer = true; // set the output character set response. charset = "gb2312"; // solves the problem of garbled characters exported to excel2007. write ("<meta http-equiv = Content-Type content = text/html; charset = gb2312>"); // The exported file name is the disk node fruit table .xls response. appendheader ("content-disposition", "attachment; filename =" + httputility. urlencode (filename, encoding. utf8 ). tostring (); // solves the Response Problem of exporting to excel2007 garbled characters. contentencoding = system. text. encoding. getencoding ("gb2312"); // you can specify the response format for the exported file. contenttype = filetype; // disable viewstate this. enableviewstate = false; stringwriter = new stringwriter (); htmltextwriter textwriter = new htmltextwriter (stringwriter); gridview1.rendercontrol (textwriter); // write HTML back to the browser response. write (stringwriter. tostring (); response. flush (); response. end (); gridview1.allowpaging = true; // restore pagination // rebind the data source gridview_bindbydate ();} # endregion # region export word button to enable protected void btnword_click (Object sender, eventargs e) {gridview1.allowpaging = false; // clear the page gridview_bindbydate (); response. clear (); response. bufferoutput = true; // set the output character set response. charset = "gb2312"; // the file name is filename.doc response. appendheader ("content-disposition", "attachment?filename=filename.doc"); response. contentencoding = system. text. encoding. getencoding ("gb2312"); // you can specify the response format for the exported file. contenttype = "application/MS-word"; // disable viewstate this. enableviewstate = false; cultureinfo = new cultureinfo ("ZH-CN", true); stringwriter = new stringwriter (cultureinfo); extends textwriter = new evaluate (stringwriter); convert (textwriter ); //// write the HTML back to the browser response. write (stringwriter. tostring (); response. flush (); response. end (); // restore pagination gridview1.allowpaging = true; // rebind the data source gridview_bindbydate () for the gridview;} # endregion note: # region form flag // if there is no following method, an error will be reported. The "gridview1" control of the "gridview" type must be placed in the form tag with runat = server (used to export Excel or word) public override void verifyrenderinginserverform (Control) {}# endregion