We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Issue with save method and stream parameter

Hello, we need to save a workbook into a stream. If we use the save method with stream param, the stream data size is always ~4KB? If we use the save methode with param FileName then it works as expected. It doesn't work within stream.

Examplecode:
...
Workbook workbook = new Workbook();
fstream = new FileStream(@"C:\Temp\origin.xls", FileMode.Open);
workbook.Open(fstream,FileFormatType.Excel2003);
MemoryStream stream = new MemoryStream();
workbook.Save(@"C:\Temp\generated\test.xls", FileFormatType.Excel2003); // Works! It creates a new file (copy of origin.xls)
workbook.Save(stream, FileFormatType.Excel2003);
byte[] buffer = new byte[(int)stream.Length];
buffer = stream.ToArray();
File.WriteAllBytes(@"C:\Temp\generated\FromStream.xls", buffer); // 4KB??? Doesn't work!
...

Please check your save method with stream .

Thanks in advance.

Manfred Daeppen

Hi, <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for considering Aspose.

Well, the issue is occurring because after workbook.Save(@"C:\Temp\generated\test.xls", FileFormatType.Excel2003); the workbook object becomes null and you need to re-open the file again to save it with the stream. Please see the updated sample code.

Sample Code:

Workbook workbook = new Workbook();

FileStream fstream = new FileStream(@"C:\Temp\origin.xls", FileMode.Open);

workbook.Open(fstream, FileFormatType.Excel2003);

MemoryStream stream = new MemoryStream();

workbook.Save(@"C:\Temp\generated\test.xls", FileFormatType.Excel2003);

fstream.Close();

fstream = new FileStream(@"C:\Temp\origin.xls", FileMode.Open);

workbook.Open(fstream, FileFormatType.Excel2003);

workbook.Save(stream, FileFormatType.Excel2003);

stream.Seek(0, SeekOrigin.Begin);

byte[] buffer = new byte[(int)stream.Length];

buffer = stream.ToArray();

File.WriteAllBytes(@"C:\Temp\generated\FromStream.xls", buffer);

Thank you & Best Regards,

G'day,

The line stream.Seek(0, SeekOrigin.Begin); solved the problem. Thanks for your quick support.

Greets Manfred