File size reduced many times when save with SaveToStream() at second time

Hi Aspose team,

I’m using Aspose Cells, Version 22.1
When I remove macro in xls file, output file size decrease a little (246KB -> 219KB). After that, I re-use this process for the output file, the file size is reduced from 219KB to 28KB. I see stream capacity and stream size at second time is many times lower
Why file size is reduced so many times? Is data lost in SaveToStream() ?
I tried replacing with workbook.Save(outputXlsFile) but received the same result.

Here is my current code:

        using(var inStream = new FileStream(inputXlsFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {
            var opt = new LoadOptions
            {
                MemorySetting = MemorySetting.MemoryPreference
            };

            workbook = new Workbook(inStream, opt);
            string nameOfSheet = string.Empty;
            foreach(var wooksheet in workbook.Worksheets)
            {
                if(wooksheet.IsVisible)
                {
                    nameOfSheet = wooksheet.Name;
                    break;
                }
            }

            if(workbook.HasMacro)
            {
                workbook.RemoveMacro();
            }

            bool hasVisibleWorksheet = false;
            foreach(var worksheet in workbook.Worksheets)
            {
                if(worksheet.IsVisible)
                {
                    hasVisibleWorksheet = true;
                    break;
                }
            }
            if(!hasVisibleWorksheet)
                workbook.Worksheets.Add(nameOfSheet);

            Stream stream = workbook.SaveToStream();
            using(var cf = new OpenMcdf.CompoundFile(stream))
            {
                cf.Save(outputXlsFile);
            }
        }

Attached sample below. Please help me check. Thanks!
Sample.zip (391.2 KB)
image.png (9.0 KB)

@dunghnguyen,

Please notice, after an initial test, I am able to reproduce the issue as you mentioned. I found output file size reduced considerably when saving the XLS file with SaveToStream()/Save at second time. Apparently, if there is no issue with the output file, then you should not be worry with the size of the output file which is reduced rather than increased. Anyways, to investigate your issue in details, I have logged a ticket with an id “CELLSNET-50706” for your issue. We will look into it soon.

Once we have an update on it, we will let you know.

@dunghnguyen
If you call Workbook.RemoveMacro(), a lot of BIFF4Macro worksheets will be removed because they are part of the macro.
However, when the worksheet was deleted, the collected pictures were not cleaned up. When loading and saving again, we cleaned up these unused pictures, so the file became smaller.
We will clean up unused images when deleting the worksheet

@simon.zhao,
Thanks for the explanation.
I can accept file size reduction, but I want it just happen at the first time, not at second.
Can you clean up unused images when deleting the worksheet at the first time?

Yes, Aspose.Cells will do this when deleting the worksheet first time.

@Amjad_Sahi
If Aspose.Cells release newer version (has solved this problem), please let me know.
Thank you for supporting!

@dunghnguyen,

The fix will be included in our upcoming release, i.e., Aspose.Cells for .NET v22.4 which is due in the second week of April, 2022. You will also be notified once the next version is published.

The issues you have found earlier (filed as CELLSNET-50706) have been fixed in this update. This message was posted using Bugs notification tool by johnson.shi