在我们平时的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时:
当“A1”的值介于50和100之间时:
标签:
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,尊重他人劳动成果
文章转载自:慧都控件网
2
好文不易,鼓励一下吧!