我想以编程方式使用Excel 2007的条件格式功能。我有以下情况。以编程方式在Excel 2007中以编程方式添加条件格式
我有一个数字
A B C D E F
100 25 25 15 20 50
....
if (C1/A1)*100 >= B1
我要颜色为红色的6列。同样的规则适用于D1,E1,F1列。
我在Excel 2007中看到了条件格式化功能。但我想了解如何在C#代码中实现它的一些指针。
我想以编程方式使用Excel 2007的条件格式功能。我有以下情况。以编程方式在Excel 2007中以编程方式添加条件格式
我有一个数字
A B C D E F
100 25 25 15 20 50
....
if (C1/A1)*100 >= B1
我要颜色为红色的6列。同样的规则适用于D1,E1,F1列。
我在Excel 2007中看到了条件格式化功能。但我想了解如何在C#代码中实现它的一些指针。
我打算假设您很高兴使用Interop,因为您没有另行说明。这是我用来在Excel中设置条件格式的东西。它假定您已经在名为xlWorksheet
的变量中引用了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;
我已经猜测了一下您的预期用法,但您可以用它来调整它以获得公式和单元格引用。
thnx为答案。但是,当我使用代码Im得到以下错误消息=“方法未找到:”Microsoft.Office.Interop.Excel.FormatCondition Microsoft.Office.Interop.Excel.FormatConditions.Add(Microsoft.Office.Interop.Excel.XlFormatConditionType,System .Object,System.Object,System.Object)'。“ – Rajneesh 2013-03-06 09:20:13
@Rajneesh对不起,我的代码中有一个错字。我没有正确关闭支架。再给它一次。如果它仍然不起作用,请显示导致错误的语句。 – 2013-03-06 16:06:43
VS Intillesense在使用3个参数的add方法时显示错误。因此,我使用了FormatCondition formatCondition =(Excel.FormatCondition)formatConditions.Add(XlFormatConditionType.xlExpression,Type.Missing,“= D2/B2 * 100> = C2”,Type.Missing);但是,在执行代码时,它显示了我在之前的评论中提到的错误。 – Rajneesh 2013-03-07 04:48:23
我很确定Google知道该怎么办) – Ksenia 2013-03-05 16:31:18