将Excel文件导出到视图(MVC) [英] Exporting Excel File to View (MVC)
本文介绍了将Excel文件导出到视图(MVC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须将数据导出为Excel,实际上我已经实现了,但是我怀疑何时 使用
I have to Export Data to View as Excel , Actually I have Implemented,but my doubt is when to use
return new FileContentResult(fileContents, "application/vnd.ms-excel");
与
return File(fileContents, "application/vnd.ms-excel");
以及如何在每种方法中设置可下载文件名"?
and How can set Downloadable Filename in each of this methods?
示例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在这里: ASP.NET MVC中有四个文件结果
You can read about the differences between FileContentResult & FileResult here : 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");
这篇关于将Excel文件导出到视图(MVC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文