此示例演示如何应用图标集条件格式规则。
3. 使用 IconSetConditionalFormatting.ShowValue 属性指定是显示还是隐藏应用条件格式规则的单元格的值。
要为创建的规则指定自定义图标样式,请执行以下操作:
若要删除 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® 中打开)。应用的图标允许您识别成本的上升和下降趋势。