【问题标题】:How to add a HttpResponse to a C# class [closed]如何将 HttpResponse 添加到 C# 类 [关闭]
【发布时间】:2016-11-29 17:45:07
【问题描述】:

我正在生成一个 Excel 文件,我想在我的 ASP.NET MVC 5 应用程序中像这样下载它:

var ef = new ExcelFile();
var ws = ef.Worksheets.Add("Worksheet 1");

ef.Save(this.Response, "asd.xlsx");

在最后一行中,由于没有 HttpResponse,它给出了错误,我怎样才能将 HttpResponse 添加到我的类中?谢谢。

【问题讨论】:

  • 不确定你的意思。你在哪个班?这段代码应该做什么?
  • HttpResponse Class 在System.Web 命名空间中可用,你的代码中导入了吗?
  • 第 1 步:下载一些东西,第 2 步:????,第 3 步:保存到 Excel ... 是我自己还是缺少什​​么?

标签: c# asp.net excel asp.net-mvc-5


【解决方案1】:

如果你想在 ASP.Net MVC5 中返回一个文件,你可以在你的操作中使用the File method

public FileStreamResult MyExcelFile()
{
    // Create your excel filestream

    // ...
    return File(fileStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
}

【讨论】:

    猜你喜欢
    • 2013-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-30
    • 2012-05-10
    • 1970-01-01
    相关资源
    最近更新 更多