原創|其它|編輯:郝浩|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時:
當“A1”的值介于50和100之間時:
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都控件網