Apply conditional formatting in Excel

  • VSTO
  • Spire.XLS
  • Download Sample Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
using System.Drawing;

namespace VSTO
{
    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            //Instantiate the Application object
            Excel.Application excelApp = Application;

            //Add a Workbook
            Excel.Workbook workbook = excelApp.Workbooks.Add();

            //Get the First sheet
            Excel.Worksheet sheet = (Excel.Worksheet)workbook.Sheets["Sheet1"];

            //Access cells A1, A2, A3
            Excel.Range range1 = sheet.get_Range("A1");
            Excel.Range range2 = sheet.get_Range("A2");
            Excel.Range range3 = sheet.get_Range("A3");

            //Set values
            range1.Value = 100;
            range2.Value = 200;
            range3.Value = 400;

            //Add a conditional formatting and set its type to CellValue.
            Excel.XlFormatConditionOperator operate = Excel.XlFormatConditionOperator.xlLess;
            string formula = "300";
            Excel.FormatCondition format = sheet.get_Range("A1", "A3").FormatConditions.Add(Excel.XlFormatConditionType.xlCellValue, operate, formula, missing, missing, missing, missing, missing);
            format.Font.Color = Color.Red;

            //Save the file
            workbook.SaveAs("ConditionalFormatting.xlsx");
 
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        
        #endregion
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spire.Xls;
using System.Drawing;

namespace Spire.XLS
{
    class Program
    {
        static void Main(string[] args)
        {
            //Initialize a new instance of workbook
            Workbook workbook = new Workbook();

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Access cells A1, A2, A3
            CellRange range1 = sheet.Range["A1"];
            CellRange range2 = sheet.Range["A2"];
            CellRange range3 = sheet.Range["A3"];

            //Set value
            range1.NumberValue = 100;
            range2.NumberValue = 200;
            range3.NumberValue = 400;

            //Add a conditional formatting of cell range and set its type to CellValue
            ConditionalFormatWrapper format = sheet.Range.ConditionalFormats.AddCondition();
            format.FormatType = ConditionalFormatType.CellValue;
            format.FirstFormula = "300";
            format.Operator = ComparisonOperatorType.Less;
            format.FontColor = Color.Red;

            //Save and Launch
            workbook.SaveToFile("ConditionalFormatting.xlsx",ExcelVersion.Version2010);
            System.Diagnostics.Process.Start("ConditionalFormatting.xlsx");
           
        }
    }
}