danielgindi/SpreadsheetStreams.net

How to Finish() without closing stream

Closed this issue · 2 comments

Hi,

I have problem/question with this library.

We have a scenario creating spreedsheet on MemoryStream and after using Finish() on ExcelSpreadsheetWriter causes Stream to Close. I would like to pass it to the response in WCF message but only way i found is to copy stream to new stream which causes 2x amount of memory for this operation.

I tried looking in to code and i found two (maybe) possible options:

  1. I found
    if (outputStream.GetType().FullName == "System.Web.HttpResponseStream") outputStream = (Stream) new WriteStreamWrapper(outputStream);
    so i thought it will resolve my problem but it also closes the stream.
  2. I found that PackageWriteStream which uses ZipArchive which on closing causes Dispose() which in turn closes the stream. But there is an optionleaveOpen
  3. Write Custom implementation of Stream that would allow copying underlying Buffer and Length to new Stream which won't allocate new Buffer

And now i'm thinking if i am doing something wrong or maybe i should propose some implementation

  1. The MemoryStream is essentially a byte[]. So you could take it through .ToArray() and write it to your response stream.
  2. You could pass Response.OutputStream directly to the spreadsheet writer.
  3. You could open a PR that adds a flag telling it to not close the underlying stream :)

Hi,

  1. as i wrote earlier this is not acceptable due to new allocation of memory and sometimes those files are around 50 mb, so it will be allocated on LOH which GC will slow down whole process.
  2. I'm not sure to what kind Response You are referring to. I have WCF not WebAPI or MVC so HttpContext is not available here.
  3. I Think this is probably best way, ill make PR soon. Are there any UnitTests ?