【发布时间】:2015-12-29 08:16:50
【问题描述】:
我有这段代码。我想在以Sat 或Sun 开头的行中用绿色为背景着色。我还想用红色字体为小于特定值的单元格着色。
// Color the row when the day is Saturday or Sunday
var rang2 = ws.Range["$A$5:$M35"];
Excel.FormatCondition condition2 = (Excel.FormatCondition)rang2.FormatConditions.Add(
Excel.XlFormatConditionType.xlExpression,
Type.Missing, "=OR(TEXT($A1,\"ddd\")=\"Sun\",TEXT($A1,\"ddd\")=\"Sat\")", Type.Missing, Type.Missing,Type.Missing, Type.Missing, Type.Missing);
// Color with red
var rang = ws.Range["$K$5:$L35"];
Excel.FormatCondition condition = (Excel.FormatCondition)rang.FormatConditions.Add(
Excel.XlFormatConditionType.xlCellValue,
Excel.XlFormatConditionOperator.xlLess, "=$L$5", Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing);
condition2.Interior.ColorIndex = 50; condition2.Font.ColorIndex = 1; // Green Background and Black font
condition.Interior.ColorIndex = 0; condition.Font.ColorIndex = 3; // White Background and Red font
问题在于这两种条件格式相互重叠。我尝试更改两种条件格式的顺序,但仍然无法使其正常工作。
使用此设置,第一条规则也将包含红色字体,而第二条规则仅包含白色背景和黑色文本(而不是红色文本)。
【问题讨论】:
-
是否需要使用条件格式?用 C# 写出逻辑会容易得多
-
我正在尝试保存到 Excel 文件中。最终文件将发送给某人,所以我需要条件格式。
标签: c# excel formatting interop conditional