openXML spreadsheetdocument return byte array for MVC file download

后端 未结 2 1622
无人及你
无人及你 2021-01-02 04:29

I\'m trying to return a openXML spreadsheetdocument as a byte[] which I can then use to allow my MVC to send that file to a user. here is my spreadsheetdocument method to re

相关标签:
2条回答
  • 2021-01-02 04:37

    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;
    
    0 讨论(0)
  • 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

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