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