Zellen formatieren

Einführung

Formatieren Sie Cells mit den Methoden GetStyle und SetStyle

Wenden Sie verschiedene Arten von Formatierungsstilen auf Zellen an, um Hintergrund- oder Vordergrundfarben, Rahmen, Schriftarten, horizontale und vertikale Ausrichtung, Einzugsebene, Textrichtung, Drehwinkel und vieles mehr festzulegen.

Verwenden der GetStyle- und SetStyle-Methoden

Wenn Entwickler unterschiedliche Formatierungsstile auf verschiedene Zellen anwenden müssen, ist es besser, dieStil der Zelle mitCell.GetStyle -Methode, geben Sie die Stilattribute an und wenden Sie dann die Formatierung anCell.SetStyleMethode. Ein Beispiel ist unten angegeben, um diesen Ansatz zu veranschaulichen, um verschiedene Formatierungen auf eine Zelle anzuwenden.

// 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();
// Obtaining the reference of the first worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Hello Aspose!");
// Defining a Style object
Aspose.Cells.Style style;
// Get the style from A1 cell
style = cell.GetStyle();
// Setting the vertical alignment
style.VerticalAlignment = TextAlignmentType.Center;
// Setting the horizontal alignment
style.HorizontalAlignment = TextAlignmentType.Center;
// Setting the font color of the text
style.Font.Color = Color.Green;
// Setting to shrink according to the text contained in it
style.ShrinkToFit = true;
// Setting the bottom border color to red
style.Borders[BorderType.BottomBorder].Color = Color.Red;
// Setting the bottom border type to medium
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Medium;
// Applying the style to A1 cell
cell.SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls");

Verwenden des Stilobjekts zum Formatieren verschiedener Cells

Wenn Entwickler denselben Formatierungsstil auf verschiedene Zellen anwenden müssen, können sie verwendenStil Objekt. Bitte befolgen Sie die nachstehenden Schritte, um die zu verwendenStilObjekt:

  1. Füge hinzu einStil Objekt durch Aufrufen derCreateStyle Methode derArbeitsmappeKlasse
  2. Greifen Sie auf die neu hinzugefügten zuStilObjekt
  3. Stellen Sie die gewünschten Eigenschaften/Attribute der einStilObjekt, um die gewünschten Formatierungseinstellungen anzuwenden
  4. Weisen Sie die konfigurierte zuStilObjekt zu Ihren gewünschten Zellen

Dieser Ansatz kann die Effizienz Ihrer Anwendungen erheblich verbessern und auch Speicher sparen.

// 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 first worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Hello Aspose!");
// Adding a new Style
Style style = workbook.CreateStyle();
// Setting the vertical alignment of the text in the "A1" cell
style.VerticalAlignment = TextAlignmentType.Center;
// Setting the horizontal alignment of the text in the "A1" cell
style.HorizontalAlignment = TextAlignmentType.Center;
// Setting the font color of the text in the "A1" cell
style.Font.Color = Color.Green;
// Shrinking the text to fit in the cell
style.ShrinkToFit = true;
// Setting the bottom border color of the cell to red
style.Borders[BorderType.BottomBorder].Color = Color.Red;
// Setting the bottom border type of the cell to medium
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Medium;
// Assigning the Style object to the "A1" cell
cell.SetStyle(style);
// Apply the same style to some other cells
worksheet.Cells["B1"].SetStyle(style);
worksheet.Cells["C1"].SetStyle(style);
worksheet.Cells["D1"].SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls");

Verwenden von Microsoft Excel 2007 vordefinierte Stile

Wenn Sie verschiedene Formatierungsstile für Microsoft Excel 2007 anwenden müssen, wenden Sie Stile mit dem Aspose.Cells API an. Ein Beispiel wird unten gegeben, um diesen Ansatz zu demonstrieren, um einen vordefinierten Stil auf eine Zelle anzuwenden.

// 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);
// Instantiate a new Workbook.
Workbook workbook = new Workbook();
// Create a style object .
Style style = workbook.CreateStyle();
// Input a value to A1 cell.
workbook.Worksheets[0].Cells["A1"].PutValue("Test");
// Apply the style to the cell.
workbook.Worksheets[0].Cells["A1"].SetStyle(style);
// Save the Excel 2007 file.
workbook.Save(dataDir + "book1.out.xlsx");

Ausgewählte Zeichen in Cell formatieren

Der Umgang mit Schrifteinstellungen erklärt, wie Text in Zellen formatiert wird, aber es erklärt nur, wie der gesamte Zellinhalt formatiert wird. Was ist, wenn Sie nur ausgewählte Zeichen formatieren möchten?

Aspose.Cells unterstützt diese Funktion ebenfalls. In diesem Thema wird erläutert, wie wir diese Funktion effektiv nutzen.

Ausgewählte Zeichen formatieren

Aspose.Cells bietet eine Klasse,Arbeitsmappe das stellt eine Microsoft Excel-Datei dar. DasArbeitsmappe Klasse enthält dieArbeitsblätter Sammlung, die den Zugriff auf jedes Arbeitsblatt in einer Excel-Datei ermöglicht. Ein Arbeitsblatt wird durch dargestelltArbeitsblatt Klasse. DasArbeitsblatt Klasse bietet aCells Sammlung. Jeder Artikel in derCells Sammlung stellt ein Objekt derCellKlasse.

