1

我需要以编程方式使用条件格式在 Excel 工作表中查找重复值。

试过这种方式,但在 6 行代码中我有 COM 异常

无法转换为 Excel.FormatCondition

这是我的代码

Excel.Workbook xlWB = Application.ActiveWorkbook;
Excel.Worksheet xlWS = xlWB.ActiveSheet;
xlWS.Range["B2:B9"].Select();
Excel.Range xlS = Application.Selection;
xlS.FormatConditions.AddUniqueValues();
Excel.FormatCondition xlFC = 
    (Excel.FormatCondition)xlS.FormatConditions[xlS.FormatConditions.Count];
xlFC.SetFirstPriority();
Excel.FormatCondition xlFC1 = (Excel.FormatCondition)xlS.FormatConditions[1];
xlFC1.Interior.Pattern = Excel.XlPattern.xlPatternAutomatic;
xlFC1.Interior.TintAndShade = 0;
xlFC1.Interior.Color = ColorTranslator.FromOle(13551615);
4

1 回答 1

0

看看它MSDN 链接

或者你可以使用这样的东西

 Excel.Range usedRange = Worksheet.UsedRange;
 usedRange.Interior.Color =    
 System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);

  FormatCondition format = (FormatCondition)(Worksheet.get_Range("A1:D13",
            Type.Missing).FormatConditions.Add(XlFormatConditionType.xlExpression,    
  XlFormatConditionOperator.xlEqual,
            "=$A1=$D1", Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
  Type.Missing));

   format.Font.Bold = true;
   format.Interior.Color = 
   System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);//Duplicate values

希望这有帮助..它对我有用

于 2013-01-28T07:24:12.413 回答