MVC 使用FileResult导出excel文件实例
2015-12-21 16:18
375 查看
public FileResult ExportExcel() { var sbHtml = new StringBuilder(); sbHtml.Append("<table border='1' cellspacing='0' cellpadding='0'>"); sbHtml.Append("<tr>"); var lstTitle = new List<string> { "编号", "姓名", "年龄", "创建时间" }; foreach (var item in lstTitle) { sbHtml.AppendFormat("<td style='font-size: 14px;text-align:center;background-color: #DCE0E2; font-weight:bold;' height='25'>{0}</td>", item); } sbHtml.Append("</tr>"); for (int i = 0; i < 1000; i++) { sbHtml.Append("<tr>"); sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", i); sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>屌丝{0}号</td>", i); sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", new Random().Next(20, 30) + i); sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", DateTime.Now); sbHtml.Append("</tr>"); } sbHtml.Append("</table>"); //第一种:使用FileContentResult byte[] fileContents = Encoding.Default.GetBytes(sbHtml.ToString()); return File(fileContents, "application/ms-excel", "fileContents.xls"); //第二种:使用FileStreamResult var fileStream = new MemoryStream(fileContents); return File(fileStream, "application/ms-excel", "fileStream.xls"); //第三种:使用FilePathResult //服务器上首先必须要有这个Excel文件,然会通过Server.MapPath获取路径返回. var fileName = Server.MapPath("~/Files/fileName.xls"); return File(fileName, "application/ms-excel", "fileName.xls"); }
文章转载自: MVC 使用FileResult导出excel文件实例 http://www.studyofnet.com/news/1119.html
相关文章推荐
- 分享微信开发Html5轻游戏中的几个坑
- Zend的MVC机制使用分析(二)
- ASP.NET MVC 4 捆绑和缩小实例介绍
- ASP.NET MVC中将控制器分离到类库的实现
- asp.net实现在非MVC中使用Razor模板引擎的方法
- ASP.NET MVC中的AJAX应用
- 为ASP.NET MVC及WebApi添加路由优先级
- ASP.NET MVC中图表控件的使用方法
- ASP.NET MVC的四种验证编程方式
- ASP.NET MVC 3仿Server.Transfer效果的实现方法
- 如何在MVC应用程序中使用Jquery
- ASP.NET MVC小结之基础篇(二)
- ASP.NET小结之MVC, MVP, MVVM比较以及区别(一)
- Asp.net实现MVC处理文件的上传下载功能实例教程
- ASP.NET MVC小结之基础篇(一)
- 12种JavaScript常用的MVC框架比较分析
- 浅析Asp.net MVC 中Ajax的使用
- JSP使用MVC模式完成删除和修改功能实例详解
- SpringMVC框架下JQuery传递并解析Json格式的数据是如何实现的
- Flex中最好的MVC框架Mate框架