ConditionalFormattingCollection

ConditionalFormattingCollection class

封装了一个集合FormatCondition对象.

public class ConditionalFormattingCollection : CollectionBase<FormatConditionCollection>

特性

姓名 描述
Capacity { get; set; }
Count { get; }
Item { get; } 获取指定索引处的 FormatConditions 元素。
Item { get; set; }

方法

姓名 描述
Add() 将 FormatConditions 添加到集合中。
BinarySearch(FormatConditionCollection)
BinarySearch(FormatConditionCollection, IComparer<FormatConditionCollection>)
BinarySearch(int, int, FormatConditionCollection, IComparer<FormatConditionCollection>)
Clear()
Contains(FormatConditionCollection)
Copy(ConditionalFormattingCollection) 复制条件格式。
CopyTo(FormatConditionCollection[])
CopyTo(FormatConditionCollection[], int)
CopyTo(int, FormatConditionCollection[], int, int)
Exists(Predicate<FormatConditionCollection>)
Find(Predicate<FormatConditionCollection>)
FindAll(Predicate<FormatConditionCollection>)
FindIndex(Predicate<FormatConditionCollection>)
FindIndex(int, Predicate<FormatConditionCollection>)
FindIndex(int, int, Predicate<FormatConditionCollection>)
FindLast(Predicate<FormatConditionCollection>)
FindLastIndex(Predicate<FormatConditionCollection>)
FindLastIndex(int, Predicate<FormatConditionCollection>)
FindLastIndex(int, int, Predicate<FormatConditionCollection>)
GetEnumerator()
IndexOf(FormatConditionCollection)
IndexOf(FormatConditionCollection, int)
IndexOf(FormatConditionCollection, int, int)
LastIndexOf(FormatConditionCollection)
LastIndexOf(FormatConditionCollection, int)
LastIndexOf(FormatConditionCollection, int, int)
RemoveArea(int, int, int, int) 删除范围内的所有条件格式。
RemoveAt(int)

例子


[C#]

//实例化一个工作簿对象
Workbook workbook = new Workbook();

Worksheet sheet = workbook.Worksheets[0];

//获取条件格式
ConditionalFormattingCollection cformattings = sheet.ConditionalFormattings;

//添加一个空的条件格式
int index = cformattings.Add();

//获取新添加的条件格式
FormatConditionCollection fcs = cformattings[index];

//设置条件格式范围。
CellArea ca = new CellArea();

ca.StartRow = 0;

ca.EndRow = 0;

ca.StartColumn = 0;

ca.EndColumn = 0;

fcs.AddArea(ca);

ca = new CellArea();

ca.StartRow = 1;

ca.EndRow = 1;

ca.StartColumn = 1;

ca.EndColumn = 1;

fcs.AddArea(ca);

//添加条件。
int conditionIndex = fcs.AddCondition(FormatConditionType.CellValue, OperatorType.Between, "=A2", "100");

//添加条件。
int conditionIndex2 = fcs.AddCondition(FormatConditionType.CellValue, OperatorType.Between, "50", "100");

//设置背景颜色。
FormatCondition fc = fcs[conditionIndex];

fc.Style.BackgroundColor = Color.Red;

//保存Excel文件
workbook.Save("output.xls");

[VB.NET]

'实例化工作簿对象
DDim workbook As Workbook = New Workbook()

Dim sheet As Worksheet = workbook.Worksheets(0)

'获取条件格式
Dim cformattings As ConditionalFormattingCollection = sheet.ConditionalFormattings

'添加一个空的条件格式
Dim index As Integer = cformattings.Add()

'获取新添加的条件格式
Dim fcs As FormatConditionCollection = cformattings(index)

'设置条件格式范围。
Dim ca As New CellArea()

ca.StartRow = 0

ca.EndRow = 0

ca.StartColumn = 0

ca.EndColumn = 0

fcs.AddArea(ca)

ca = New CellArea()

ca.StartRow = 1

ca.EndRow = 1

ca.StartColumn = 1

ca.EndColumn = 1

fcs.AddArea(ca)

'添加条件。
Dim conditionIndex As Integer = fcs.AddCondition(FormatConditionType.CellValue, OperatorType.Between, "=A2", "100")

'添加条件。
Dim conditionIndex2 As Integer = fcs.AddCondition(FormatConditionType.CellValue, OperatorType.Between, "50", "100")

'设置背景颜色。
Dim fc As FormatCondition = fcs(conditionIndex)

fc.Style.BackgroundColor = Color.Red

'保存 Excel 文件
workbook.Save("output.xls")

也可以看看