导出到Excel的两种方法
第一种:
1、首先创建Excle模板,另存为 “xml”文件。使用记事本等编辑软件打开文件的代码。然后另存为视图文件“Export.cshtml”;
2、控制器操作
1 public ActionResult Export() 2 { 3 #region Excel下载设置 4 Response.Clear(); 5 Response.ClearContent(); 6 Response.Buffer = true; 7 Response.ContentEncoding = System.Text.Encoding.UTF8; 8 Response.ContentType = "application/ms-excel"; 9 string downloadFileName = "文件名" + ".xls"; 10 if (Request.UserAgent != null && Request.UserAgent.ToLower().IndexOf("msie", System.StringComparison.CurrentCultureIgnoreCase) > -1) 11 { 12 downloadFileName = HttpUtility.UrlPathEncode(downloadFileName); 13 } 14 if (Request.UserAgent != null && Request.UserAgent.ToLower().IndexOf("firefox", System.StringComparison.CurrentCultureIgnoreCase) > -1) 15 { 16 Response.AddHeader("Content-Disposition", "attachment;filename=\"" + downloadFileName + "\""); 17 } 18 else 19 Response.AddHeader("Content-Disposition", "attachment;filename=" + downloadFileName); 20 21 #endregion 22 return View(); 23 }