以编程方式在Excel 2007中添加条件格式
我想以编程方式使用excel 2007的条件格式化function。 我有以下情况。
我有6列数字
ABCDEF 100 25 25 15 20 50 ....
if (C1/A1)*100 >= B1
我必须将其颜色设为红色。 D1,E1,F1列同样适用。
我在Excel 2007中看到了条件格式化function。但是我想要一些关于如何在C#代码中实现它的指针。
我会假设你很高兴使用Interop,因为你没有另行说明。 这是我用来在Excel中设置条件格式。 它假定你已经有一个名为xlWorksheet
的variables引用了你的Worksheet
。
// Create a FormatCondition and add it to the specified range of cells, using the // passed-in condition and cell colour Excel.Range range = xlWorksheet.get_Range("C1", "C1"); Excel.FormatConditions fcs = range.FormatConditions; Excel.FormatCondition fc = (Excel.FormatCondition)fcs.Add (Excel.XlFormatConditionType.xlExpression, Type.Missing, "=IF($C$1/$A$1)*100 >= $B$1"); Excel.Interior interior = fc.Interior; interior.Color = ColorTranslator.ToOle(Color.Red); interior = null; fc = null; fcs = null; range = null;
我已经猜到了你的预期用法,但你可以摆弄它来获得公式和单元格引用的权利。