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