2011-10-01 74 views
6

我必须导出数据以查看Excel中,其实我已经实现了,但我的疑问是,当 使用导出Excel文件来查看(MVC)

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

VS

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"); 
} 

回答

9

您可以阅读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");