Error while creating open XML excel sheet from ASP.NET MVC

老子叫甜甜 提交于 2019-12-03 20:57:01

Try resetting your memory stream position to zero before you copy it to response:

// Close the document.
spreadsheetDocument.Close();
mem.Position = 0;
mem.CopyTo(response.OutputStream);

Alternatively to copying memory stream to response, you can return FileStreamResult from your MVC Action:

public ActionResult XlsDocument()
{
    // generating your document here...

    // very important to reset memory stream to a starting position, otherwise you would get 0 bytes returned
    memoryStream.Position = 0;

    var resultStream = new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";);
    resultStream.FileDownloadName = String.Format("{0}.{1}", fileName, extension);

    return resultStream;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!