Wie man Text Bedingte Formatierung hinzufügt

Mögliche Verwendungsszenarien

Die textbasierte bedingte Formatierung in Tabellenkalkulationen ist nützlich, um Zellen hervorzuheben, die bestimmte textuelle Kriterien erfüllen. Dies kann die Datenanalyse verbessern und es erleichtern, wichtige Informationen in einem großen Datensatz zu finden. Hier sind einige Gründe für die Verwendung von Text-Bedingter Formatierung:

  1. Spezifischen Text hervorheben: Sie können Formatierungen basierend auf bestimmten Wörtern, Phrasen oder Zeichen anwenden. Zum Beispiel möchten Sie alle Zellen hervorheben, die das Wort “Urgent” oder “Completed” enthalten, um Aufgaben in einem Projekt leicht zu unterscheiden.
  2. Muster oder Trends erkennen: Wenn Sie mit Kategorien oder Status arbeiten (wie “Hoch”, “Mittel”, “Niedrig”), kann die textbasierte bedingte Formatierung diese visuell unterscheiden, was die Fortschrittsverfolgung oder Priorisierung erleichtert.
  3. Fehler- oder Dateneingabenwarnungen: Textformatierungen können inkonsistente oder fehlerhafte Eingaben kennzeichnen, wie falsch geschriebene Wörter, unvollständiger Text oder falsche Werte. Dies ist besonders in Datensätzen mit viel Texteingaben nützlich.
  4. Verbesserte Lesbarkeit: Farbcodierung von Text oder Änderung seines Stils (fett, kursiv usw.) hilft, wichtige Informationen hervorzuheben und verbessert die Gesamtlesbarkeit Ihres Blattes.
  5. Dynamisches Feedback: Sie können Regeln einrichten, die die Formatierung automatisch anpassen, wenn Text bestimmte Bedingungen erfüllt. Das bedeutet, dass Sie die Formatierung nicht manuell aktualisieren müssen, wenn sich die Daten ändern.

Kurz gesagt, hilft Ihnen die bedingte Textformatierung schnell relevante Informationen, Fehler und Trends zu erkennen und ist ein leistungsstarkes Werkzeug für das Management und die Interpretation von Textdaten.

Wie man Text-Bedingte Formatierung mit Excel hinzufügt

Um die textbasierte bedingte Formatierung in Excel hinzuzufügen, folgen Sie diesen Schritten:

  1. Zellenbereich auswählen: Markieren Sie die Zellen, auf die Sie die bedingte Formatierung anwenden möchten.
  2. Öffnen Sie das Menü für bedingte Formatierung: Gehen Sie zum Start-Tab im Excel-Ribbons. Klicken Sie auf „Bedingte Formatierung“ in der Gruppe „Stile“.
  3. Wählen Sie „Neue Regel“: Wählen Sie im Dropdown-Menü „Neue Regel“.
  4. Wählen Sie „Nur Zellen formatieren, die enthalten“: Im Dialogfeld für Neue Formatierungsregel wählen Sie „Nur Zellen formatieren, die enthalten“ unter dem Abschnitt „Regeltyp auswählen“.
  5. Legen Sie die Regelkriterien fest: Im Abschnitt “Formatieren Sie Zellen mit” wählen Sie aus dem Dropdown “Bestimmter Text”. Wählen Sie entweder “enthalten”, “beginnen mit” oder “enden mit”, je nach Bedingung, die Sie anwenden möchten. Geben Sie den Text ein, den Sie formatieren möchten (z.B. ein bestimmtes Wort wie “Dringend” oder “Abgeschlossen”).
  6. Format auswählen: Klicken Sie auf die Schaltfläche Format. Im Dialogfeld Zellen formatieren können Sie die Schriftfarbe, Füllfarbe oder andere Formatierungsoptionen nach Wunsch auswählen.
  7. Regel anwenden: Sobald Sie das gewünschte Format eingestellt haben, klicken Sie auf OK, um die Regel anzuwenden. Klicken Sie erneut auf OK im Dialogfeld Neue Formatierungsregel, um es zu schließen.
  8. Ergebnisse anzeigen: Die Zellen, die den angegebenen Text enthalten, werden jetzt entsprechend formatiert, was das Erkennen relevanter Informationen erleichtert.

So fügen Sie bedingte Textformatierung mit Aspose.Cells for .NET hinzu

