Zellen formatieren

Einführung

Wie man Zellen mit den Methoden GetStyle und SetStyle formatiert

Auf Zellen verschiedene Arten von Formatierungsstilen anwenden, um Hintergrund- oder Vordergrundfarben, Rahmen, Schriftarten, horizontale und vertikale Ausrichtungen, Einrückungsebene, Textausrichtung, Drehwinkel und vieles mehr festzulegen.

Wie man die GetStyle und SetStyle Methoden verwendet

Wenn Entwickler unterschiedliche Formatierungsstile auf verschiedene Zellen anwenden müssen, ist es besser, den Style der Zelle mit der Cell.get_style Methode abzurufen, die Style-Attribute anzugeben und dann die Formatierung mit der Cell.set_style Methode anzuwenden. Ein Beispiel ist unten gegeben, um diesen Ansatz zur Anwendung verschiedener Formatierungen auf einer Zelle zu demonstrieren.

from aspose.cells import BorderType, CellBorderType, TextAlignmentType, Workbook
from aspose.pydrawing import Color
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()
# Obtaining the reference of the first worksheet
worksheet = workbook.worksheets[0]
# Accessing the "A1" cell from the worksheet
cell = worksheet.cells.get("A1")
# Adding some value to the "A1" cell
cell.put_value("Hello Aspose!")
# Get the style from A1 cell
style = cell.get_style()
# Setting the vertical alignment
style.vertical_alignment = TextAlignmentType.CENTER
# Setting the horizontal alignment
style.horizontal_alignment = 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.shrink_to_fit = True
# Setting the bottom border color to red
style.borders.get(BorderType.BOTTOM_BORDER).color = Color.red
# Setting the bottom border type to medium
style.borders.get(BorderType.BOTTOM_BORDER).line_style = CellBorderType.MEDIUM
# Applying the style to A1 cell
cell.set_style(style)
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls")

Wie man das Style-Objekt verwendet, um verschiedene Zellen zu formatieren

Wenn Entwickler denselben Formatierungsstil auf verschiedenen Zellen anwenden müssen, können sie das Style Objekt verwenden. Bitte folgen Sie den unten stehenden Schritten, um das Style Objekt zu verwenden:

  1. Fügen Sie ein Style Objekt hinzu, indem Sie die create_style Methode der Workbook Klasse aufrufen
  2. Greifen Sie auf das neu hinzugefügte Style Objekt zu
  3. Setzen Sie die gewünschten Eigenschaften/Attribute des Style Objekts, um die gewünschten Formatierungseinstellungen anzuwenden
  4. Weisen Sie den konfigurierten Style Objekt Ihren gewünschten Zellen zu

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

from aspose.cells import BorderType, CellBorderType, TextAlignmentType, Workbook
from aspose.pydrawing import Color
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 first 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!")
# Adding a new Style
style = workbook.create_style()
# Setting the vertical alignment of the text in the "A1" cell
style.vertical_alignment = TextAlignmentType.CENTER
# Setting the horizontal alignment of the text in the "A1" cell
style.horizontal_alignment = 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.shrink_to_fit = True
# Setting the bottom border color of the cell to red
style.borders.get(BorderType.BOTTOM_BORDER).color = Color.red
# Setting the bottom border type of the cell to medium
style.borders.get(BorderType.BOTTOM_BORDER).line_style = CellBorderType.MEDIUM
# Assigning the Style object to the "A1" cell
cell.set_style(style)
# Apply the same style to some other cells
worksheet.cells.get("B1").set_style(style)
worksheet.cells.get("C1").set_style(style)
worksheet.cells.get("D1").set_style(style)
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls")

Wie man die Microsoft Excel 2007 vordefinierten Stile verwendet

Wenn Sie unterschiedliche Formatierungsstile für Microsoft Excel 2007 anwenden müssen, nutzen Sie die Styles mit der Aspose.Cells für Python via .NET API. Unten ist ein Beispiel, um diesen Ansatz zu demonstrieren, wie man einen vordefinierten Stil auf eine Zelle anwendet.

from aspose.cells import 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)
# Instantiate a new Workbook.
workbook = Workbook()
# Create a style object .
style = workbook.create_style()
# Input a value to A1 cell.
workbook.worksheets[0].cells.get("A1").put_value("Test")
# Apply the style to the cell.
workbook.worksheets[0].cells.get("A1").set_style(style)
# Save the Excel 2007 file.
workbook.save(dataDir + "book1.out.xlsx")

Wie man ausgewählte Zeichen in einer Zelle formatiert

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

Aspose.Cells für Python via .NET unterstützt diese Funktion ebenfalls. Dieser Abschnitt erklärt, wie man diese Funktion effektiv nutzt.

Wie man ausgewählte Zeichen formatiert

Aspose.Cells für Python via .NET bietet eine Klasse, Workbook, die eine Microsoft Excel-Datei repräsentiert. Die Workbook-Klasse enthält die worksheets-Sammlung, die den Zugriff auf jedes Arbeitsblatt in einer Excel-Datei ermöglicht. Ein Arbeitsblatt wird durch die Worksheet-Klasse repräsentiert. Die Worksheet-Klasse bietet eine cells-Sammlung. Jedes Element in der cells-Sammlung stellt ein Objekt der Cell-Klasse dar.

Die Cell Klasse bietet die characters Methode, die folgende Parameter annimmt, um eine Reihe von Zeichen innerhalb einer Zelle auszuwählen:

  • Startindex: Der Index des Zeichens, von dem die Auswahl beginnt.
  • Anzahl der Zeichen: Die Anzahl der ausgewählten Zeichen.

