Come aggiungere la formattazione condizionale con barre dati
Possibili Scenari di Utilizzo
Usare le barre dati nella formattazione condizionale è un modo potente (e visivo!) per comprendere i tuoi dati a colpo d’occhio.
- Confronto visivo dei valori: le barre dati trasformano i numeri in barre orizzontali, rendendo molto facile confrontare i valori fianco a fianco — come un mini grafico a barre all’interno delle tue celle!
- Riconoscimento immediato dei pattern: puoi vedere istantaneamente massimi, minimi e outlier senza ordinare o scansionare i numeri.
- Migliore leggibilità: particolarmente utile in tabelle lunghe — riduce il carico cognitivo e aiuta a cogliere rapidamente le tendenze chiave.
- Dinamico & in tempo reale: man mano che i valori cambiano, le barre si aggiornano automaticamente — ottimo per monitorare metriche, progresso o KPI in tempo reale.
- Dashboard dal look professionale: aggiunge un aspetto pulito, moderno e curato ai report o ai dashboard.
Come aggiungere la formattazione condizionale con barre dati usando Excel
Per aggiungere la formattazione condizionale con barre dati in Excel, ecco come puoi farlo passo passo:
- Seleziona l’intervallo di dati, ad esempio: C2:C20 — questo potrebbe essere vendite, punteggi o valori di progresso.
- Vai alla scheda Home sulla barra multifunzione.
- Clicca su Formattazione condizionale nel gruppo Stili.
- Passa sopra le Barre Dati.
- Scegli uno stile: Riempimento gradiente (le barre sfumano da sinistra a destra) e Riempimento solido (le barre hanno un colore solido).
- Clicca sullo stile che preferisci — e hai finito!
Come aggiungere la formattazione condizionale con barre dati usando Aspose.Cells for .NET
Aspose.Cells supporta completamente la formattazione condizionale fornita da Microsoft Excel 2007 e versioni successive in formato XLSX sui celle in tempo di esecuzione. Questo esempio mostra un esercizio per la formattazione condizionale con barre dati con diversi insiemi di attributi.
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; | |
} |