Search code examples
c#.netexcelstylesclosedxml

C# clear "styles.xml" in the book excel (xlsx) using closedxml


I have a C# program which insert values and applies styles to an Excel workbook (xlsx). I use the ClosedXML library.

I ran into the problem that after many file entries (xlsx), the style of the book does not apply. I noticed that the file size increases over time and when I changed the file extension from .xlsx to .zip and unzipped it I noticed that the file "styles.xml" is large in size 8 - 10 MB.
I think that the reason for the file overflow "styles.xml".

How to clean using ClosedXML all previously applied styles in the book excel?


Solution

  • This should do the trick

    foreach (var worksheet in workbook.Worksheets)
        worksheet.Clear(XLClearOptions.AllFormats);
    

    AllFormats includes conditional formats too. If you want to preserve them use NormalFormats, as Francois suggested.