在工作表中应用条件格式

可能的使用场景

Aspose.Cells 允许您添加各种条件格式,例如公式、高于平均值、色标、数据栏、图标集、Top10 等。它提供了I格式条件类,它具有应用您选择的条件格式的所有必要方法。下面是一些 Get 方法的列表。

在工作表中应用条件格式

以下示例代码显示如何在单元格 A1 和 B2 上添加 Cell 值条件格式。请参阅输出excel文件由代码生成,下面的截图解释了代码对输出excel文件.如果您在单元格 A2 和 B2 中输入大于 100 的值,则单元格 A1 和 B2 中的红色填充颜色将消失。

待办事项:图片_替代_文本

示例代码

//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Path of output excel file
StringPtr outputApplyConditionalFormattingInWorksheet = outPath->StringAppend(new String("outputApplyConditionalFormattingInWorksheet.xlsx"));
//Create an empty workbook
intrusive_ptr<IWorkbook> wb = Factory::CreateIWorkbook();
//Access first worksheet
intrusive_ptr<IWorksheet> ws = wb->GetIWorksheets()->GetObjectByIndex(0);
//Adds an empty conditional formatting
int idx = ws->GetIConditionalFormattings()->Add();
intrusive_ptr<IFormatConditionCollection> fcs = ws->GetIConditionalFormattings()->GetObjectByIndex(idx);
//Set the conditional format range
intrusive_ptr<ICellArea> ca = ICellArea::CreateICellArea(new String("A1"), new String("A1"));
fcs->AddArea(ca);
ca = ICellArea::CreateICellArea(new String("B2"), new String("B2"));
fcs->AddArea(ca);
//Add condition and set the background color
idx = fcs->AddCondition(FormatConditionType_CellValue, OperatorType_Between, new String("=A2"), new String("100"));
intrusive_ptr<IFormatCondition> fc = fcs->GetObjectByIndex(idx);
fc->GetIStyle()->SetBackgroundColor(Systems::Drawing::Color::GetRed());
//User friendly message to test the output excel file.
StringPtr msgStr = new String("Red color in cells A1 and B2 is because of Conditional Formatting. Put 101 or any value >100 in cell A2 and B2, you will see Red background color will be gone.");
ws->GetICells()->GetObjectByIndex(new String("A10"))->PutValue(msgStr);
//Save the output excel file
wb->Save(outputApplyConditionalFormattingInWorksheet);