トップ10条件付き書式の追加方法
Contents
[
Hide
]
可能な使用シナリオ
Excelのトップ10条件付き書式を使用すると、データセット内の最高のパフォーマンス値を素早くハイライトできます — 文字通りのトップ10値だけでなく、Top NやTop N%(選択可能)も含まれます。
- 傾向と異常値を見つける:例えば、トップ10の営業担当者、最高得点、最も収益の高い月などを即座に識別。データの並べ替えなしで分析しやすくなります。
- データの可視化:重要なデータポイントを視覚的に際立たせる色のヒントを追加。スプレッドシートの閲覧者が主要な値を一目で理解できるようにします。
- クイック比較:ダッシュボードやレポートで、優れた結果やピークをハイライトしたい場合に役立ちます。
- ダイナミックな更新:データが変更された場合、条件付き書式は自動的に更新され、新しいトップ値を反映します。
Excelでトップ10条件付き書式を追加する方法
Excelでトップ10条件付き書式をステップバイステップで追加する方法:
- 分析したいセル範囲を選択します。例:スコアや販売数字に取り組む場合はB2:B100を選択します。
- Excelリボンのホームタブに移動します。
- スタイルグループの条件付き書式をクリックします。
- ドロップダウンメニューのトップ/ボトムルールにカーソルを合わせます。
- Top 10 Items…をクリックします。
- ポップアップのダイアログボックスが表示され、次のように表示されます:上位10にランク付けされるセルをフォーマットします。数字(例:Top 5、Top 3など)を変更可能です。フォーマット(薄い赤色の塗りつぶし、太字のテキスト、または詳細なオプションのためにカスタムフォーマットをクリック)を選択します。
- OKをクリック
Aspose.Cells for .NETを使ったトップ10条件付き書式の追加方法
Aspose.Cellsは、Microsoft Excel 2007以降の条件付き書式をXLSX形式のセルに対して実行時に完全にサポートします。この例では、異なる属性のトップ10条件付き書式の演習を示しています。
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
private void TestTop10() | |
{ | |
// Instantiate a workbook object | |
Workbook book = new Workbook(); | |
// Create a worksheet object and get the first worksheet | |
Worksheet _sheet = book.Worksheets[0]; | |
AddTop10_1(_sheet); | |
AddTop10_2(_sheet); | |
AddTop10_3(_sheet); | |
AddTop10_4(_sheet); | |
book.Save(filePath + "Top10.xlsx"); | |
} | |
// This method implements a simple Top10 conditional formatting type. | |
private void AddTop10_1(Worksheet _sheet) | |
{ | |
FormatConditionCollection conds = GetFormatCondition("A17:C20", Color.Gray, _sheet); | |
int idx = conds.AddCondition(FormatConditionType.Top10); | |
FormatCondition cond = conds[idx]; | |
cond.Style.BackgroundColor = Color.Yellow; | |
cond.Style.Pattern = BackgroundType.Solid; | |
} | |
// This method implements another Top10 conditional formatting type. | |
private void AddTop10_2(Worksheet _sheet) | |
{ | |
FormatConditionCollection conds = GetFormatCondition("A21:C24", Color.Green, _sheet); | |
int idx = conds.AddCondition(FormatConditionType.Top10); | |
FormatCondition cond = conds[idx]; | |
cond.Style.BackgroundColor = Color.Pink; | |
cond.Style.Pattern = BackgroundType.Solid; | |
cond.Top10.IsBottom = true; | |
} | |
// This method implements another Top10 conditional formatting type with some custom attributes. | |
private void AddTop10_3(Worksheet _sheet) | |
{ | |
FormatConditionCollection conds = GetFormatCondition("A25:C28", Color.Orange, _sheet); | |
int idx = conds.AddCondition(FormatConditionType.Top10); | |
FormatCondition cond = conds[idx]; | |
cond.Style.BackgroundColor = Color.Blue; | |
cond.Style.Pattern = BackgroundType.Solid; | |
cond.Top10.IsPercent = true; | |
} | |
// This method implements another Top10 conditional formatting type with some custom attributes. | |
private void AddTop10_4(Worksheet _sheet) | |
{ | |
FormatConditionCollection conds = GetFormatCondition("A29:C32", Color.Gold, _sheet); | |
int idx = conds.AddCondition(FormatConditionType.Top10); | |
FormatCondition cond = conds[idx]; | |
cond.Style.BackgroundColor = Color.Green; | |
cond.Style.Pattern = BackgroundType.Solid; | |
cond.Top10.Rank = 3; | |
} | |
// This method adds formatted conditions. | |
private FormatConditionCollection GetFormatCondition(string cellAreaName, Color color, Worksheet _sheet) | |
{ | |
// Adds an empty conditional formattings | |
int index = _sheet.ConditionalFormattings.Add(); | |
// Get the formatted conditions | |
FormatConditionCollection formatConditions = _sheet.ConditionalFormattings[index]; | |
// Get the cell area calling the custom GetCellAreaByName method | |
CellArea area = GetCellAreaByName(cellAreaName); | |
// Add the formatted conditions cell area. | |
formatConditions.AddArea(area); | |
// Call the custom FillCell method | |
FillCell(cellAreaName, color, _sheet); | |
// Return the formatted conditions | |
return formatConditions; | |
} | |
// This method specifies the cell shading color for the conditional formattings cellarea range. | |
private void FillCell(string cellAreaName, Color color, Worksheet _sheet) | |
{ | |
CellArea area = GetCellAreaByName(cellAreaName); | |
int k = 0; | |
for (int i = area.StartColumn; i <= area.EndColumn; i++) | |
{ | |
for (int j = area.StartRow; j <= area.EndRow; j++) | |
{ | |
Cell c = _sheet.Cells[j, i]; | |
if (!color.IsEmpty) | |
{ | |
Style s = c.GetStyle(); | |
s.ForegroundColor = color; | |
s.Pattern = BackgroundType.Solid; | |
c.SetStyle(s); | |
} | |
// Set some random values to the cells in the cellarea range | |
int value = j + i + k; | |
c.PutValue(value); | |
k++; | |
} | |
} | |
} | |
// This method specifies the CellArea range (start row, start col, end row, end col etc.) | |
// For the conditional formatting | |
internal static CellArea GetCellAreaByName(string s) | |
{ | |
CellArea area = new CellArea(); | |
string[] strCellRange = s.Replace("$", "").Split(':'); | |
int column; | |
CellsHelper.CellNameToIndex(strCellRange[0], out area.StartRow, out column); | |
area.StartColumn = column; | |
if (strCellRange.Length == 1) | |
{ | |
area.EndRow = area.StartRow; | |
area.EndColumn = area.StartColumn; | |
} | |
else | |
{ | |
CellsHelper.CellNameToIndex(strCellRange[1], out area.EndRow, out column); | |
area.EndColumn = column; | |
} | |
return area; | |
} |