我必须将数据导出以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");
}

最佳答案

您可以在这里阅读有关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");

10-06 03:16