导出到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         }
View Code

相关文章:

  • 2021-11-24
  • 2022-12-23
  • 2022-12-23
  • 2021-12-02
  • 2021-08-28
  • 2021-08-30
  • 2022-01-02
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-02
  • 2022-12-23
  • 2022-02-26
  • 2022-12-23
相关资源
相似解决方案