データ棒条件付き書式の追加方法
Contents
[
Hide
]
可能な使用シナリオ
条件付き書式でデータ棒を使用することは、データを一目で理解するための効果的(そして視覚的!)な方法です。
- 値のビジュアル比較:データ棒は数字を横棒に変え、値を並べて比較しやすくします — セル内のミニバーグラフのように!
- 即時パターン認識:高値、低値、外れ値を並べ替えや数値のスキャンなしで瞬時に把握できます。
- 読みやすさ向上:特に長い表で役立つ — 認知負荷を軽減し、主要な傾向を素早く理解できます。
- 動的かつリアルタイム:数値が変わるとバーも自動で更新されます — リアルタイムのメトリクス、進捗やKPIの追跡に最適です。
- プロフェッショナルなダッシュボード:レポートやダッシュボードに清潔感のあるモダンな仕上がりを追加します。
Excelでデータ棒条件付き書式を追加する方法
Excelにデータ棒条件付き書式を追加するには、次の手順で行います:
- データ範囲を選択します。例:C2:C20 — 販売、スコア、進捗値など。
- リボンのホームタブに移動します。
- スタイルグループの条件付き書式をクリックします。
- データ棒にカーソルを合わせます。
- スタイルを選択します:グラデーション塗りつぶし(棒が左から右に徐々にフェード)とソリッド塗りつぶし(棒に単色)。
- 好みのスタイルをクリックして終了です!
** 条件付き書式のデータバーを追加する方法(Aspose.Cells for .NET)**
Aspose.Cellsは、ランタイム時にセルの条件付き書式設定をサポートしており、Microsoft Excel 2007以降のバージョンが提供するXLSX形式に対応しています。この例は、DataBars条件付き書式のためのさまざまな属性セットを使用した演習を示しています。
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 TestDataBar() | |
{ | |
// Instantiate a workbook object | |
Workbook book = new Workbook(); | |
// Create a worksheet object and get the first worksheet | |
Worksheet _sheet = book.Worksheets[0]; | |
AddDataBar1(_sheet); | |
AddDataBar2(_sheet); | |
book.Save(filePath + "DataBar.xlsx"); | |
} | |
// This method implements the DataBars conditional formatting type with Percentile attribute. | |
private void AddDataBar2(Worksheet _sheet) | |
{ | |
FormatConditionCollection conds = GetFormatCondition("E3:G4", Color.LightGreen, _sheet); | |
int idx = conds.AddCondition(FormatConditionType.DataBar); | |
FormatCondition cond = conds[idx]; | |
cond.DataBar.Color = Color.Orange; | |
cond.DataBar.MinCfvo.Type = FormatConditionValueType.Percentile; | |
cond.DataBar.MinCfvo.Value = 30.78; | |
cond.DataBar.ShowValue = false; | |
} | |
// This method implements the DataBars conditional formatting type. | |
private void AddDataBar1(Worksheet _sheet) | |
{ | |
FormatConditionCollection conds = GetFormatCondition("E1:G2", Color.YellowGreen, _sheet); | |
int idx = conds.AddCondition(FormatConditionType.DataBar); | |
FormatCondition cond = conds[idx]; | |
} | |
// 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; | |
} |