加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 导出Excel文件以查看(MVC)

发布时间:2020-12-15 20:57:08 所属栏目:asp.Net 来源:网络整理
导读:我必须将数据导出为Excel,实际上我已实现,但我怀疑是什么时候 使用 return new FileContentResult(fileContents,"application/vnd.ms-excel"); VS return File(fileContents,"application/vnd.ms-excel"); 以及如何在每种方法中设置可下载的文件名? 例1: p
我必须将数据导出为Excel,实际上我已实现,但我怀疑是什么时候
使用
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");
}

解决方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读