彩票走势图

Aspose.Cells如何为单元格设置条件格式

原创|其它|编辑:郝浩|2012-09-07 14:47:54.000|阅读 3556 次

概述:在我们平时的Excel操作中,条件格式是一个比较先进的应用。 在本例中,我们将实现:当“A1”这个单元格的值介于50到100的时,Aspose.Cells运用条件格式为此单元格添加红色背景色。运行代码如下:

# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>

在我们平时的Excel操作中,条件格式是一个比较先进的应用。

在本例中,我们将实现:当“A1”这个单元格的值介于50到100的时,Aspose.Cells运用条件格式为此单元格添加红色背景色。运行代码如下:

[C#]

 //Instantiating a Workbook object
Workbook workbook = new Workbook();

Worksheet sheet = workbook.Worksheets[0];

//Adds an empty conditional formatting
int index = sheet.ConditionalFormattings.Add();

FormatConditionCollection fcs =

sheet.ConditionalFormattings[index];

//Sets the conditional format range.
CellArea ca = new CellArea();

ca.StartRow = 0;

ca.EndRow = 0;

ca.StartColumn = 0;

ca.EndColumn = 0;

fcs.AddArea(ca);


//Adds condition.
int conditionIndex = fcs.AddCondition

(FormatConditionType.CellValue, OperatorType.Between, "50",

"100");

//Sets the background color.
FormatCondition fc = fcs[conditionIndex];

fc.Style.BackgroundColor = Color.Red;

//Saving the Excel file
workbook.Save("C:\\output.xls", FileFormatType.Default);
  Additionally I wrote its parallel VB.NET code here for

your convenience:

 

[VB]

 'Instantiating a Workbook object
Dim workbook As Workbook = New Workbook()

Dim sheet As Worksheet = workbook.Worksheets(0)

' Adds an empty conditional formatting
Dim index As Integer = sheet.ConditionalFormattings.Add()

Dim fcs As FormatConditionCollection =

sheet.ConditionalFormattings(index)

'Sets the conditional format range.
Dim ca As CellArea = New CellArea()

ca.StartRow = 0

ca.EndRow = 0

ca.StartColumn = 0

ca.EndColumn = 0

fcs.AddArea(ca)

'Adds condition.
Dim conditionIndex As Integer = fcs.AddCondition

(FormatConditionType.CellValue, OperatorType.Between, "50",

"100")

'Sets the background color.
Dim fc As FormatCondition = fcs(conditionIndex)

fc.Style.BackgroundColor = Color.Red

'Saving the Excel file
workbook.Save("C:\\output.xls", FileFormatType.Default)

代码运行后的效果图如下:

当“A1”的值小于50时:

Aspose.Cells为单元格设置条件格式

当“A1”的值介于50和100之间时:

Aspose.Cells为单元格设置条件格式
 


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn

文章转载自:慧都控件网

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP