ASP.NET MVC EPPlus Download Excel File

后端 未结 1 1861
渐次进展
渐次进展 2020-12-14 18:27

So I\'m using the fancy EPPlus library to write an Excel file and output it to the user to download. For the following method I\'m just using some test data to minimize on t

1条回答
  •  無奈伤痛
    2020-12-14 19:00

    Here's what I'm using - I've been using this for several months now and haven't had an issue:

    public ActionResult ChargeSummaryData(ChargeSummaryRptParams rptParams)
    {
        var fileDownloadName = "sample.xlsx";
        var contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    
        var package = CreatePivotTable(rptParams);
    
        var fileStream = new MemoryStream();
        package.SaveAs(fileStream);
        fileStream.Position = 0;
    
        var fsr = new FileStreamResult(fileStream, contentType);
        fsr.FileDownloadName = fileDownloadName;
    
        return fsr;
    }
    

    One thing I noticed right off the bat is that you don't reset your file stream position back to 0.

    0 讨论(0)
提交回复
热议问题