Aspose.Cells unterstützt vollständig die bedingte Formatierung, die von Microsoft Excel 2007 und neueren Versionen im XLSX-Format auf Zellen während der Laufzeit bereitgestellt wird. Dieses Beispiel zeigt eine Übung für fortgeschrittene bedingte Formatierungstypen, einschließlich BeginsWith, ContainsBlank, ContainsText und mehr.

Zelle formatieren, wenn der Wert mit bestimmtem Text beginnt

// This method implements the BeginsWith conditional formatting type.
private void AddBeginWith()
{
// Instantiate a workbook object
Workbook book = new Workbook();
// Create a worksheet object and get the first worksheet
Worksheet _sheet = book.Worksheets[0];
FormatConditionCollection conds = GetFormatCondition("E15:G16", Color.LightGoldenrodYellow, _sheet);
int idx = conds.AddCondition(FormatConditionType.BeginsWith);
FormatCondition cond = conds[idx];
cond.Style.BackgroundColor = Color.Pink;
cond.Style.Pattern = BackgroundType.Solid;
cond.Text = "ab";
Cell c = _sheet.Cells["E15"];
c.PutValue("abc");
c = _sheet.Cells["G16"];
c.PutValue("babx");
book.Save("BeginsWith.xlsx");
}
// 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;
}

Zelle formatieren, wenn der Wert Leer enthält

// This method implements the ContainsBlank conditional formatting type.
private void AddContainsBlank()
{
// Instantiate a workbook object
Workbook book = new Workbook();
// Create a worksheet object and get the first worksheet
Worksheet _sheet = book.Worksheets[0];
FormatConditionCollection conds = GetFormatCondition("E9:G10", Color.LightBlue, _sheet);
int idx = conds.AddCondition(FormatConditionType.ContainsBlanks);
FormatCondition cond = conds[idx];
cond.Style.BackgroundColor = Color.Yellow;
cond.Style.Pattern = BackgroundType.Solid;
Cell c = _sheet.Cells["E9"];
c.PutValue(" ");
c = _sheet.Cells["G10"];
c.PutValue(" ");
book.Save("ContainsBlank.xlsx");
}

Zelle formatieren, wenn der Wert Fehler enthält

// This method implements the ContainsErrors conditional formatting type.
private void AddContainsErrors()
{
// Instantiate a workbook object
Workbook book = new Workbook();
// Create a worksheet object and get the first worksheet
Worksheet _sheet = book.Worksheets[0];
FormatConditionCollection conds = GetFormatCondition("E17:G18", Color.LightSkyBlue, _sheet);
int idx = conds.AddCondition(FormatConditionType.ContainsErrors);
FormatCondition cond = conds[idx];
cond.Style.BackgroundColor = Color.Yellow;
cond.Style.Pattern = BackgroundType.Solid;
Cell c = _sheet.Cells["E17"];
c.PutValue(" ");
c = _sheet.Cells["G18"];
c.PutValue(" ");
book.Save("ContainsErrors.xlsx");
}

Zelle formatieren, wenn der Wert bestimmten Text enthält

// This method implements the ContainsText conditional formatting type.
private void AddContainsText()
{
// Instantiate a workbook object
Workbook book = new Workbook();
// Create a worksheet object and get the first worksheet
Worksheet _sheet = book.Worksheets[0];
FormatConditionCollection conds = GetFormatCondition("E5:G6", Color.LightBlue, _sheet);
int idx = conds.AddCondition(FormatConditionType.ContainsText);
FormatCondition cond = conds[idx];
cond.Style.BackgroundColor = Color.Yellow;
cond.Style.Pattern = BackgroundType.Solid;
cond.Text = "1";
book.Save("ContainsText.xlsx");
}

Zelle formatieren, wenn der Wert doppelte Werte enthält

// This method implements the DuplicateValues conditional formatting type.
private void AddDuplicateValues()
{
// Instantiate a workbook object
Workbook book = new Workbook();
// Create a worksheet object and get the first worksheet
Worksheet _sheet = book.Worksheets[0];
FormatConditionCollection conds = GetFormatCondition("E23:G24", Color.LightSlateGray, _sheet);
int idx = conds.AddCondition(FormatConditionType.DuplicateValues);
FormatCondition cond = conds[idx];
cond.Style.BackgroundColor = Color.Pink;
cond.Style.Pattern = BackgroundType.Solid;
Cell c = _sheet.Cells["E23"];
c.PutValue("bb");
c = _sheet.Cells["G24"];
c.PutValue("bb");
book.Save("DuplicateValues.xlsx");
}

