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 3 years, 6 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #8618

    paulsm4
    Participant

    I’m creating an Excel spreadsheet programmatically with PowerTools.

    I installed PowerTools 4.5.3.2 in my C#/ASP.Net Core with Nuget.

    I can generate the spreadsheet OK.

    BUT …

    I would like to pass the completed spreadsheet back to the user as a .Net MemoryStream.

    I’ve pored over the PowerTools documentation and sample programs on https://github.com/OfficeDev/Open-Xml-PowerTools.

    But for the life of me, I haven’t seen any way to have PowerTools give me the generated data as a MemoryStream. Or, for that matter, any way to make PowerTools use a MemoryStream I pass in to it.

    Q: Any suggestions?

    #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 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.