ワークシートに条件付き書式を適用する
Contents
[
Hide
]
考えられる使用シナリオ
Aspose.Cells では、数式、平均以上、カラー スケール、データ バー、アイコン セット、Top10 など、あらゆる種類の条件付き書式を追加できます。IFormatCondition選択した条件付き書式を適用するために必要なすべてのメソッドを持つクラス。 Get メソッドのいくつかのリストを次に示します。
ワークシートに条件付き書式を適用する
次のサンプル コードは、セル A1 と B2 に Cell 値の条件付き書式を追加する方法を示しています。をご覧ください出力エクセルファイルコードによって生成された、および次のスクリーンショットは、コードが出力エクセルファイル.セル A2 と B2 に 100 より大きい値を入力すると、セル A1 と B2 の赤の塗りつぶし色が消えます。
サンプルコード
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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); |