【问题标题】:how to download a xls file generated by NPOI in ASP MVC如何在 ASP MVC 中下载 NPOI 生成的 xls 文件
【发布时间】:2015-05-08 22:13:59
【问题描述】:

我找到了一个如何将 Excel 电子表格流式传输回客户端但在 aspx 代码中的示例。代码如下

using (var exportData = new MemoryStream())
{
workbook.Write(exportData);
string saveAsFileName = string.Format("MembershipExport-{0:d}.xls",  DateTime.Now).Replace("/", "-");
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", saveAsFileName));
Response.Clear();
Response.BinaryWrite(exportData.GetBuffer());
Response.End();
}

我正在使用 asp MVC 5 和 webApi 控制器。我想将此代码迁移到返回 HttpResponseMessage 的 WebApiController。 请问有什么建议吗?

【问题讨论】:

    标签: c# asp.net-mvc asp.net-web-api npoi


    【解决方案1】:

    这会有帮助吗?

    public FileResult DownloadFile()
    {
        // code to create workbook 
        using (var exportData = new MemoryStream())
        {
            workbook.Write(exportData);
            string saveAsFileName = string.Format("MembershipExport-{0:d}.xls", DateTime.Now).Replace("/", "-");
    
            byte[] bytes = exportData.ToArray();
            return File(bytes, "application/vnd.ms-excel", saveAsFileName);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-02-07
      • 2014-11-15
      • 1970-01-01
      • 1970-01-01
      • 2015-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多