RemoveUnusedStyles() function in Excel Open XML file

Hi,

I have a issue with function RemoveUnusedStyles() of Aspose Cells .NET. After calling this function and saving to output file, I load this file, 5 styles will be added to workbook. And, calling this function 1 time cannot remove all of unused styles completely.
Sample code:

var workbook = new Workbook(outputFile);
MemoryStream memStream = new MemoryStream();
int count = workbook.CountOfStylesInPool;
bool isRemoved = false;
do
{
    workbook.RemoveUnusedStyles();
    workbook.Save(memStream, SaveFormat.Xlsx);
    if(workbook.CountOfStylesInPool == count)
        break;
    else
        isRemoved = true;
    count = workbook.CountOfStylesInPool;
}
while(true);
workbook.Save(outputFile);
...

var workbook = new  Workbook(outputFile);
int count = workbook.CountOfStylesInPool;

And can I check CountOfStylesInPool if do not save workbook?
Here is sample file: sample.zip (862.3 KB)

Please check and help me with this issue.

Thanks,

Dung H. Nguyen

@dunghnguyen,

Thanks for the sample code and template file.

After an initial test, I am able to reproduce the issue as you mentioned by using your sample code with your template file. You are right, there is an issue with function RemoveUnusedStyles(). After calling this function and saving to output file, when loading this file, 5 styles will be added (the style count is 195 instead of 190) to workbook if we consider previous reading is ok. Moreover, calling this function 1 time cannot remove all of unused styles completely and in one go as you pointed out. I have logged a ticket with an id “CELLSNET-46840” for your issue. We will look into it soon.

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

1 Like

@dunghnguyen,

We have fixed the issue that the CountOfStylesInPool changes again for the second invocation of Workbook.RemoveUnusedStyles(). In fact the second invocation of Workbook.RemoveUnusedStyles() removes some styles which should not be removed. We will soon provide you the fixed version after performing QA and incorporating other enhancements and fixes.

For the added styles after saving, it is to keep compatibility for different Excel versions and file formats. We are afraid it cannot and should not be changed.

For the returned value of CountOfStylesInPool after Workbook.RemoveUnusedStyles(), please note:
Method Workbook.RemoveUnusedStyles() only checks and frees those style items in pool(set item as null) which are not used by any other objects, without shrinking the pool by removing those null items. It is because shrinking the pool is a complicated and time-consuming operation which needs to check and update lots of other objects, such as, all Cell objects, Row objects, …etc. So we shrink the pool only when saving the workbook. Method Workbook.RemoveUnusedStyles() is designed mainly for some special purpose of saving the workbook to smaller resultant file or decrease the memory cost. We think getting the actual used count of styles in the pool has no other sense than verifying the result of Workbook.RemoveUnusedStyles().

Thanks for your nice support!

@dunghnguyen,

You are welcome.

The issues you have found earlier (filed as CELLSNET-46840) have been fixed in Aspose.Cells for .NET v19.8. This message was posted using BugNotificationTool from Downloads module by Amjad_Sahi

1 Like