ワークシートに条件付き書式設定を適用する

セルの値に基づいた条件付き書式を適用するためのAspose.Cellsの使用

  1. Aspose.Cellsをダウンロードしてインストールします。
    1. Aspose.Cells for .NETをダウンロードします。
  2. 開発コンピュータにインストールします。 すべてのAsposeのコンポーネントは、インストールされると評価モードで動作します。評価モードには時間制限はなく、生成された文書にウォーターマークを注入するだけです。
  3. プロジェクトを作成します。 Visual Studio.NETを起動し、新しいコンソールアプリケーションを作成します。この例ではC#のコンソールアプリケーションを作成しますが、VB.NETも使用できます。
  4. 参照を追加します。 プロジェクトにAspose.Cellsへの参照を追加します。たとえば、….\Program Files\Aspose\Aspose.Cells\Bin\Net1.0\Aspose.Cells.dllへの参照を追加します。
  5. *セルの値に基づいて条件付き書式を適用します。 以下はそのタスクを達成するために使用されるコードです。セルに条件付き書式を適用します。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// 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(dataDir+ "output.out.xls", SaveFormat.Auto);

上記のコードを実行すると、出力ファイル(output.xls)の最初のワークシートのセル“A1”に条件付き書式が適用されます。A1のセル値に応じて条件付き書式が適用されます。A1のセル値が50から100の間の場合、条件付き書式が適用されて背景色が赤になります。

Aspose.Cellsを使用してセルの値に基づいた条件付き書式を適用する

  1. 数式に応じて条件付き書式を適用する(コードスニペット) 以下はそのタスクを達成するためのコードです。B3に条件付き書式を適用します。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// 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 = new CellArea();
ca.StartRow = 2;
ca.EndRow = 2;
ca.StartColumn = 1;
ca.EndColumn = 1;
fcs.AddArea(ca);
// Adds condition.
int conditionIndex = fcs.AddCondition(FormatConditionType.Expression);
// Sets the background color.
FormatCondition fc = fcs[conditionIndex];
fc.Formula1 = "=IF(SUM(B1:B2)>100,TRUE,FALSE)";
fc.Style.BackgroundColor = Color.Red;
sheet.Cells["B3"].Formula = "=SUM(B1:B2)";
sheet.Cells["C4"].PutValue("If Sum of B1:B2 is greater than 100, B3 will have RED background");
// Saving the Excel file
workbook.Save(dataDir+ "output.out.xls", SaveFormat.Auto);

上記のコードを実行すると、出力ファイル(output.xls)の最初のワークシートのセル“B3”に条件付き書式が適用されます。適用された条件付き書式は、B1とB2の値を合計する数式に依存します。