ワークシートに条件付き書式を適用する
この記事は、ワークシートのセル範囲に条件付き書式を追加する方法を詳しく説明することを目的としています。
条件付き書式は Microsoft Excel の高度な機能で、セルの範囲に書式を適用し、セルの値または数式の値に応じて書式を変更できます。たとえば、セルの背景を赤色にして負の値を強調したり、テキストの色を緑色にして正の値にしたりすることができます。セルの値がフォーマット条件を満たしている場合、フォーマットが適用されます。セルの値がフォーマット条件を満たさない場合、セルのデフォルトのフォーマットが使用されます。
Microsoft Office Automation で条件付き書式を適用することは可能ですが、これには欠点があります。関連する理由と問題はいくつかあります。たとえば、セキュリティ、安定性、スケーラビリティ、速度などです。別のソリューションを見つける主な理由は、Microsoft 自身がソフトウェア ソリューションに Office Automation を使用しないことを強く推奨しているためです。
この記事では、コンソール アプリケーションを作成し、Aspose.Cells API.
Aspose.Cells を使用して、Cell 値に基づいて条件付き書式を適用する
- Aspose.Cells をダウンロードしてインストールします.
- Aspose.Cells for .NET をダウンロードします。
- 開発用コンピューターにインストールします。 Aspose コンポーネントはすべて、インストールすると評価モードで動作します。評価モードには時間制限がなく、生成されたドキュメントに透かしを挿入するだけです。
- プロジェクトを作成する. Visual Studio.NET を起動し、新しいコンソール アプリケーションを作成します。この例では、C# コンソール アプリケーションを作成しますが、VB.NET も使用できます。
- 参照を追加する. プロジェクトに Aspose.Cells への参照を追加します。たとえば、….\Program Files\Aspose\Aspose.Cells\Bin\Net1.0\Aspose.Cells.dll への参照を追加します。
- *セルの値に基づいて条件付き書式を適用します。 以下は、タスクを実行するために使用されるコードです。セルに条件付き書式を適用します。
// 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 を使用して数式に基づく条件付き書式を適用する
- 数式に応じた条件付き書式の適用 (コード スニペット) 以下は、タスクを実行するためのコードです。 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」に条件付き書式が適用されます。適用される条件付き書式は、「B3」の値を B1 と B2 の合計として計算する式によって異なります。