Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return excel file without saving it in the server inside controller

I want to return Excel file (using NPOI library) to user without the need to save the file in the server first. Here's my code :

        [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();
        }

Any help will be appreciated.

like image 965
warheat1990 Avatar asked Aug 19 '15 10:08

warheat1990


1 Answers

[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();
}
like image 93
holdenmcgrohen Avatar answered Oct 11 '22 01:10

holdenmcgrohen