Export data to excel/word to prevent Garbled text when only one row of data is exported

Source: Internet
Author: User

 

   
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 );}

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.