先來寫一段代碼,這段代碼也是我在網上找的,可是他那個原先有點問題,我對他那個進行了修改,如今這個代碼是我修改改過的,應該沒有問題的。html
1 public int StreamExport(System.Data.DataTable dt, List<string> ColNames, string fileName) 2 { 3 4 if (string.IsNullOrEmpty(fileName)) return 0; 5 6 StringBuilder content = new StringBuilder(); 7 StringBuilder strtitle = new StringBuilder(); 8 content.Append("<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'>"); 9 content.Append("<head><title></title><meta http-equiv='Content-Type' content=\"text/html; charset=gb2312\">"); 10 //注意:[if gte mso 9]到[endif]之間的代碼,用於顯示Excel的網格線,若不想顯示Excel的網格線,能夠去掉此代碼 11 content.Append("<!--[if gte mso 9]>"); 12 content.Append("<xml>"); 13 content.Append(" <x:ExcelWorkbook>"); 14 content.Append(" <x:ExcelWorksheets>"); 15 content.Append(" <x:ExcelWorksheet>"); 16 content.Append(" <x:Name>Sheet1</x:Name>"); 17 content.Append(" <x:WorksheetOptions>"); 18 content.Append(" <x:Print>"); 19 content.Append(" <x:ValidPrinterInfo />"); 20 content.Append(" </x:Print>"); 21 content.Append(" </x:WorksheetOptions>"); 22 content.Append(" </x:ExcelWorksheet>"); 23 content.Append(" </x:ExcelWorksheets>"); 24 content.Append("</x:ExcelWorkbook>"); 25 content.Append("</xml>"); 26 content.Append("<![endif]-->"); 27 content.Append("</head><body><table style='border-collapse:collapse;table-layout:fixed;'><tr>"); 28 for (int i = 0; i < ColNames.Count; i++) 29 { 30 31 content.Append("<td><b>" + ColNames[i] + "</b></td>"); 32 } 33 content.Append("</tr>\n"); 34 35 for (int j = 0; j < dt.Rows.Count; j++) 36 { 37 content.Append("<tr>"); 38 for (int k = 0; k < dt.Columns.Count; k++) 39 { 40 object obj = dt.Rows[j][k]; 41 Type type = obj.GetType(); 42 if (type.Name == "Int32" || type.Name == "Single" || type.Name == "Double" || type.Name == "Decimal") 43 { 44 double d = obj == DBNull.Value ? 0.0d : Convert.ToDouble(obj); 45 if (type.Name == "Int32" || (d - Math.Truncate(d) == 0)) 46 content.AppendFormat("<td style='vnd.ms-excel.numberformat:#,##0'>{0}</td>", obj); 47 else 48 content.AppendFormat("<td style='vnd.ms-excel.numberformat:#,##0.00'>{0}</td>", obj); 49 } 50 else 51 content.AppendFormat("<td style='vnd.ms-excel.numberformat:@'>{0}</td>", obj); 52 } 53 content.Append("</tr>\n"); 54 } 55 content.Append("</table></body></html>"); 56 content.Replace(" ", ""); 57 Response.Clear(); 58 Response.Buffer = true; 59 Response.ContentType = "application/vnd.ms-excel"; //"application/ms-excel"; 60 Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); 61 Response.Charset = "gb2312"; 62 fileName = System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8); 63 Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName); 64 Response.Write(content.ToString()); 65 Response.Output.Flush(); 66 //pages.Response.End(); //注意,若使用此代碼結束響應可能會出現「因爲代碼已通過優化或者本機框架位於調用堆棧之上,沒法計算表達式的值。」的異常。 67 HttpContext.Current.ApplicationInstance.CompleteRequest(); //用此行代碼代替上一行代碼,則不會出現上面所說的異常。 68 return 1; 69 }
下面來寫寫經過此次導出個人總結。ajax
首先我是第一次作將datatable導出成excel,並且對Jquery和asp.net都不太熟,徹底是個小白,在這個過程當中遇到不少問題,解決也花了很長一段時間。在這裏作個總結,給跟我同樣的小白點參考。app
(先說個題外話,一開始不是很懂的人看這段代碼可能會很奇怪拼接excel的格式爲何很相似html的語法,我也納悶,百度了一下發現原來html和excel的編碼是同樣的,後來去作別的系統的excel導出時發現同事用了拼接table的方法去組成excel也證實了這一點,並且table的格式在excel中也是通用的。)框架
1.導出的excel怎樣傳回html頁讓他下載?asp.net
一開始我用的是ajax提交表格,但後來怎麼點擊按鈕都沒有反應,查了半天才知道,ajax只能返回的數據只能是文本形式,而在上面代碼第63行也就是學習
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
已經把返回的值改爲了下載文件,因此ajax不能用了。後來又查到window.Location,這個很簡單,只要在原來html頁的方法里加上window.Localtion=「你的url」就能夠了,但另外一個問題出現了,點擊完按鈕以後個人整個頁面都處於加載狀態,並非我想要的結果。後來問了同事才知道,原來直接用a標籤就能夠解決怪本身以爲這個過高大上了a標籤應該不行,結果用了那麼多複雜的方法都沒成,沒想到最簡單的卻解決了問題。優化
2.如何在a標籤傳遞參數?ui
由於導出的excel要和根據不一樣條件查詢出來結果一致的,因此傳入的參數不是固定的,那麼a標籤的herf就不能在一開始就寫好了,採用a標籤的onclick方法,先把href初始值設成Javascript(0)(好像是這樣寫,我也忘了)讓它無路徑,再在方法中取得各標籤的值,而後再設置a標籤的href,這個問題就解決了。編碼
如今回想起來這些問題真的挺簡單的,但剛作的時候真的是耗費了很多時間,尤爲是本身在學校中歷來沒接觸過這些,不過作出來看到本身的代碼實現了各類功能真的仍是挺開心的!須要學習的地方太多了,要不斷學習,不斷進步!url