The procedure is as follows:
Program Description: The program can be implemented with a large title, and the table first-class function
Calling method: Toexcel (NEWDT, FileName);
protected voidToexcel (DataTable DT,stringFileName) {StringBuilder SB=NewStringBuilder (); Sb. Append ("<style type=\ "Text/css\" >"); Sb. Append ("<!--"); Sb. Append (". Text"); Sb. Append ("{mso-style-parent:style0;"); Sb. Append ("font-size:10.0pt;"); Sb. Append ("font-family:\ "Arial Unicode ms\", Sans-serif;"); Sb. Append ("mso-font-charset:0;"); Sb. Append (@"mso-number-format:\@;"); Sb. Append ("Text-align:center;"); Sb. Append ("border:.5pt solid black;"); Sb. Append ("White-space:normal;}"); Sb. Append (" -"); Sb. Append ("</style>"); Sb. Append ("<table cellspacing=\ "0\" rules=\ "all\" border=\ "1\" style=\ "border-collapse:collapse;\" >"); Datarow[] Myrow=dt. Select (); inti =0; intCL =dt. Columns.count; //Defining HeadingsSb. Append ("<tr align=\ "Center\" style=\ "font-weight:bold;\" >"); Sb. Append ("<td colspan=\ ""+ CL +"\ "Style =\" height:25px; font-size:20pt;\ ">"+ Dropenterprise.selecteditem +"</td>"); Sb. Append ("</tr>"); //Define intermediate headingsSb. Append ("<tr align=\ "Center\" style=\ "font-weight:bold;\" >"); Sb. Append ("<TD >"+ Txtstart.text +"</td>"); for(i =0; I < CL-2; i++) {sb. Append ("<TD ></td>"); } sb. Append ("<TD >"+ Dropenterprise.selecteditem +"</td>"); Sb. Append ("</tr>"); //define Field namesSb. Append ("<tr align=\ "Center\" style=\ "font-weight:bold;\" >"); for(i =0; I < CL; i++) { if(i = = (CL-1) ) {sb. Append ("<td>"+ dt. Columns[i]. Columnname.tostring () +"</td></tr>"); } Else { //Ls_item + = dt. Columns[i]. Columnname.tostring () + "\ T";Sb. Append ("<td>"+ dt. Columns[i]. Columnname.tostring () +"</td>"); } } //Defining Data foreach(DataRow rowinchmyrow) {sb. Append ("<tr align=\ "Center\" >"); for(i =0; I < CL; i++) { if(i = = (CL-1) ) {sb. Append ("<td>"+ Row[i]. ToString () +"</td></tr>"); } Else { //Ls_item + = dt. Columns[i]. Columnname.tostring () + "\ T";Sb. Append ("<td>"+ Row[i]. ToString () +"</td>"); }}} sb. Append ("</table>"); System.Web.HttpContext.Current.Response.Clear (); System.Web.HttpContext.Current.Response.Charset="GB2312"; //System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; //Add header information, specify a default file name for the file download/Save As dialog boxSystem.Web.HttpContext.Current.Response.AddHeader ("content-disposition","attachment; Filename="+ System.Web.HttpUtility.UrlEncode (FileName, System.Text.Encoding.UTF8) +". xls"); //Add header information, specify file size, and allow the browser to display download Progress//System.Web.HttpContext.Current.Response.AddHeader ("Content-length", sb.) ToString ()); //specifies that a stream is returned that cannot be read by the client and must be downloadedSystem.Web.HttpContext.Current.Response.ContentType ="Application/ms-excel"; //send the file stream to the clientSystem.Web.HttpContext.Current.Response.Write (sb.) ToString ()); //Stop the execution of a pageSystem.Web.HttpContext.Current.Response.End (); }
How to export Excel from asp: Using System.Web.HttpContext.Current.Response to export Excel