Asp.Net MVC 使用FileResult导出Excel数据文件
生活随笔
收集整理的這篇文章主要介紹了
Asp.Net MVC 使用FileResult导出Excel数据文件
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
前幾天一個(gè)MVC3.0項(xiàng)目做了一個(gè)Excel導(dǎo)出功能,今天來記錄一下. 采取了最簡(jiǎn)單的方法.用的是Html拼接成Table表格的方式,返回 FileResult?輸出一個(gè)二進(jìn)制的文件.
第一種:使用FileContentResult
//// 摘要:// 通過使用文件內(nèi)容,內(nèi)容類型,文件名稱創(chuàng)建一個(gè)FileContentResult對(duì)象//// 參數(shù):// fileContents:// 響應(yīng)的二進(jìn)制文件內(nèi)容//// contentType:// 內(nèi)容類型(MIME類型)//// fileDownloadName:// 顯示在瀏覽器下載窗口的文件名稱//// 返回結(jié)果:// 文件內(nèi)容對(duì)象.protected internal virtual FileContentResult File(byte[] fileContents, string contentType, string fileDownloadName);需要將文件內(nèi)容轉(zhuǎn)化成字節(jié)數(shù)組byte[]
byte[] fileContents = Encoding.Default.GetBytes(sbHtml.ToString());?
第二種:使用FileStreamResult
// 其他參數(shù)描述同F(xiàn)ileContentResult// 參數(shù):// fileStream:// 響應(yīng)的流// // 返回結(jié)果:// 文件流對(duì)象.protected internal virtual FileStreamResult File(Stream fileStream, string contentType, string fileDownloadName);需要將文件內(nèi)容轉(zhuǎn)化成流
var fileStream = new MemoryStream(fileContents);?
第三種:使用FilePathResult
// 其他參數(shù)描述同F(xiàn)ileContentResult// 參數(shù):// fileName:// 響應(yīng)的文件路徑//// 返回結(jié)果:// 文件流對(duì)象.protected internal virtual FilePathResult File(string fileName, string contentType, string fileDownloadName);服務(wù)器上首先必須要有這個(gè)Excel文件,然會(huì)通過Server.MapPath獲取路徑返回.
具體詳情請(qǐng)看代碼.
ExportExcel Code 1 public FileResult ExportExcel() 2 { 3 var sbHtml = new StringBuilder(); 4 sbHtml.Append("<table border='1' cellspacing='0' cellpadding='0'>"); 5 sbHtml.Append("<tr>"); 6 var lstTitle = new List<string> { "編號(hào)", "姓名", "年齡", "創(chuàng)建時(shí)間" }; 7 foreach (var item in lstTitle) 8 { 9 sbHtml.AppendFormat("<td style='font-size: 14px;text-align:center;background-color: #DCE0E2; font-weight:bold;' height='25'>{0}</td>", item); 10 } 11 sbHtml.Append("</tr>"); 12 13 for (int i = 0; i < 1000; i++) 14 { 15 sbHtml.Append("<tr>"); 16 sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", i); 17 sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>屌絲{0}號(hào)</td>", i); 18 sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", new Random().Next(20, 30) + i); 19 sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", DateTime.Now); 20 sbHtml.Append("</tr>"); 21 } 22 sbHtml.Append("</table>"); 23 24 //第一種:使用FileContentResult 25 byte[] fileContents = Encoding.Default.GetBytes(sbHtml.ToString()); 26 return File(fileContents, "application/ms-excel", "fileContents.xls"); 27 28 //第二種:使用FileStreamResult 29 var fileStream = new MemoryStream(fileContents); 30 return File(fileStream, "application/ms-excel", "fileStream.xls"); 31 32 //第三種:使用FilePathResult 33 //服務(wù)器上首先必須要有這個(gè)Excel文件,然會(huì)通過Server.MapPath獲取路徑返回. 34 var fileName = Server.MapPath("~/Files/fileName.xls"); 35 return File(fileName, "application/ms-excel", "fileName.xls"); 36 } 超強(qiáng)干貨來襲 云風(fēng)專訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生總結(jié)
以上是生活随笔為你收集整理的Asp.Net MVC 使用FileResult导出Excel数据文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt官方开发环境生成的exe发布方式--
- 下一篇: Quick-Cocos2d-x初学者游戏