Error SpreadML saved to MemoryStream

Is there an issue with SpreadML saving to a memory stream -- versus Excel2003? And then I noticed that CSV had an issue until the fix mentioned in <A href="</A></P> <P>The code below does not work.</P> <P>{... missing code}</P> <P>MemoryStream</FONT><FONT size=2> ms = </FONT><FONT color=#0000ff size=2>new</FONT><FONT size=2> </FONT><FONT color=#008080 size=2>MemoryStream</FONT><FONT size=2>();</P> <P>excel.Save(ms, </FONT><FONT color=#008080 size=2>FileFormatType</FONT><FONT size=2>.SpreadsheetML);</P> <P></FONT><FONT color=#008080 size=2>FileStream</FONT><FONT size=2> fs = </FONT><FONT color=#0000ff size=2>new</FONT><FONT size=2> </FONT><FONT color=#008080 size=2>FileStream</FONT><FONT size=2>(</FONT><FONT color=#800000 size=2>"d:\\downloads\\test.xls", FileMode.Create, FileAccess.ReadWrite);

ms.WriteTo(fs);

----------------------------------------------------

returns this error (similiar to the old CSV issue.)

at System.IO.__Error.StreamIsClosed()
at System.IO.MemoryStream.WriteTo(Stream stream)
at yada, yada...

----------------------------------------------------

But this code DOES work.

{... missing code}

MemoryStream ms = new MemoryStream();

excel.Save(ms, FileFormatType.CVS);

FileStream fs = new FileStream("d:\\downloads\\test.xls", FileMode.Create, FileAccess.ReadWrite);

ms.WriteTo(fs);

AND so does this.

{... missing code}

MemoryStream ms = new MemoryStream();

excel.Save(ms, FileFormatType.Excel2003);

FileStream fs = new FileStream("d:\\downloads\\test.xls", FileMode.Create, FileAccess.ReadWrite);

ms.WriteTo(fs);

Please try this attached version.

Yes, thank you that version fixed the code as I had presented. We had version 3.7 and I see this attached file is version 4.0.2.5.

Unfortunately, the code I presented was just the test you suggested to the CSV post, thus why I used that code to demonstrated the issue and avoid additional thread exchanges. Our actual code, which still fails with 4.0.2.5 if FileFormatType.SpreadML but not Excel2003, is the following:

MemoryStream ms = new MemoryStream();

_excel.Save(ms, FileFormatType.SpreadsheetML);

StreamReader sr = new StreamReader(ms);

string spreadML = sr.ReadToEnd();

However, even with the older version 3.7, we found that this DOES work.

MemoryStream ms = new MemoryStream();

_excel.Save(ms, FileFormatType.SpreadsheetML);

string spreadML = Encoding.ASCII.GetString(ms.GetBuffer());

I'm not sure if this is an encoding issue or "stream" issue with DotNet itself or Aspose.Cells. Meaning that I would not be surprised that Aspose.Cells is using the passed in MemoryStream to help feed a XmlTextWriter or something in the generation of the SpreadML -- thus is reading the passed in stream (my stream) to the end and subsequently closing it. So when the caller (me) goes to use the stream it's already at the end of the stream and closed (by default.) My reasoning is that if you call the buffer on the "saved to" stream the data is there but if try to access the SpreadML memoryStream it's closed -- whereas the Excel2003 is not.

In any case, we coded around this and are moving forward. Thanks.

Yes, you are right. Previously we wrote to the stream to end and close it. Now we don't close it but still write to the end. So please change your code to:

excel.Save(ms, FileFormatType.SpreadsheetML);

ms.Seek(0, SeekOrigin.Begin);

StreamReader sr = new StreamReader(ms);

string spreadML = sr.ReadToEnd();