Using the pagination function to export Excel or word through the gridview

Source: Internet
Author: User
# 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

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.