OpenXML PowerTools: is it possible to write an .xlsx to a .Net MemoryStream?

Home Forums Open-Xml-PowerTools OpenXML PowerTools: is it possible to write an .xlsx to a .Net MemoryStream?

This topic contains 2 replies, has 2 voices, and was last updated by  Anonymous 4 years, 8 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #8619

    paulsm4
    Participant

    CONTEXT:

    All I want to do is:

    1. My ASP.Net web controller calls a function that generates an .xlsx spreadsheet from scratch.
    2. The function uses OpenXML PowerTools (because that seems the most straightforward way to include a Pivot Table in the .xlsx)
    3. Once the function returns, the controller needs to return the .xlsx back to the web client as an Asp.Net Core FileStream object.

    In other words, all I want to do is go from 2 (working) to 3 (that’s where I’m stuck). I can’t seem to “convert” the successfully created spreadsheet into a MVC FileStream-compatible object for the controller to pass back to the caller.

    #9785

    Anonymous

    No, it doesn’t appear that OpenXml PowerTools will let you write out to anything but an actual disk file.

    So I used a temp file, then converted it to a byte[] array to pass back to the caller:

    using (OpenXmlMemoryStreamDocument streamDoc = OpenXmlMemoryStreamDocument.CreateSpreadsheetDocument())
    {
    doc = streamDoc.GetSpreadsheetDocument();
    ms = new MemorySpreadsheet();

    string tmpFile = System.IO.Path.GetTempFileName();
    streamDoc.GetModifiedSmlDocument().SaveAs(tmpFile);
    byte[] fileBytes = System.IO.File.ReadAllBytes(tmpFile);
    return fileBytes;

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.