Legen Sie bedingte Formate von Excel- und ODS-Dateien fest.
Einführung
Die bedingte Formatierung ist eine erweiterte Microsoft Excel-Funktion, mit der Sie Formate auf eine Zelle oder einen Zellbereich anwenden und diese Formatierung abhängig vom Wert der Zelle oder dem Wert einer Formel ändern können. Beispielsweise können Sie eine Zelle nur dann fett darstellen lassen, wenn der Wert der Zelle größer als 500 ist. Wenn der Wert der Zelle die Bedingung erfüllt, wird das angegebene Format auf die Zelle angewendet. Wenn der Wert der Zelle die Formatbedingung nicht erfüllt, wird die Standardformatierung der Zelle verwendet. Wählen Sie in Microsoft Excel ausFormat , dannBedingte Formatierung , um das Dialogfeld „Bedingte Formatierung“ zu öffnen.
Aspose.Cells unterstützt die Anwendung von bedingter Formatierung auf Zellen zur Laufzeit. Dieser Artikel erklärt, wie. Außerdem wird erläutert, wie die von Excel für die bedingte Formatierung der Farbskala verwendete Farbe berechnet wird.
Bedingte Formatierung anwenden
Aspose.Cells unterstützt die bedingte Formatierung auf verschiedene Weise:
- Verwenden von Designer-Tabellenkalkulationen
- Verwenden der Kopiermethode.
- Bedingte Formatierung zur Laufzeit erstellen.
Verwenden von Designer-Tabellenkalkulation
Entwickler können ein Designer-Arbeitsblatt mit bedingter Formatierung in Microsoft Excel erstellen und dieses Arbeitsblatt dann mit Aspose.Cells öffnen. Aspose.Cells lädt und speichert das Designer-Arbeitsblatt, wobei alle Einstellungen für die bedingte Formatierung beibehalten werden.
Verwenden der Kopiermethode
Aspose.Cells ermöglicht es Entwicklern, bedingte Formateinstellungen von einer Zelle in eine andere im Arbeitsblatt zu kopieren, indem sie dieRange.Copy() Methode.
// 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(); | |
Anwenden der bedingten Formatierung zur Laufzeit
Aspose.Cells können Sie bedingte Formatierung zur Laufzeit hinzufügen und entfernen. Die folgenden Codebeispiele zeigen, wie Sie die bedingte Formatierung festlegen:
- Instanziieren Sie eine Arbeitsmappe.
- Fügen Sie ein leeres bedingtes Format hinzu.
- Legen Sie den Bereich fest, für den die Formatierung gelten soll.
- Definieren Sie die Formatierungsbedingungen.
- Speicher die Datei.
Nach diesem Beispiel folgen einige weitere kleinere Beispiele, die zeigen, wie Schrifteinstellungen, Rahmeneinstellungen und Muster angewendet werden.
Microsoft Excel 2007 hat erweiterte bedingte Formatierung hinzugefügt, die Aspose.Cells ebenfalls unterstützt. Die Beispiele hier veranschaulichen, wie einfache Formatierung verwendet wird, die Microsoft Excel 2007-Beispiele zeigen, wie fortgeschrittenere bedingte Formatierung angewendet wird.
// 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); |
Grenze setzen
// 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 setzen
// 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"); |
Themen vorantreiben
- Bedingte Formatierungen für 2-Farben-Skala und 3-Farben-Skala hinzufügen
- Wenden Sie die erweiterte bedingte Formatierung an
- Wenden Sie bedingte Formatierung in Arbeitsblättern an
- Wenden Sie Schattierung auf abwechselnde Zeilen und Spalten mit bedingter Formatierung an
- Generieren Sie DataBars-Bilder mit bedingter Formatierung
- Holen Sie sich Symbolsätze, Datenbalken oder Farbskalenobjekte, die in der bedingten Formatierung verwendet werden