2011-10-01 25 views
6

devo esportare i dati in Visualizza come Excel, In realtà ho implementato, ma il mio dubbio è quando da usareEsportazione Excel file da visualizzare (MVC)

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

vs

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

e Come è possibile impostare Nome file scaricabile in ciascuno di questi metodi?

Esempio 1:

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

Esempio: 2

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

risposta

9

Si può leggere sulle differenze tra FileContentResult & FileResult qui: What's the difference between the four File Results in ASP.NET MVC

È possibile specificare il nome del file come questo

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"); 
Problemi correlati