【问题标题】:Exporting Excel File to View (MVC)将 Excel 文件导出到视图 (MVC)
【发布时间】:2011-11-29 05:32:43
【问题描述】:

我必须导出数据以查看 Excel,实际上我已经实现了,但我怀疑什么时候 使用

return new FileContentResult(fileContents, "application/vnd.ms-excel");

对比

return File(fileContents, "application/vnd.ms-excel");

以及如何在每种方法中设置可下载文件名?

示例 1:

public ActionResult ExcelExport()
{
   byte[] fileContents = Encoding.UTF8.GetBytes(data);
   return new FileContentResult(fileContents, "application/vnd.ms-excel");
}

示例:2

public ActionResult ExcelExport()
{
   byte[] fileContents = Encoding.UTF8.GetBytes(data);
   return File(fileContents, "application/vnd.ms-excel");
}

【问题讨论】:

    标签: asp.net-mvc export-to-excel


    【解决方案1】:

    您可以在此处了解 FileContentResult 和 FileResult 之间的区别:What's the difference between the four File Results in ASP.NET MVC

    你可以像这样指定文件名

    return new FileContentResult(fileContents, "application/vnd.ms-excel") { FileDownloadName = "name.xls" };
    
    // or
    
    // note that this call will return a FileContentResult object
    return new File(fileContents, "application/vnd.ms-excel", "name.xls");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-05
      相关资源
      最近更新 更多