openXML spreadsheetdocument return byte array for MVC file download

天涯浪子 提交于 2019-11-30 18:29:30

I know that this is late, but I believe it is because you are not calling close on your spreadsheet document after you are finished with it. Make sure you close the document before returning the memory stream to commit all underlying streams used by the document.

// code omitted for clarity...
workbookpart.Workbook.Save();
spreadsheetDocument.Close();
return mem.ToArray()

http://msdn.microsoft.com/en-gb/library/documentformat.openxml.packaging.spreadsheetdocument(v=office.14).aspx

I had a similar problem with a Word document. I solved this by using the byte array outsite the using, like:


byte[] returnBytes = null;
using (MemoryStream mem = new MemoryStream())
  {
    // your code
    returnBytes = mem.ToArray();
  }

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