save feature

Nov 26, 2010 at 5:42 PM

Is it possible to allow the save to go to a memory stream and not just a file?

Have not looked at the source code yet to see if it can be done.

-Dave

Nov 26, 2010 at 6:22 PM

Maybe I should elaborate? 

I am trying to build a web app and I have to direct the output to HttpResponse.  The current problem is the save puts itself on the server which is not good.  Typically I use a memory stream of one kind or another to deal with the output.

-Dave

Nov 26, 2010 at 6:53 PM

:/  Sorry about the repetiveness of my posts.  I downloaded the code, I did not think this was going to be as simple.  There probably is a better way to do this such as overloading... ?  But I do not know C# very well.

XLWorkbook.cs

public void SaveAs_MS(MemoryStream ms) {  CreatePackageMS(ms); }

XLWorkbook_Save.cs

    private void CreatePackageMS(MemoryStream ms)
        {
            using (SpreadsheetDocument package = SpreadsheetDocument.Create(ms, SpreadsheetDocumentType.Workbook))
            {
                RelId.GetNext(RelType.Worksheet);
                CreateParts(package);
            }
        }

My code (using VB), I made a button and called the following code.

        Dim workbook As New xlworkbook
        Dim worksheet = workbook.Worksheets.Add("Sample Sheet")
        worksheet.Cell("B1").Value = 5
        worksheet.Cell("C2").Value = "Hello World"
        worksheet.Cell(1, 5).FormulaA1 = "=1+1"

        Dim ms As New MemoryStream
        workbook.SaveAs_MS(ms)

        Dim response As System.Web.HttpResponse = System.Web.HttpContext.Current.Response

        response.ClearHeaders()
        response.ClearContent()
        response.Clear()
        response.Buffer = True
        response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"
        response.AddHeader("content-disposition", "attachment; filename=" & Now.ToString("yyyyMMdd-HHmmss") & "-TestReport" & ".xlsx")
        response.Charset = ""
        ms.WriteTo(response.OutputStream)
        response.Flush()
        response.End()

 

Anyway, just giving back... but please make the code correct ... I did not overload so I know what I am providing is somewhat incorrect.



Coordinator
Nov 26, 2010 at 8:32 PM
Edited Dec 4, 2010 at 2:19 AM

As of v0.33 ClosedXML can read/write from/to a stream.

Thank you for using this API, I really appreciate your input.