简体   繁体   中英

EXCEL Conditional Formatting in C#

I have column (excel column: AG) called 'First Price' and a column called 'First Price Override' (excel column: AH) and a column called 'First Price Override 2' (excel column: AI).

I need to write some conditional formatting in C# which checks the values in either column AH or AI. If they have values typed into this cell, the AG column (associated row to the values typed in - need to have a font style strike through set to true)

How do i do this?

        var orfirstpricerange1 = Worksheet.Range["AH:AH"];
        var orfirstpricerange2 = Worksheet.Range["AI:AI"];
        var firstpricerange = Worksheet.Range["AG:AG"];

firstpricerange.Font.Strikethrough = true;

I have done the above but dont know how to put it together to use conditional formatting.

    //firstpricerange.Font.Strikethrough = true;

Above will set the formatting of the complete column to 'Strikethrough'. I have commented it because you do not want to have every cell a strikethrough.

    Excel.FormatCondition format = (Excel.FormatCondition)(Worksheet.get_Range("AG:AG",
        Type.Missing).FormatConditions.Add(Excel.XlFormatConditionType.xlExpression, Excel.XlFormatConditionOperator.xlEqual,
        "=AH1=AI1", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing));
    format.Font.Strikethrough = true;

'Excel' is referring to using Excel = Microsoft.Office.Interop.Excel;

Currently column AG gets Strikethrough when the value in AH equals the value in AI.

  Microsoft.Office.Interop.Excel.FormatCondition format = (Microsoft.Office.Interop.Excel.FormatCondition)(uiWorksheet.get_Range("AG:AG",
  Type.Missing).FormatConditions.Add(Microsoft.Office.Interop.Excel.XlFormatConditionType.xlExpression, Microsoft.Office.Interop.Excel.XlFormatConditionOperator.xlNotEqual,
  "=AH1<>AI1", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing));
        format.Font.Strikethrough = true;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM