Previous topicNext topic
Help > 开发指南 > Excel > API > 示例 > 条件格式 >
如何:设置排名靠前或排名靠后的值的格式

此示例演示如何应用“top/bottom N”条件格式规则。

  1. 若要创建由 RankConditionalFormatting 对象表示的新条件格式规则,请从 Worksheet.ConditionalFormattings 属性访问条件格式集合,并调用 ConditionalFormattingCollection.AddRankConditionalFormatting 方法。 传递以下参数:

   2. 使用 RankConditionalFormatting 对象的 ISupportsFormatting.Formatting 属性指定要应用于单元格的格式设置选项(如果条件为 true)。定义背景和字体颜色,并设置轮廓边框。
注意

条件格式不支持透明度。

若要删除 RankConditionalFormatting 对象,请使用 ConditionalFormattingCollection.Remove、ConditionalFormattingCollection.RemoveAt 或 ConditionalFormattingCollection.Clear 方法。

Vb.Net
'创建规则以识别单元格F2到F15中的前三个值。
Dim cfRule As DevExpress.Spreadsheet.RankConditionalFormatting = worksheet.ConditionalFormattings.AddRankConditionalFormatting(worksheet.Range("$F$2:$F$15"), DevExpress.Spreadsheet.ConditionalFormattingRankCondition.TopByRank, 3)
'如果条件为true,请指定要应用于单元格的格式选项。
'将背景色设置为深兰色。
cfRule.Formatting.Fill.BackgroundColor = Color.DarkOrchid
'设置轮廓边框。
cfRule.Formatting.Borders.SetOutsideBorders(Color.Black, DevExpress.Spreadsheet.BorderLineStyle.Thin)
'将字体颜色设置为白色。
cfRule.Formatting.Font.Color = Color.White

C#
// 创建规则以识别单元格F2到F15中的前三个值。
DevExpress.Spreadsheet.RankConditionalFormatting cfRule = worksheet.ConditionalFormattings.AddRankConditionalFormatting(worksheet.Range["$F$2:$F$15"], DevExpress.Spreadsheet.ConditionalFormattingRankCondition.TopByRank, 3);
// 如果条件为true,请指定要应用于单元格的格式选项。
// 将背景色设置为深兰色。
cfRule.Formatting.Fill.BackgroundColor = Color.DarkOrchid;
// 设置轮廓边框。
cfRule.Formatting.Borders.SetOutsideBorders(Color.Black, DevExpress.Spreadsheet.BorderLineStyle.Thin);
// 将字体颜色设置为白色。
cfRule.Formatting.Font.Color = Color.White;

下图显示了结果(工作簿在 Microsoft® Excel® 中打开)。突出显示前三个价格值。