Setzen Sie bedingte Formate in Excel und ODS Dateien.

Einführung

Bedingte Formatierung ist eine erweiterte Funktion von Microsoft Excel, die es ermöglicht, Formate auf eine Zelle oder einen Zellenbereich anzuwenden und diese Formatierung abhängig vom Wert der Zelle oder vom Wert einer Formel zu ändern. Zum Beispiel können Sie eine Zelle fett gedruckt anzeigen lassen, nur wenn der Wert der Zelle größer als 500 ist. Wenn der Wert der Zelle der Bedingung entspricht, wird das angegebene Format auf die Zelle angewendet. Wenn der Wert der Zelle nicht der Formatbedingung entspricht, wird das Standardformat der Zelle verwendet. In Microsoft Excel wählen Sie Format und dann Bedingte Formatierung, um den Dialog für die bedingte Formatierung zu öffnen.

Aspose.Cells unterstützt die Anwendung von bedingten Formatierungen auf Zellen zur Laufzeit. Dieser Artikel erklärt wie. Er erklärt auch, wie die von Excel für die bedingte Formatierung mit Farbskala verwendete Farbe berechnet wird.

Anwendung von bedingten Formaten

Aspose.Cells unterstützt bedingte Formatierungen auf verschiedene Weise:

  • Verwenden der Designer-Tabelle
  • Verwenden der Kopiermethode
  • Erstellen bedingter Formatierungen zur Laufzeit

Verwenden der Designer-Tabelle

Entwickler können eine Designer-Tabelle erstellen, die bedingte Formatierungen in Microsoft Excel enthält, und dann diese Tabelle mit Aspose.Cells öffnen. Aspose.Cells lädt und speichert die Designer-Tabelle und behält dabei alle bedingten Formatierungseinstellungen bei.

Verwenden der Kopiermethode

Aspose.Cells ermöglicht Entwicklern, bedingte Formatierungseinstellungen von einer Zelle auf eine andere im Arbeitsblatt zu kopieren, indem sie die Methode Range.Copy() aufrufen.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(dataDir + "Book1.xlsx", FileMode.Open);
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Copying conditional format settings from cell "A1" to cell "B1"
//worksheet.CopyConditionalFormatting(0, 0, 0, 1);
int TotalRowCount = 0;
for (int i = 0; i < workbook.Worksheets.Count; i++)
{
Worksheet sourceSheet = workbook.Worksheets[i];
Range sourceRange = sourceSheet.Cells.MaxDisplayRange;
Range destRange = worksheet.Cells.CreateRange(sourceRange.FirstRow + TotalRowCount, sourceRange.FirstColumn,
sourceRange.RowCount, sourceRange.ColumnCount);
destRange.Copy(sourceRange);
TotalRowCount = sourceRange.RowCount + TotalRowCount;
}
// Saving the modified Excel file
workbook.Save(dataDir + "output.xls");
// Closing the file stream to free all resources
fstream.Close();

Bedingte Formatierung zur Laufzeit anwenden

Mit Aspose.Cells können Sie bedingte Formatierungen zur Laufzeit sowohl hinzufügen als auch entfernen. Die unten stehenden Codebeispiele zeigen, wie bedingte Formatierungen festgelegt werden:

  1. Instanziieren Sie ein Arbeitsbuch.
  2. Fügen Sie eine leere bedingte Formatierung hinzu.
  3. Legen Sie den Bereich fest, auf den die Formatierung angewendet werden soll.
  4. Definieren Sie die Formatierungsbedingungen.
  5. Speichern Sie die Datei.

Nach diesem Beispiel folgen mehrere kleinere Beispiele, die zeigen, wie Schriftart-Einstellungen, Rand-Einstellungen und Muster angewendet werden.

Microsoft Excel 2007 hat eine erweiterte bedingte Formatierung hinzugefügt, die auch von Aspose.Cells unterstützt wird. Die hier aufgeführten Beispiele zeigen, wie einfache Formatierungen verwendet werden. Die Beispiele für Microsoft Excel 2007 zeigen, wie erweiterte bedingte Formatierungen angewendet werden.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
string filePath = dataDir + "Book1.xlsx";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];
// Adds an empty conditional formatting
int index = sheet.ConditionalFormattings.Add();
FormatConditionCollection fcs = sheet.ConditionalFormattings[index];
// Sets the conditional format range.
CellArea ca = new CellArea();
ca.StartRow = 0;
ca.EndRow = 0;
ca.StartColumn = 0;
ca.EndColumn = 0;
fcs.AddArea(ca);
ca = new CellArea();
ca.StartRow = 1;
ca.EndRow = 1;
ca.StartColumn = 1;
ca.EndColumn = 1;
fcs.AddArea(ca);
// Adds condition.
int conditionIndex = fcs.AddCondition(FormatConditionType.CellValue, OperatorType.Between, "=A2", "100");
// Adds condition.
int conditionIndex2 = fcs.AddCondition(FormatConditionType.CellValue, OperatorType.Between, "50", "100");
// Sets the background color.
FormatCondition fc = fcs[conditionIndex];
fc.Style.BackgroundColor = Color.Red;
// Saving the Excel file
workbook.Save(dataDir + "output.xls");

Schriftart festlegen

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Excel object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Hello Aspose!");
// Obtaining the style of the cell
Style style = cell.GetStyle();
// Setting the font weight to bold
style.Font.IsBold = true;
// Applying the style to the cell
cell.SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls", SaveFormat.Excel97To2003);

Rahmen festlegen

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
public static void Run()
{
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];
// Adds an empty conditional formatting
int index = sheet.ConditionalFormattings.Add();
FormatConditionCollection fcs = sheet.ConditionalFormattings[index];
// Sets the conditional format range.
CellArea ca = new CellArea();
ca.StartRow = 0;
ca.EndRow = 5;
ca.StartColumn = 0;
ca.EndColumn = 3;
fcs.AddArea(ca);
// Adds condition.
int conditionIndex = fcs.AddCondition(FormatConditionType.CellValue, OperatorType.Between, "50", "100");
// Sets the background color.
FormatCondition fc = fcs[conditionIndex];
fc.Style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Dashed;
fc.Style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Dashed;
fc.Style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Dashed;
fc.Style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Dashed;
fc.Style.Borders[BorderType.LeftBorder].Color = Color.FromArgb(0, 255, 255);
fc.Style.Borders[BorderType.RightBorder].Color = Color.FromArgb(0, 255, 255);
fc.Style.Borders[BorderType.TopBorder].Color = Color.FromArgb(0, 255, 255);
fc.Style.Borders[BorderType.BottomBorder].Color = Color.FromArgb(255, 255, 0);
workbook.Save(dataDir + "output.xlsx");
}

Muster festlegen

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];
// Adds an empty conditional formatting
int index = sheet.ConditionalFormattings.Add();
FormatConditionCollection fcs = sheet.ConditionalFormattings[index];
// Sets the conditional format range.
CellArea ca = new CellArea();
ca.StartRow = 0;
ca.EndRow = 5;
ca.StartColumn = 0;
ca.EndColumn = 3;
fcs.AddArea(ca);
// Adds condition.
int conditionIndex = fcs.AddCondition(FormatConditionType.CellValue, OperatorType.Between, "50", "100");
FormatCondition fc = fcs[conditionIndex];
fc.Style.Pattern = BackgroundType.ReverseDiagonalStripe;
fc.Style.ForegroundColor = Color.FromArgb(255, 255, 0);
fc.Style.BackgroundColor = Color.FromArgb(0, 255, 255);
workbook.Save(dataDir + "output.xlsx");

Erweiterte Themen