Remove conditional format from Excel in C#

With the help of Spire.XLS, we can set the conditional format the Excel cell in C# and VB.NET. We can also use Spire.XLS to remove the conditional format from a specific cell or the entire Excel worksheet. This article will demonstrate how to remove conditional format from Excel in C#.

Firstly, view the original Excel worksheet with conditional formats:

Remove conditional format from Excel in C#

Step 1: Create an instance of Excel workbook and load the document from file.

Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");

Step 2: Get the first worksheet from the workbook.

Worksheet sheet = workbook.Worksheets[0];

Step 3: Remove the first conditional format.

sheet.ConditionalFormats.RemoveAt(0);

Step 4: Remove all the conditional formats from the whole Excel worksheet.

for (int i = sheet.ConditionalFormats.Count-1; i >= 0; i--)
{
sheet.ConditionalFormats.RemoveAt(i);
}

Step 5: Save the document to file.

workbook.SaveToFile("Result.xlsx", ExcelVersion.Version2010);

Remove the conditional format from a special Excel range B2:

Remove conditional format from Excel in C#

Remove all the conditional formats from the entire Excel worksheet:

Remove conditional format from Excel in C#

Full codes of how to remove the conditional formats from Excel worksheet:

using Spire.Xls;

namespace RemoveConditionalFormat
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Sample.xlsx");

            Worksheet sheet = workbook.Worksheets[0];

            // Remove the first conditional format
            //sheet.ConditionalFormats.RemoveAt(0);

            // Remove all conditional formats
            for (int i = sheet.ConditionalFormats.Count-1; i >= 0; i--)
            {
                sheet.ConditionalFormats.RemoveAt(i);
            }

            workbook.SaveToFile("Result.xlsx", ExcelVersion.Version2010);
        }
    }
}