c# mvc FileResult 导出excel
2014-11-04 22:58
435 查看
后台代码
前台代码
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"); }
前台代码
<a href="/Home/ExportExcel">test</a>
相关文章推荐
- Asp.Net MVC 使用FileResult导出Excel数据文件
- Asp.Net MVC 使用FileResult导出Excel数据文件
- MVC 使用FileResult导出Excel数据文件
- Asp.Net MVC 使用FileResult导出Excel数据文件
- Asp.Net MVC 使用FileResult导出Excel数据文件
- Asp.Net MVC 使用FileResult导出Excel数据文件
- MVC导出数据到EXCEL新方法:将视图或分部视图转换为HTML后再直接返回FileResult
- MVC导出数据到EXCEL新方法:将视图或分部视图转换为HTML后再直接返回FileResult
- Asp.Net MVC 使用FileResult导出Excel数据文件
- Asp.Net MVC 使用FileResult导出Excel数据文件
- MVC导出数据到EXCEL新方法:将视图或分部视图转换为HTML后再直接返回FileResult
- 使用FileResult导出Excel数据文件
- 【C#MVC】使用ajaxFileUpload导入Excel,并显示在Grid中
- C# MVC .Net NPOI实现excel导出功能
- 使用第三方.Net NPOI实现.net mvc使用FileResult导出excel(下载到客户端)
- MVC 使用FileResult导出excel文件实例
- ASP.NET MVC 导出CSV 的 CsvFileResult 类
- MVC下用C#实现Excel导出
- ASP.NET MVC 将IList<T>导出Excel文档的泛型类(继承自ActionResult)
- NET MVC FileResult 导出/下载 文件/Excel