SaveToStream on a workbook with at least 257 columns generates corrupt data

If you call SaveToStream on a workbook that contains at least 257 columns, the returned stream contains corrupt data. If you create a new workbook from the stream, this workbook will not contain the correct values. In addition, all ranges containing at least 257 columns are corrupt. If you save the workbook in a file instead of a stream, the bug does not occur. I reprodcued this bug with version 25.4.0

    using Workbook saveWorkbook = new();
    Worksheet sheet = saveWorkbook.Worksheets.First();
    sheet.Cells[0, 256].Value = 1;
    
    using MemoryStream saveStream = saveWorkbook.SaveToStream();
    byte[] bytes = saveStream.ToArray();
    saveStream.Close();
    
    using MemoryStream readStream = new(bytes);
    using Workbook readWorkbook = new Workbook(readStream);
    readStream.Close();
    object value = readWorkbook.Worksheets.First().Cells[0, 256].Value; // value is null instead of 1.
    Range readRange = readWorkbook.Worksheets.CreateRange("A1:IW1", 0);
    string address = readRange.Address; // throws NullReferenceException

@Nic0
saveWorkbook.SaveToStream() is called, an xls file is saved.
So the max column is 255 in xls file, so Range “A1:IW1” can not be created.

@Nic0,

Please try using Workbook.Save(stream, SaveFormat.Xlsx) method instead to save the Excel workbook to streams. Please replace the following lines of code from your code segment:

using MemoryStream saveStream = saveWorkbook.SaveToStream();
byte[] bytes = saveStream.ToArray();
saveStream.Close();

with:

using MemoryStream saveStream = new MemoryStream();
saveWorkbook.Save(saveStream, Aspose.Cells.SaveFormat.Xlsx); 
saveStream.Position = 0;
byte[] bytes = saveStream.ToArray();
saveStream.Close();

Hope, this helps a bit.

This works. Thank you very much!

@Nic0,

You are welcome. I’m glad the suggested code segment meets your needs. If you have any further questions or comments, please don’t hesitate to reach out to us anytime.