Previous topicNext topic
Help > 开发指南 > Excel > API > 示例 > 条件格式 >
如何:设置包含日期的单元格的格式

此示例演示如何应用“发生日期...”条件格式规则。

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

  2. 使用 TimePeriodConditionalFormatting 对象的 ISupportsFormatting.Formatting 属性指定要应用于单元格的格式设置选项(如果条件为 true)。

注意

条件格式不支持透明度。

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

Vb.Net
'创建规则,在单元格B2到B6中突出显示今天的日期。
Dim cfRule As DevExpress.Spreadsheet.TimePeriodConditionalFormatting = worksheet.ConditionalFormattings.AddTimePeriodConditionalFormatting(worksheet.Range("$B$2:$B$6"), DevExpress.Spreadsheet.ConditionalFormattingTimePeriod.Today)
'如果条件为true,请指定要应用于单元格的格式选项。
'将背景色设置为粉红色。
cfRule.Formatting.Fill.BackgroundColor = Color.FromArgb(255, &HF2, &HAE, &HE3)

C#
// 创建规则,在单元格B2到B6中突出显示今天的日期。
DevExpress.Spreadsheet.TimePeriodConditionalFormatting cfRule = worksheet.ConditionalFormattings.AddTimePeriodConditionalFormatting(worksheet.Range["$B$2:$B$6"], DevExpress.Spreadsheet.ConditionalFormattingTimePeriod.Today);
// 如果条件为true,请指定要应用于单元格的格式选项。
// 将背景色设置为粉红色。
cfRule.Formatting.Fill.BackgroundColor = Color.FromArgb(255, 0xF2, 0xAE, 0xE3);

下图显示了结果(工作簿在 Microsoft® Excel® 中打开)。“截止日期”列中的“今天”日期以粉红色突出显示。