Export Excel file for viewing (MVC)

I need to export data for viewing as Excel, I actually implemented it, but I doubt when to use

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

against

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

and How to set the downloadable file name in each of these methods?

Example 1:

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

Example: 2

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

You can read about the differences between FileContentResult and FileResult here: What is the difference between the four file results in ASP.NET MVC

You can specify the file name as

 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"); 
+9
source

Source: https://habr.com/ru/post/898464/


All Articles