← 返回所有博客文章

Spread 表格控件提供了完整功能和可视化元素,包括数据条、颜色标尺、图标集等。这将帮助您以可见方式对与数据相关的问题作出回应,同时高亮感兴趣的单元格或区域,强调特殊数据以及将数据可视化展现。条件格式可以根据设置的条件或标准改变单元格或区域的外观样式。当条件为true,单元格或区域按照条件格式化;当条件为false,单元格或区域不会根据条件格式化。

例如,您希望根据单元格的值来改变单元格背景色。如果单元格值小于100,那么背景色为红色。格式化条件为“小于100”其中包含了“小于”和数值条件“100”,格式化添加可以是一个常量或表达式。一些条件格式声明可能包含两个限制条件:例如,如果需要设置当单元格在0至100之间时改变背景色。对于这个用例,格式化条件为“介于”,第一个条件为0,第二个条件为100。

如果两个格式化条件同时设置给同一个单元格,那么第二个格式化条件启作用。

使用代码:

首先定义样式,然后使用样式创建条件格式,设置给单元格。

示例:

示例代码以单元格数值为依据设置条件格式。可以在 B2 单元格中输入数值”77”查看效果。

  1: FarPoint.Win.Spread.NamedStyle styleCold = new FarPoint.Win.Spread.NamedStyle();
  2: FarPoint.Win.Spread.NamedStyle styleCool = new FarPoint.Win.Spread.NamedStyle();
  3: FarPoint.Win.Spread.NamedStyle styleMild = new FarPoint.Win.Spread.NamedStyle();
  4: FarPoint.Win.Spread.NamedStyle styleWarm = new FarPoint.Win.Spread.NamedStyle();
  5: FarPoint.Win.Spread.NamedStyle styleHot = new FarPoint.Win.Spread.NamedStyle();
  6:  
  7: 
  8: styleCold.BackColor = Color.Blue;
  9: styleCold.ForeColor = Color.White;
 10: styleCool.BackColor = Color.Cyan;
 11: styleMild.BackColor = Color.Lime;
 12: styleWarm.BackColor = Color.Yellow;
 13: styleHot.BackColor = Color.Red;
 14:  
 15: 
 16: for (int col = 0; col < 6; col++)
 17: {
 18:   fpSpread1.ActiveSheet.SetConditionalFormat(0, col, styleCold, FarPoint.Win.Spread.ComparisonOperator.LessThanOrEqualTo, "32");
 19:   fpSpread1.ActiveSheet.SetConditionalFormat(0, col, styleCool, FarPoint.Win.Spread.ComparisonOperator.Between, "32", "55");
 20:   fpSpread1.ActiveSheet.SetConditionalFormat(0, col, styleMild, FarPoint.Win.Spread.ComparisonOperator.Between, "55", "75");
 21:   fpSpread1.ActiveSheet.SetConditionalFormat(0, col, styleWarm, FarPoint.Win.Spread.ComparisonOperator.Between, "75", "85");
 22:   fpSpread1.ActiveSheet.SetConditionalFormat(0, col, styleHot, FarPoint.Win.Spread.ComparisonOperator.GreaterThan, "85");
 23: }
 24: 
 25: 

 

VB.NET:

  1: Dim styleCold As New FarPoint.Win.Spread.NamedStyle()
  2: Dim styleCool As New FarPoint.Win.Spread.NamedStyle()
  3: Dim styleMild As New FarPoint.Win.Spread.NamedStyle()
  4: Dim styleWarm As New FarPoint.Win.Spread.NamedStyle()
  5: Dim styleHot As New FarPoint.Win.Spread.NamedStyle()
  6:  
  7: 
  8: styleCold.BackColor = Color.Blue
  9: styleCold.ForeColor = Color.White
 10: styleCool.BackColor = Color.Cyan
 11: styleMild.BackColor = Color.Lime
 12: styleWarm.BackColor = Color.Yellow
 13: styleHot.BackColor = Color.Red
 14:  
 15: 
 16: For col As Integer = 0 To 5
 17:   fpSpread1.ActiveSheet.SetConditionalFormat(0, col, styleCold, FarPoint.Win.Spread.ComparisonOperator.LessThanOrEqualTo, "32")
 18:   fpSpread1.ActiveSheet.SetConditionalFormat(0, col, styleCool, FarPoint.Win.Spread.ComparisonOperator.Between, "32", "55")
 19:   fpSpread1.ActiveSheet.SetConditionalFormat(0, col, styleMild, FarPoint.Win.Spread.ComparisonOperator.Between, "55", "75")
 20:   fpSpread1.ActiveSheet.SetConditionalFormat(0, col, styleWarm, FarPoint.Win.Spread.ComparisonOperator.Between, "75", "85")
 21:   fpSpread1.ActiveSheet.SetConditionalFormat(0, col, styleHot, FarPoint.Win.Spread.ComparisonOperator.GreaterThan, "85")
 22: Next col

 

使用 Spread 设计器:

1.选择目标格式化单元格。

2.在菜单项“首页”的选项中,点击条件格式化。

3.点击高亮单元格规则

4.设置条件格式化条件。

5.在“文件”菜单中选择“应用并退出”来应用您的变更到组件中并退出设计器。

效果图:

Demo1