Problem saving designer worksheet as stream

I am trying to create a workbook from a desginer workbook. When I save the workbook as a Stream object and return it from a method. The stream is not null, but there are no bytes to read.

// snippet from method that creates workbook

WorkbookDesigner designer = new WorkbookDesigner();
designer.Open(@"C:\dev\designersample.xls");

Workbook workbook = designer.Workbook;
Worksheet summaryRASheet = workbook.Worksheets["Summary RA"];

summaryRASheet.Cells["A3"].PutValue(inRA.VendorSummary.CompanyHeader.Name);
summaryRASheet.Cells["A4"].PutValue(inRA.VendorSummary.CompanyHeader.Address1);

MemoryStream ms = new MemoryStream();
workbook.Save(ms, FileFormatType.Excel2003);
return ms;

When I try to read from the memory stream there are no bytes returned. I am using a simple MemoryStream.Read() call.

Any ideas what I am missing?

Jay Warling

Hi,

Thank you for considering Aspose.

Well, After saving the file to stream, the stream.Position is equal to stream.Length, so you are not able to read any data from the stream, please move the position to the begin.

See following codes:

MemoryStream ms = new MemoryStream();
workbook.Save(ms, FileFormatType.Excel2003);
ms.Seek(0, SeekOrigin.Begin);

Thank you & Best Regards,

Hi Nausherwan,

That was the problem. Thanks!

I did run into something else that wasn't obvious to me. While debugging I added another designer.Save() to a file, before my workbook.SaveToStream() call. What I didn't realize is the designer.Save() removed all worksheets from the designer instance. At least that was the behavior I saw.

Is that documented somewhere?

Thanks,

Jay

Hi Jay,

Yes, when you call Workbook.Save() method, all the objects (i.e.., worksheets, cells, data, other drawing objects) which were initialized previously, would be null/nothing. Well, it will boost the performance to some extent you know. Anyways, thanks for the suggestion, we will document it soon.

Thank you.