gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 21:05:45 25 4
gpt4 key购买 nike

我想将 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com