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 für Python via .NET unterstützt die Anwendung bedingter Formatierungen zur Laufzeit. Dieser Artikel erklärt, wie das funktioniert. Er erklärt auch, wie die von Excel verwendete Farbe für Farbskalen-Bedingungsformatierungen berechnet wird.
Anwendung von bedingten Formaten
Aspose.Cells für Python via .NET unterstützt bedingte Formatierungen auf verschiedene Weisen:
- 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 diese Tabelle dann mit Aspose.Cells für Python via .NET öffnen. Aspose.Cells für Python via .NET lädt und speichert die Designer-Tabelle und bewahrt alle Einstellungen der bedingten Formatierung.
Verwenden der Kopiermethode
Aspose.Cells für Python via .NET ermöglicht es Entwicklern, bedingte Formatierungen von einer Zelle auf eine andere in der Arbeitsmappe zu kopieren, indem die Range.copy()-Methode aufgerufen wird.
from aspose.cells import Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Creating a file stream containing the Excel file to be opened | |
fstream = open(dataDir + "Book1.xlsx", "rb") | |
# Opening the Excel file through the file stream | |
workbook = Workbook(fstream) | |
# Accessing the first worksheet in the Excel file | |
worksheet = workbook.worksheets[0] | |
# Copying conditional format settings from cell "A1" to cell "B1" | |
# worksheet.CopyConditionalFormatting(0, 0, 0, 1); | |
TotalRowCount = 0 | |
for i in range(len(workbook.worksheets)): | |
sourceSheet = workbook.worksheets[i] | |
sourceRange = sourceSheet.cells.max_display_range | |
destRange = worksheet.cells.create_range(sourceRange.first_row + TotalRowCount, sourceRange.first_column, sourceRange.row_count, sourceRange.column_count) | |
destRange.copy(sourceRange) | |
TotalRowCount = sourceRange.row_count + 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
Aspose.Cells für Python via .NET erlaubt das Hinzufügen und Entfernen von bedingten Formatierungen zur Laufzeit. Die nachfolgenden Code-Beispiele zeigen, wie man bedingte Formatierungen festlegt:
- Instanziieren Sie ein Arbeitsbuch.
- Fügen Sie eine leere bedingte Formatierung hinzu.
- Legen Sie den Bereich fest, auf den die Formatierung angewendet werden soll.
- Definieren Sie die Formatierungsbedingungen.
- 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 führte fortschrittlichere bedingte Formatierungen ein, die ebenfalls von Aspose.Cells für Python via .NET unterstützt werden. Die Beispiele hier demonstrieren die Verwendung einfacher Formatierungen, während die Microsoft Excel 2007-Beispiele zeigen, wie man komplexere bedingte Formatierungen anwendet.
from aspose.cells import CellArea, FormatConditionType, OperatorType, Workbook | |
from aspose.pydrawing import Color | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
filePath = dataDir + "Book1.xlsx" | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
sheet = workbook.worksheets[0] | |
# Adds an empty conditional formatting | |
index = sheet.conditional_formattings.add() | |
fcs = sheet.conditional_formattings[index] | |
# Sets the conditional format range. | |
ca = CellArea() | |
ca.start_row = 0 | |
ca.end_row = 0 | |
ca.start_column = 0 | |
ca.end_column = 0 | |
fcs.add_area(ca) | |
ca = CellArea() | |
ca.start_row = 1 | |
ca.end_row = 1 | |
ca.start_column = 1 | |
ca.end_column = 1 | |
fcs.add_area(ca) | |
# Adds condition. | |
conditionIndex = fcs.add_condition(FormatConditionType.CELL_VALUE, OperatorType.BETWEEN, "=A2", "100") | |
# Adds condition. | |
conditionIndex2 = fcs.add_condition(FormatConditionType.CELL_VALUE, OperatorType.BETWEEN, "50", "100") | |
# Sets the background color. | |
fc = fcs[conditionIndex] | |
fc.style.background_color = Color.red | |
# Saving the Excel file | |
workbook.save(dataDir + "output.xls") |
Schriftart festlegen
from aspose.cells import SaveFormat, Workbook | |
from os import os, path | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Create directory if it is not already present. | |
IsExists = path.isdir(dataDir) | |
if notIsExists: | |
os.makedirs(dataDir) | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Adding a new worksheet to the Excel object | |
i = workbook.worksheets.add() | |
# Obtaining the reference of the newly added worksheet by passing its sheet index | |
worksheet = workbook.worksheets[i] | |
# Accessing the "A1" cell from the worksheet | |
cell = worksheet.cells.get("A1") | |
# Adding some value to the "A1" cell | |
cell.put_value("Hello Aspose!") | |
# Obtaining the style of the cell | |
style = cell.get_style() | |
# Setting the font weight to bold | |
style.font.is_bold = True | |
# Applying the style to the cell | |
cell.set_style(style) | |
# Saving the Excel file | |
workbook.save(dataDir + "book1.out.xls", SaveFormat.EXCEL_97_TO_2003) |
Rahmen festlegen
from aspose.cells import BorderType, CellArea, CellBorderType, FormatConditionType, OperatorType, Workbook | |
from aspose.pydrawing import Color | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
sheet = workbook.worksheets[0] | |
# Adds an empty conditional formatting | |
index = sheet.conditional_formattings.add() | |
fcs = sheet.conditional_formattings[index] | |
# Sets the conditional format range. | |
ca = CellArea() | |
ca.start_row = 0 | |
ca.end_row = 5 | |
ca.start_column = 0 | |
ca.end_column = 3 | |
fcs.add_area(ca) | |
# Adds condition. | |
conditionIndex = fcs.add_condition(FormatConditionType.CELL_VALUE, OperatorType.BETWEEN, "50", "100") | |
# Sets the background color. | |
fc = fcs[conditionIndex] | |
fc.style.borders.get(BorderType.LEFT_BORDER).line_style = CellBorderType.DASHED | |
fc.style.borders.get(BorderType.RIGHT_BORDER).line_style = CellBorderType.DASHED | |
fc.style.borders.get(BorderType.TOP_BORDER).line_style = CellBorderType.DASHED | |
fc.style.borders.get(BorderType.BOTTOM_BORDER).line_style = CellBorderType.DASHED | |
fc.style.borders.get(BorderType.LEFT_BORDER).color = Color.from_argb(0, 255, 255) | |
fc.style.borders.get(BorderType.RIGHT_BORDER).color = Color.from_argb(0, 255, 255) | |
fc.style.borders.get(BorderType.TOP_BORDER).color = Color.from_argb(0, 255, 255) | |
fc.style.borders.get(BorderType.BOTTOM_BORDER).color = Color.from_argb(255, 255, 0) | |
workbook.save(dataDir + "output.xlsx") |
Muster festlegen
from aspose.cells import BackgroundType, CellArea, FormatConditionType, OperatorType, Workbook | |
from aspose.pydrawing import Color | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
sheet = workbook.worksheets[0] | |
# Adds an empty conditional formatting | |
index = sheet.conditional_formattings.add() | |
fcs = sheet.conditional_formattings[index] | |
# Sets the conditional format range. | |
ca = CellArea() | |
ca.start_row = 0 | |
ca.end_row = 5 | |
ca.start_column = 0 | |
ca.end_column = 3 | |
fcs.add_area(ca) | |
# Adds condition. | |
conditionIndex = fcs.add_condition(FormatConditionType.CELL_VALUE, OperatorType.BETWEEN, "50", "100") | |
fc = fcs[conditionIndex] | |
fc.style.pattern = BackgroundType.REVERSE_DIAGONAL_STRIPE | |
fc.style.foreground_color = Color.from_argb(255, 255, 0) | |
fc.style.background_color = Color.from_argb(0, 255, 255) | |
workbook.save(dataDir + "output.xlsx") |
Erweiterte Themen
- Hinzufügen von 2-Farben-Skalen und 3-Farben-Skalen bedingter Formatierungen
- Bedingte Formatierung in Arbeitsblättern anwenden
- Abwechselnde Zeilen und Spalten mit bedingter Formatierung schattieren
- Generieren von Datenleistungsimages für bedingte Formatierungen
- Erhalten von Symbolsets, Datenleisten oder Farbskalenobjekten, die bei der bedingten Formatierung verwendet werden