Die characters Methode gibt eine Instanz der FontSetting Klasse zurück, die Entwicklern ermöglicht, die Zeichen genauso zu formatieren, wie sie eine Zelle formatieren würden, wie unten im Beispielcode gezeigt. In der Ausgabedatei wird im A1-Zelle das Wort ‘Besuchen’ mit der Standardschriftart formatiert, aber ‘Aspose!’ ist fett und blau.

from aspose.cells import Workbook
from aspose.pydrawing import Color
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()
# Obtaining the reference of the first(default) worksheet by passing its sheet index
worksheet = workbook.worksheets[0]
# Accessing the "A1" cell from the worksheet
cell = worksheet.cells.get("A1")
# Adding some value to the "A1" cell
cell.put_value("Visit Aspose!")
# Setting the font of selected characters to bold
cell.characters(6, 7).font.is_bold = 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")

Wie man Zeilen und Spalten formatiert

Manchmal müssen Entwickler dieselbe Formatierung auf Zeilen oder Spalten anwenden. Die Formatierung einzelner Zellen nacheinander dauert oft länger und ist keine gute Lösung. Um dieses Problem zu lösen, bietet Aspose.Cells für Python via .NET eine einfache, schnelle Methode, die in diesem Artikel ausführlich erläutert wird.

Formatierung von Zeilen & Spalten

Aspose.Cells für Python via .NET bietet eine Klasse, die Workbook heißt, welche eine Microsoft Excel-Datei repräsentiert. Die Workbook-Klasse enthält eine worksheets-Sammlung, die den Zugriff auf jedes Arbeitsblatt in der Excel-Datei ermöglicht. Ein Arbeitsblatt wird durch die Worksheet-Klasse repräsentiert. Die Worksheet-Klasse bietet eine cells-Sammlung. Die cells-Sammlung stellt eine rows-Sammlung bereit.

Wie man eine Zeile formatiert

Jedes Element in der rows-Sammlung repräsentiert ein Row-Objekt. Das Row-Objekt bietet die apply_style-Methode zum Festlegen der Formatierung der Zeile. Um dieselbe Formatierung auf eine Zeile anzuwenden, verwenden Sie das Style-Objekt. Die folgenden Schritte zeigen, wie es verwendet wird.

  1. Fügen Sie ein Style-Objekt zur Workbook-Klasse hinzu, indem Sie ihre create_style-Methode aufrufen.
  2. Legen Sie die Eigenschaften des Style-Objekts fest, um Formatierungseinstellungen anzuwenden.
  3. Schalten Sie die relevanten Attribute für das StyleFlag-Objekt EIN.
  4. Weisen Sie das konfigurierte Style-Objekt der Row-Klasse zu.
from aspose.cells import BorderType, CellBorderType, StyleFlag, TextAlignmentType, Workbook
from aspose.pydrawing import Color
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()
# Obtaining the reference of the first (default) worksheet by passing its sheet index
worksheet = workbook.worksheets[0]
# Adding a new Style to the styles
style = workbook.create_style()
# Setting the vertical alignment of the text in the "A1" cell
style.vertical_alignment = TextAlignmentType.CENTER
# Setting the horizontal alignment of the text in the "A1" cell
style.horizontal_alignment = 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.shrink_to_fit = True
# Setting the bottom border color of the cell to red
style.borders.get(BorderType.BOTTOM_BORDER).color = Color.red
# Setting the bottom border type of the cell to medium
style.borders.get(BorderType.BOTTOM_BORDER).line_style = CellBorderType.MEDIUM
# Creating StyleFlag
styleFlag = StyleFlag()
styleFlag.horizontal_alignment = True
styleFlag.vertical_alignment = True
styleFlag.shrink_to_fit = True
styleFlag.borders = True
styleFlag.font_color = True
# Accessing a row from the Rows collection
row = worksheet.cells.rows[0]
# Assigning the Style object to the Style property of the row
row.apply_style(style, styleFlag)
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls")

Wie man eine Spalte formatiert

Die cells-Sammlung bietet auch eine columns-Sammlung. Jedes Element in der columns-Sammlung repräsentiert ein Column-Objekt. Ähnlich wie ein Row-Objekt, bietet das Column-Objekt auch die apply_style-Methode zur Formatierung einer Spalte.

from aspose.cells import BorderType, CellBorderType, StyleFlag, TextAlignmentType, Workbook
from aspose.pydrawing import Color
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()
# Obtaining the reference of the first (default) worksheet by passing its sheet index
worksheet = workbook.worksheets[0]
# Adding a new Style to the styles
style = workbook.create_style()
# Setting the vertical alignment of the text in the "A1" cell
style.vertical_alignment = TextAlignmentType.CENTER
# Setting the horizontal alignment of the text in the "A1" cell
style.horizontal_alignment = 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.shrink_to_fit = True
# Setting the bottom border color of the cell to red
style.borders.get(BorderType.BOTTOM_BORDER).color = Color.red
# Setting the bottom border type of the cell to medium
style.borders.get(BorderType.BOTTOM_BORDER).line_style = CellBorderType.MEDIUM
# Creating StyleFlag
styleFlag = StyleFlag()
styleFlag.horizontal_alignment = True
styleFlag.vertical_alignment = True
styleFlag.shrink_to_fit = True
styleFlag.borders = True
styleFlag.font_color = True
# Accessing a column from the Columns collection
column = worksheet.cells.columns[0]
# Applying the style to the column
column.apply_style(style, styleFlag)
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls")

Erweiterte Themen