Zelle formatieren, wenn der Wert mit bestimmtem Text endet

// This method implements the EndsWith conditional formatting type.
private void AddEndWith()
{
// Instantiate a workbook object
Workbook book = new Workbook();
// Create a worksheet object and get the first worksheet
Worksheet _sheet = book.Worksheets[0];
FormatConditionCollection conds = GetFormatCondition("E13:G14", Color.LightGray, _sheet);
int idx = conds.AddCondition(FormatConditionType.EndsWith);
FormatCondition cond = conds[idx];
cond.Style.BackgroundColor = Color.Yellow;
cond.Style.Pattern = BackgroundType.Solid;
cond.Text = "ab";
Cell c = _sheet.Cells["E13"];
c.PutValue("nnnab");
c = _sheet.Cells["G14"];
c.PutValue("mmmabc");
book.Save("EndsWith.xlsx");
}

Zelle formatieren, wenn der Wert Leer nicht enthält

// This method implements the NotContainsBlank conditional formatting type.
private void AddNotContainsBlank()
{
// Instantiate a workbook object
Workbook book = new Workbook();
// Create a worksheet object and get the first worksheet
Worksheet _sheet = book.Worksheets[0];
FormatConditionCollection conds = GetFormatCondition("E11:G12", Color.LightCoral, _sheet);
int idx = conds.AddCondition(FormatConditionType.NotContainsBlanks);
FormatCondition cond = conds[idx];
cond.Style.BackgroundColor = Color.Pink;
cond.Style.Pattern = BackgroundType.Solid;
Cell c = _sheet.Cells["E11"];
c.PutValue("abc");
c = _sheet.Cells["G12"];
c.PutValue(" ");
book.Save("NotContainsBlank.xlsx");
}

Zelle formatieren, wenn der Wert Fehler nicht enthält

// This method implements the NotContainsErrors conditional formatting type.
private void AddNotContainsErrors()
{
// Instantiate a workbook object
Workbook book = new Workbook();
// Create a worksheet object and get the first worksheet
Worksheet _sheet = book.Worksheets[0];
FormatConditionCollection conds = GetFormatCondition("E19:G20", Color.LightSeaGreen, _sheet);
int idx = conds.AddCondition(FormatConditionType.NotContainsErrors);
FormatCondition cond = conds[idx];
cond.Style.BackgroundColor = Color.Pink;
cond.Style.Pattern = BackgroundType.Solid;
Cell c = _sheet.Cells["E19"];
c.PutValue(" ");
c = _sheet.Cells["G20"];
c.PutValue(" ");
book.Save("NotContainsErrors.xlsx");
}

Zelle formatieren, wenn der Wert bestimmten Text nicht enthält

// This method implements the NotContainsText conditional formatting type.
private void AddNotContainsText()
{
// Instantiate a workbook object
Workbook book = new Workbook();
// Create a worksheet object and get the first worksheet
Worksheet _sheet = book.Worksheets[0];
FormatConditionCollection conds = GetFormatCondition("E7:G8", Color.LightCoral, _sheet);
int idx = conds.AddCondition(FormatConditionType.NotContainsText);
FormatCondition cond = conds[idx];
cond.Style.BackgroundColor = Color.Pink;
cond.Style.Pattern = BackgroundType.Solid;
cond.Text = "3";
book.Save("NotContainsText.xlsx");
}

Zelle formatieren, wenn der Wert eindeutige Werte enthält

// This method implements the UniqueValues conditional formatting type.
private void AddUniqueValues()
{
// Instantiate a workbook object
Workbook book = new Workbook();
// Create a worksheet object and get the first worksheet
Worksheet _sheet = book.Worksheets[0];
FormatConditionCollection conds = GetFormatCondition("E21:G22", Color.LightSalmon, _sheet);
int idx = conds.AddCondition(FormatConditionType.UniqueValues);
FormatCondition cond = conds[idx];
cond.Style.BackgroundColor = Color.Yellow;
cond.Style.Pattern = BackgroundType.Solid;
Cell c = _sheet.Cells["E21"];
c.PutValue("aa");
c = _sheet.Cells["G22"];
c.PutValue("aa");
book.Save("UniqueValues.xlsx");
}