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! ...
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);