C#/VB.NET: Alternate Row Colors in Excel Using Conditional Formatting

A large worksheet can be made easier to scan and read by adding color to alternative rows or columns. Applying a built-in table style or using conditional formatting are two quick ways to alternate row colors. This article focuses on how to highlight alternative rows using conditional formatting in C# and VB.NET using Spire.XLS for .NET.

Install Spire.XLS for .NET

To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.XLS

Alternate Row Colors in Excel Using Conditional Formatting

The following are the steps to add color to alternative rows in Excel using Spire.XLS for .NET.

  • Create a Workbook object.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get a specific worksheet from the workbook through Workbook.Worsheets[index] property.
  • Add a conditional formatting to the worksheet using Worksheet.ConditionalFormats.Add() method and return an object of XlsConditionalFormats class.
  • Set the cell range where the conditional formatting will be applied using XlsConditionalFormats.AddRange() method.
  • Add a condition using XlsConditionalFormats.AddCondition() method, then set the conditional formula and the cell color of even rows.
  • Add another condition to change the format of the cells of odd rows.
  • Save the workbook to an Excel file using Workbook.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Xls;
using Spire.Xls.Core;
using Spire.Xls.Core.Spreadsheet.Collections;
using System.Drawing;

namespace AlternateRowColors
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook object
            Workbook workbook = new Workbook();

            //Load an Excel file
            workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.xlsx");
            
            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Add a conditional format to the worksheet
            XlsConditionalFormats format = sheet.ConditionalFormats.Add();

            //Set the range where the conditional format will be applied
            format.AddRange(sheet.Range[2, 1, sheet.LastRow, sheet.LastColumn]);

            //Add a condition to change the format of the cells based on formula
            IConditionalFormat condition1 = format.AddCondition();        
            condition1.FirstFormula = "=MOD(ROW(),2)=0";
            condition1.FormatType = ConditionalFormatType.Formula;
            condition1.BackColor = Color.Yellow;

            //Add another condition to change the format of the cells based on formula
            IConditionalFormat condition2 = format.AddCondition();
            condition2.FirstFormula = "=MOD(ROW(),2)=1";
            condition2.FormatType = ConditionalFormatType.Formula;
            condition2.BackColor = Color.LightSeaGreen;

            //Save the workbook to an Excel file
            workbook.SaveToFile("AlternateRowColors.xlsx", ExcelVersion.Version2016);
        }
    }
}

C#/VB.NET: Alternate Row Colors in Excel Using Conditional Formatting

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.