Previous topicNext topic
Help > 开发指南 > Excel > API > 示例 > 条件格式 >
如何:使用图标集设置单元格格式

此示例演示如何应用图标集条件格式规则。

  1. 首先,指定分隔数据类别的阈值,每个阈值都将由图标集中的自己的图标表示。为此,请使用 ConditionalFormattingCollection.CreateIconSetValue 方法创建 ConditionalFormattingIconSetValue 对象的实例。此对象继承自 ConditionalFormattingValue 基接口,并提供对阈值及其类型的访问。阈值的类型由 ConditionalFormattingValueType 枚举值之一确定,可以是数字、百分比、公式或百分位数。ConditionalFormattingCollection.CreateIconSetValue 方法中的第三个参数定义由 ConditionalFormattingValueOperator 枚举值之一指定的关系运算符。
  2. 若要应用由 IconSetConditionalFormatting 对象表示的条件格式规则,请从 Worksheet.ConditionalFormattings 属性访问条件格式集合,并调用 ConditionalFormattingCollection.AddIconSetConditionalFormatting 方法。 传递以下参数:

  3.  使用 IconSetConditionalFormatting.ShowValue 属性指定是显示还是隐藏应用条件格式规则的单元格的值。

要为创建的规则指定自定义图标样式,请执行以下操作:

  1. 将 IconSetConditionalFormatting.IsCustom 属性设置为 true 以指示使用自定义图标样式。
  2. 使用默认构造函数初始化 ConditionalFormattingCustomIcon 结构的实例。使用 ConditionalFormattingCustomIcon.IconSet 和 ConditionalFormattingCustomIcon.IconIndex 对象的属性分别指定要从中获取图标的图标集以及集中所需图标的索引。
  3. 使用以下参数通过 IconSetConditionalFormatting.SetCustomIcon 方法设置自定义图标:初始图标集中将添加自定义图标的位置,以及 ConditionalFormattingCustomIcon 对象指定的自定义图标。

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

Vb.Net
Dim conditionalFormattings As DevExpress.Spreadsheet.ConditionalFormattingCollection = worksheet.ConditionalFormattings
'使用MIN()公式将第一个阈值设置为单元格范围内的最低值。
Dim minPoint As DevExpress.Spreadsheet.ConditionalFormattingIconSetValue = conditionalFormattings.CreateIconSetValue(DevExpress.Spreadsheet.ConditionalFormattingValueType.Formula, "=MIN($E$2:$E$15)", DevExpress.Spreadsheet.ConditionalFormattingValueOperator.GreaterOrEqual)
'将第二个阈值设置为0。
Dim midPoint As DevExpress.Spreadsheet.ConditionalFormattingIconSetValue = conditionalFormattings.CreateIconSetValue(DevExpress.Spreadsheet.ConditionalFormattingValueType.Number, "0", DevExpress.Spreadsheet.ConditionalFormattingValueOperator.GreaterOrEqual)
'将第三个阈值设置为0.01。
Dim maxPoint As DevExpress.Spreadsheet.ConditionalFormattingIconSetValue = conditionalFormattings.CreateIconSetValue(DevExpress.Spreadsheet.ConditionalFormattingValueType.Number, "0.01", DevExpress.Spreadsheet.ConditionalFormattingValueOperator.GreaterOrEqual)
'创建规则,根据其值将三箭头图标集中的特定图标应用于范围E2:E15中的每个单元格。
Dim cfRule As DevExpress.Spreadsheet.IconSetConditionalFormatting = conditionalFormattings.AddIconSetConditionalFormatting(worksheet.Range("$E$2:$E$15"), DevExpress.Spreadsheet.IconSetType.Arrows3, New DevExpress.Spreadsheet.ConditionalFormattingIconSetValue() {minPoint, midPoint, maxPoint})
'如果第二个条件为true,则指定要显示的自定义图标。
'要执行此操作,请设置IcoSettingConditionalFormatting。IsCustom属性设置为true,默认情况下为false。
cfRule.IsCustom = True
'初始化ConditionalFormattingCustomIcon对象。
Dim cfCustomIcon As New DevExpress.Spreadsheet.ConditionalFormattingCustomIcon()
'指定要获取图标的图标集。
cfCustomIcon.IconSet = DevExpress.Spreadsheet.IconSetType.TrafficLights13
'指定集合中所需图标的索引。
cfCustomIcon.IconIndex = 1
'在初始图标集中的指定位置添加自定义图标。
cfRule.SetCustomIcon(1, cfCustomIcon)
'隐藏应用规则的单元格的值。
cfRule.ShowValue = False

C#
DevExpress.Spreadsheet.ConditionalFormattingCollection conditionalFormattings = worksheet.ConditionalFormattings;
// 使用MIN()公式将第一个阈值设置为单元格范围内的最低值。
DevExpress.Spreadsheet.ConditionalFormattingIconSetValue minPoint = conditionalFormattings.CreateIconSetValue(DevExpress.Spreadsheet.ConditionalFormattingValueType.Formula, "=MIN($E$2:$E$15)", DevExpress.Spreadsheet.ConditionalFormattingValueOperator.GreaterOrEqual);
// 将第二个阈值设置为0。
DevExpress.Spreadsheet.ConditionalFormattingIconSetValue midPoint = conditionalFormattings.CreateIconSetValue(DevExpress.Spreadsheet.ConditionalFormattingValueType.Number, "0", DevExpress.Spreadsheet.ConditionalFormattingValueOperator.GreaterOrEqual);
// 将第三个阈值设置为0.01。
DevExpress.Spreadsheet.ConditionalFormattingIconSetValue maxPoint = conditionalFormattings.CreateIconSetValue(DevExpress.Spreadsheet.ConditionalFormattingValueType.Number, "0.01", DevExpress.Spreadsheet.ConditionalFormattingValueOperator.GreaterOrEqual);
// 创建规则,根据其值将三箭头图标集中的特定图标应用于范围E2:E15中的每个单元格。
DevExpress.Spreadsheet.IconSetConditionalFormatting cfRule = conditionalFormattings.AddIconSetConditionalFormatting(worksheet.Range["$E$2:$E$15"], DevExpress.Spreadsheet.IconSetType.Arrows3, new DevExpress.Spreadsheet.ConditionalFormattingIconSetValue[] { minPoint, midPoint, maxPoint });
// 如果第二个条件为true,则指定要显示的自定义图标。
// 要执行此操作,请设置IcoSettingConditionalFormatting。IsCustom属性设置为true,默认情况下为false。
cfRule.IsCustom = true;
// 初始化ConditionalFormattingCustomIcon对象。
DevExpress.Spreadsheet.ConditionalFormattingCustomIcon cfCustomIcon = new DevExpress.Spreadsheet.ConditionalFormattingCustomIcon();
// 指定要获取图标的图标集。
cfCustomIcon.IconSet = DevExpress.Spreadsheet.IconSetType.TrafficLights13;
// 指定集合中所需图标的索引。
cfCustomIcon.IconIndex = 1;
// 在初始图标集中的指定位置添加自定义图标。
cfRule.SetCustomIcon(1, cfCustomIcon);
// 隐藏应用规则的单元格的值。
cfRule.ShowValue = false;

下图显示了结果(工作簿在 Microsoft® Excel® 中打开)。应用的图标允许您识别成本的上升和下降趋势。