我想将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();
}
任何帮助将不胜感激。
答案 0 :(得分:3)
[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();
}
答案 1 :(得分:1)
这个堆栈溢出问题有你的答案。 How to download memory stream object via angularJS and webaAPI2
std::unique_ptr