Free Support Forum - aspose.com

When saving Excel file to streams with Aspose.Cells an exception occurred in .NET

Hi,

I’m using the following code to convert Excel file to CSV:

Workbook workbook = new Workbook();
workbook.Open(filePath);
using (MemoryStream ms = new MemoryStream()) {
workbook.Save(ms, FileFormatType.CSV);
return Utilities.ReadStreamToString(ms);
}

However when calling workbook.Save() I get an error saying:

System.ObjectDisposedException: Cannot access a closed Stream. at
System.IO.__Error.StreamIsClosed() at System.IO.MemoryStream.WriteTo(Stream
stream) at Aspose.Cells.Worksheets.֩(Stream Ԏ, FileFormatType ֪) at
Aspose.Cells.Worksheets.֫(Stream Ԏ, FileFormatType ֪) at
Aspose.Cells.Workbook.Save(Stream stream, FileFormatType fileFormatType)

Please help me to understand if I do anything wrong.

Thanks & Regards,
Mike




Sorry for this problem. Please try this attached fix. It's solved.

Thank you, Laurence for this fix. It’s working.

Now I noticed another bug. When I call workbook.Save(…) it saves only the first worksheet. I would expect that all sheet in workbook are saved (since I call workbook’s method). Another solution would be to call Save() for each worksheet which is good as well, however worksheet object does not expose any Save() method.

Any ideas?

Best Regards,
Mike

Hi Mike,

That's the routine of MS Excel. Only data the first worksheet can be saved in CSV file.

I will check how to allow you save data in other worksheet. Maybe a Save method in Worksheet object or other solution. I will spend a few days on this feature.

Hi Laurence,

You are not absolutely right. MS Excel saves the data from Active Worksheet. Thus, by looping thru all sheets in MS Excel I can have the whole workbook saved. This method doesn’t work with Aspose.Cells library. Even if I change ActiveSheetIndex the library anyway saves the first worksheet.

Yours,
Mike

Yeah, you are right.

I fixed it to save the active worksheet. Please try this attached version.

But if you are using an evaluation copy, the saved worksheet is still the first worksheet. Only when you use a license copy, you can use this feature.