DasCell Klasse bietet dieZeichenMethode, die die folgenden Parameter benötigt, um einen Bereich von Zeichen in einer Zelle auszuwählen:

  • Startindex, der Index des Zeichens, bei dem die Auswahl beginnt.
  • Anzahl von Charakteren, die Anzahl der auszuwählenden Zeichen.

DasZeichen -Methode gibt eine Instanz von zurückSchrifteinstellung-Klasse, mit der Entwickler die Zeichen genauso formatieren können wie eine Zelle, wie unten im Codebeispiel gezeigt. In der Ausgabedatei wird in der A1-Zelle das Wort „Besuch“ mit der Standardschrift formatiert, aber „Aspose!“. ist fett und blau.

// 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();
// Obtaining the reference of the first(default) worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[0];
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Visit Aspose!");
// Setting the font of selected characters to bold
cell.Characters(6, 7).Font.IsBold = true;
// Setting the font color of selected characters to blue
cell.Characters(6, 7).Font.Color = Color.Blue;
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls");

Zeilen und Spalten formatieren

Manchmal müssen Entwickler dieselbe Formatierung auf Zeilen oder Spalten anwenden. Das Anwenden der Formatierung auf einzelne Zellen dauert oft länger und ist keine gute Lösung. Um dieses Problem zu beheben, bietet Aspose.Cells eine einfache und schnelle Möglichkeit, die in diesem Artikel ausführlich beschrieben wird.

Zeilen und Spalten formatieren

Aspose.Cells bietet eine Klasse, dieArbeitsmappe das stellt eine Microsoft Excel-Datei dar. DasArbeitsmappe Klasse enthält aArbeitsblätter Sammlung, die den Zugriff auf jedes Arbeitsblatt in der Excel-Datei ermöglicht. Ein Arbeitsblatt wird durch dargestelltArbeitsblatt Klasse. DasArbeitsblatt Klasse bietet aCells Sammlung. DasCellsSammlung bietet aReihenSammlung.

Formatieren einer Zeile

Jeder Artikel in derReihen Sammlung repräsentiert aReihe Objekt. DasReiheObjekt bietet dieStil anwenden Methode zum Festlegen der Formatierung der Zeile. Um dieselbe Formatierung auf eine Zeile anzuwenden, verwenden Sie dieStilObjekt. Die folgenden Schritte zeigen, wie Sie es verwenden.

  1. Füge hinzu einStil Widerspruch gegen dieArbeitsmappe Klasse durch den Aufruf seinerCreateStyleMethode.
  2. Stellen Sie die einStilObjekteigenschaften, um Formatierungseinstellungen anzuwenden.
  3. Setzen Sie die relevanten Attribute für die auf ONStyleFlagObjekt.
  4. Weisen Sie die konfigurierte zuStil Widerspruch gegen dieReiheObjekt.
// 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();
// Obtaining the reference of the first (default) worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[0];
// Adding a new Style to the styles
Style style = workbook.CreateStyle();
// Setting the vertical alignment of the text in the "A1" cell
style.VerticalAlignment = TextAlignmentType.Center;
// Setting the horizontal alignment of the text in the "A1" cell
style.HorizontalAlignment = TextAlignmentType.Center;
// Setting the font color of the text in the "A1" cell
style.Font.Color = Color.Green;
// Shrinking the text to fit in the cell
style.ShrinkToFit = true;
// Setting the bottom border color of the cell to red
style.Borders[BorderType.BottomBorder].Color = Color.Red;
// Setting the bottom border type of the cell to medium
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Medium;
// Creating StyleFlag
StyleFlag styleFlag = new StyleFlag();
styleFlag.HorizontalAlignment = true;
styleFlag.VerticalAlignment = true;
styleFlag.ShrinkToFit = true;
styleFlag.Borders = true;
styleFlag.FontColor = true;
// Accessing a row from the Rows collection
Row row = worksheet.Cells.Rows[0];
// Assigning the Style object to the Style property of the row
row.ApplyStyle(style, styleFlag);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls");

Formatieren einer Spalte

DasCells Sammlung bietet auch aSäulen Sammlung. Jeder Artikel in derSäulen Sammlung repräsentiert aSpalte Objekt. Ähnlich wie einReihe Objekt, dasSpalte Objekt bietet auch dieStil anwendenMethode zum Formatieren einer Spalte.

// 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();
// Obtaining the reference of the first (default) worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[0];
// Adding a new Style to the styles
Style style = workbook.CreateStyle();
// Setting the vertical alignment of the text in the "A1" cell
style.VerticalAlignment = TextAlignmentType.Center;
// Setting the horizontal alignment of the text in the "A1" cell
style.HorizontalAlignment = TextAlignmentType.Center;
// Setting the font color of the text in the "A1" cell
style.Font.Color = Color.Green;
// Shrinking the text to fit in the cell
style.ShrinkToFit = true;
// Setting the bottom border color of the cell to red
style.Borders[BorderType.BottomBorder].Color = Color.Red;
// Setting the bottom border type of the cell to medium
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Medium;
// Creating StyleFlag
StyleFlag styleFlag = new StyleFlag();
styleFlag.HorizontalAlignment = true;
styleFlag.VerticalAlignment = true;
styleFlag.ShrinkToFit = true;
styleFlag.Borders = true;
styleFlag.FontColor = true;
// Accessing a column from the Columns collection
Column column = worksheet.Cells.Columns[0];
// Applying the style to the column
column.ApplyStyle(style, styleFlag);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls");

Themen vorantreiben