To export data to an Excel-generic method class, you should modify the requirements yourself.
Resource Download List
usingSystem.Data;usingSystem.IO;namespaceim.common.tools{ Public classExport { Public stringEncoding ="UTF-8"; System.Web.HttpResponse Response=System.Web.HttpContext.Current.Response; Public voidEcportexcel (DataTable DT,stringfileName) { if(dt! =NULL) {StringWriter SW=NewStringWriter (); Createstringwriter (DT,refSW); Sw. Close (); Response. Clear (); Response. Buffer=true; Response. Charset=Encoding; //This . EnableViewState = false;Response. AddHeader ("content-disposition","attachment; Filename="+ FileName +". xls"); Response. ContentType="Application/ms-excel"; //Response. ContentEncoding = System.Text.Encoding.GetEncoding (Encoding)Response. ContentEncoding =System.Text.Encoding.UTF8; Response. Write (SW); Response. End (); } } Private voidCreatestringwriter (DataTable DT,refStringWriter SW) { stringSheetName ="SheetName"; Sw. WriteLine (""); Sw. WriteLine (""); Sw. WriteLine ("<!--[if GTE mso 9]>"); Sw. WriteLine ("<xml>"); Sw. WriteLine ("<x:ExcelWorkbook>"); Sw. WriteLine ("<x:ExcelWorksheets>"); Sw. WriteLine ("<x:ExcelWorksheet>"); Sw. WriteLine ("<x:Name>"+ SheetName +"</x:Name>"); Sw. WriteLine ("<x:WorksheetOptions>"); Sw. WriteLine ("<x:Print>"); Sw. WriteLine ("<x:validprinterinfo/>"); Sw. WriteLine ("</x:Print>"); Sw. WriteLine ("</x:WorksheetOptions>"); Sw. WriteLine ("</x:ExcelWorksheet>"); Sw. WriteLine ("</x:ExcelWorksheets>"); Sw. WriteLine ("</x:ExcelWorkbook>"); Sw. WriteLine ("</xml>"); Sw. WriteLine ("<! [endif]-->"); Sw. WriteLine (""); Sw. WriteLine ("<body>"); Sw. WriteLine ("<table>"); Sw. WriteLine ("<tr>"); string[] Columnarr =New string[dt. Columns.count]; inti =0; foreach(DataColumn columnsinchdt. Columns) {SW. WriteLine ("<td>"+ Columns. ColumnName +"</td>"); Columnarr[i]=columns. ColumnName; I++; } SW. WriteLine ("</tr>"); foreach(DataRow Drinchdt. Rows) {SW. WriteLine ("<tr>"); foreach(stringColumnNameinchColumnarr) {SW. WriteLine ("<td style= ' vnd.ms-excel.numberformat:@ ' >"+ Dr[columnname] +"</td>"); } SW. WriteLine ("</tr>"); } SW. WriteLine ("</table>"); Sw. WriteLine ("</body>"); Sw. WriteLine (""); } }}
C # Export data to Excel-generic method classes