Cómo agregar formato condicional de Barras de Datos

Escenarios de uso posibles

Usar Barras de Datos en formato condicional es una forma poderosa (¡y visual!) de entender tus datos de un vistazo.

  1. Comparación Visual de Valores: Las barras de datos convierten los números en barras horizontales, facilitando mucho la comparación de valores uno al lado del otro — ¡como un mini gráfico de barras dentro de tus celdas!
  2. Reconocimiento Inmediato de Patrones: Puedes ver instantáneamente los picos, valles y valores atípicos sin ordenar o escanear números.
  3. Mejor Legibilidad: Especialmente útil en tablas largas — reduce la carga cognitiva y te ayuda a captar rápidamente las tendencias clave.
  4. Dinámico y en Tiempo Real: A medida que cambian los valores, las barras se actualizan automáticamente — ideal para monitorear métricas en vivo, progreso o KPI.
  5. Paneles de Control de Aspecto Profesional: Añade un aspecto limpio, moderno y pulido a informes o paneles.

Cómo agregar formato condicional de Barras de Datos usando Excel

Para agregar formato condicional de Barras de Datos en Excel, así es como puedes hacerlo paso a paso:

  1. Selecciona tu rango de datos, por ejemplo: C2:C20 — esto podría ser ventas, puntuaciones o valores de progreso.
  2. Ve a la pestaña Inicio en la cinta de opciones.
  3. Haz clic en Formato condicional en el grupo Estilos.
  4. Pasa el cursor sobre Barras de Datos.
  5. Elige un estilo: Relleno con Degradado (las barras se desvanecen de izquierda a derecha) y Relleno Sólido (las barras tienen un color sólido).
  6. Haz clic en el estilo que deseas — ¡y listo!

Cómo agregar formato condicional de Barras de Datos usando Aspose.Cells for .NET

Aspose.Cells admite completamente el formato condicional proporcionado por Microsoft Excel 2007 y versiones posteriores en formato XLSX en las celdas en tiempo de ejecución. Este ejemplo demuestra un ejercicio para el formato condicional de Barras de Datos con diferentes conjuntos de atributos.

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;
}