Exporttoexcel (work notes)

Source: Internet
Author: User

1. datatable export to excel

(1) datatabletoexcel Function

     ///   <Summary>      ///  Datatable output to excel  ///   </Summary>      ///   <Param name = "DT"> d  Atatable Dataset  </Param>      ///   <Param name = "FILENAME"> E  Xcel file name  </Param>     Protected   Void Datatabletoexcel (datatable DT, String  Filename) {system. Web. UI. webcontrols. gridview gv = New  System. Web. UI. webcontrols. gridview (); GV. datasource = DT; GV. databind ();
// Sets the HTTP character set for the network output stream to UTF-8 (depending on your website format), and current gets the httpcontext object for the current HTTP Request Httpcontext. Current. response. charset = " UTF-8 " ; Httpcontext. Current. response. contentencoding = System. Text. encoding. utf8;
// Set the httpmime type of the output stream to excel. Httpcontext. Current. response. contenttype = " Application/MS-Excel " ;
// Add the HTTP header to the output stream Httpcontext. Current. response. appendheader ( " Content-Disposition " , " Attachment; filename = " + "" + Filename + " . Xls " );
// Do not save the view status of the control // CTL. Page. enableviewstate = false; System. Io. stringwriter Sw = New System. Io. stringwriter ();
// Write text to the output stream System. Web. UI. htmltextwriter hW = New System. Web. UI. htmltextwriter (SW ); // Output the contents of the server control to the htmltextwriter object.

GV. rendercontrol (HW );
// Stringwriter. tostring returns the string containing the characters written to the current stringwriter so far. Httpcontext. Current. response. Write (SW. tostring (); httpcontext. Current. response. End ();}

(2) Application (Here we use dataset to convert datatable)

Dataset DS =Dao. getevaluationstationbasicinfobycondition (infobean); datatabletoexcel (Ds. Tables [0],"Data");

 

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.