asp.net-mvc - 返回excel文件而不将其保存在 Controller 内的服务器中

标签 asp.net-mvc excel asp.net-mvc-5 npoi

我想将 Excel 文件(使用 NPOI 库)返回给用户,而无需先将文件保存在服务器中。这是我的代码:

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Report(SalesReportViewModel model)
        {
            if (ModelState.IsValid)
            {
                XSSFWorkbook wb = context.GetReport(model);
                //I have no idea what to return after I got my wb
            }

            return View();
        }

任何帮助将不胜感激。

最佳答案

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Report(SalesReportViewModel model)
{
    if (ModelState.IsValid)
    {
        XSSFWorkbook wb = context.GetReport(model);

        byte[] fileContents = null;
        using (var memoryStream = new MemoryStream())
        {
            wb.Write(memoryStream);
            fileContents = memoryStream.ToArray();
        }

        return File(fileContents, System.Net.Mime.MediaTypeNames.Application.Octet, "file.xlsx");
    }

    return View();
}

关于asp.net-mvc - 返回excel文件而不将其保存在 Controller 内的服务器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32093297/

相关文章:

c# - 在 ASP.NET MVC Core 应用程序 (RC2) 中显示项目版本

asp.net-mvc - 如何在提交操作中获取所有列表框项目

vba - 使用带有 ? 的字符串来自 Visual Basic 中的 Excel 单元格内容

java - 使用 Apache poi 和电子邮件验证生成 excel 工作表

vba - 从另一个工作簿中的范围更改多个工作表中单元格的值

javascript - Braintree 沙盒帐户和 Paypal 集成

c# - IIS Express 工作进程不释放程序集

javascript - 返回 false 不适用于 MVC post

jquery - 通过 AJAX 而不是回发进行模型绑定(bind)复选框时出现 MVC 3 问题

c# - 我添加了一个用户并将他分配给一个角色,当我使用 Membership.GetAllUsers() 时它没有返回