MEMO
- この記事は GitHub - ClosedXML - Wiki を参考に書かせていただいたものですが、C# から VB.NET への書き換えをし、英語から日本語へ翻訳しただけでなく、元記事の構成・コード・コメント等を変更している場合もありますのでご了承ください。
- 現在 ClosedXML のメンテナンスをご担当されている Francois Botha 様からの掲載許可もいただいてます。
- この記事のコードサンプルについては、Imports ClosedXML.Excel が宣言されていることを前提としています。
- ClosedXML の作成者について:Francois Botha, Aleksei Pankratev, Manuel de Leon, Amir Ghezelbash
- ClosedXML のライセンスについて:GitHub - ClosedXML - MIT License
セルの値が 2 以上 3 以下ならセルの背景色を赤にする
ws.FirstCell.SetValue(1) _ .CellBelow.SetValue(1) _ .CellBelow.SetValue(2) _ .CellBelow.SetValue(3) _ .CellBelow.SetValue(4) ws.RangeUsed.AddConditionalFormat.WhenBetween(2, 3) _ .Fill.SetBackgroundColor(XLColor.Red)
3 色スケールの適用
ws.FirstCell.SetValue(1) _ .CellBelow.SetValue(1) _ .CellBelow.SetValue(2) _ .CellBelow.SetValue(3) _ .CellBelow.SetValue(4) ws.RangeUsed.AddConditionalFormat.ColorScale _ .LowestValue(XLColor.Red) _ .Midpoint(XLCFContentType.Percent, 50, XLColor.Yellow) _ .HighestValue(XLColor.Green)
アイコンセットの適用
ws.FirstCell.SetValue(1) _ .CellBelow.SetValue(1) _ .CellBelow.SetValue(2) _ .CellBelow.SetValue(3) _ .CellBelow.SetValue(4) ws.RangeUsed.AddConditionalFormat.IconSet(XLIconSetStyle.ThreeTrafficLights2) _ .AddValue(XLCFIconSetOperator.EqualOrGreaterThan, 0, XLCFContentType.Number) _ .AddValue(XLCFIconSetOperator.EqualOrGreaterThan, 2, XLCFContentType.Number) _ .AddValue(XLCFIconSetOperator.EqualOrGreaterThan, 3, XLCFContentType.Number)
データバーの適用
ws.FirstCell.SetValue(1) _ .CellBelow.SetValue(1) _ .CellBelow.SetValue(2) _ .CellBelow.SetValue(3) _ .CellBelow.SetValue(4) ws.RangeUsed.AddConditionalFormat.DataBar(XLColor.Red) _ .LowestValue _ .HighestValue
式の使用
セルの値を比較対象にすることもできます。
ws.FirstCell.SetValue(1) _ .CellBelow.SetValue(1) _ .CellBelow.SetValue(2) _ .CellBelow.SetValue(3) _ .CellBelow.SetValue(4) Dim oUsedRange = ws.RangeUsed ws.Cell("C1").Value = 3 oUsedRange.AddConditionalFormat.WhenEqualOrGreaterThan("=$C$1") _ .Fill.SetBackgroundColor(XLColor.Yellow)