Zeilen und Spalten formatieren

Arbeiten mit Zeilen

Wie man die Zeilenhöhe anpasst

Aspose.Cells für Python via .NET bietet eine Klasse, Workbook, die eine Microsoft Excel-Datei repräsentiert. Die Workbook-Klasse enthält eine WorksheetCollection, die 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 alle Zellen im Arbeitsblatt repräsentiert.

Die Cells-Sammlung bietet verschiedene Methoden zum Verwalten von Zeilen oder Spalten in einem Arbeitsblatt. Einige davon werden unten genauer erläutert.

Wie man die Höhe einer Zeile festlegt

Es ist möglich, die Höhe einer einzelnen Zeile durch Aufrufen der Cells-Sammlungsmethode set_row_height festzulegen. Die set_row_height-Methode nimmt die folgenden Parameter wie folgt entgegen:

  • row, der Index der Zeile, deren Höhe geändert werden soll.
  • height, die Höhe der Zeile, die auf die Zeile angewendet werden soll.
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.xls", "rb")
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Setting the height of the second row to 13
worksheet.cells.set_row_height(1, 13)
# Saving the modified Excel file
workbook.save(dataDir + "output.out.xls")
# Closing the file stream to free all resources
fstream.close()

Wie man die Höhe aller Zeilen in einem Arbeitsblatt festlegt

Wenn Entwickler die gleiche Zeilenhöhe für alle Zeilen im Arbeitsblatt festlegen müssen, können sie dies über die standard_height-Eigenschaft der Cells-Sammlung tun.

Beispiel:

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.xls", "rb")
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Setting the height of all rows in the worksheet to 15
worksheet.cells.standard_height = 15.0
# Saving the modified Excel file
workbook.save(dataDir + "output.out.xls")
# Closing the file stream to free all resources
fstream.close()

Arbeiten mit Spalten

Wie man die Breite einer Spalte festlegt

Die Breite einer Spalte wird durch Aufrufen der Cells-Sammlungsmethode set_column_width festgelegt. Die set_column_width-Methode nimmt folgende Parameter entgegen:

  • Spalte, der Index der Spalte, deren Breite Sie ändern.
  • width, die gewünschte Spaltenbreite.
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.xls", "rb")
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Setting the width of the second column to 17.5
worksheet.cells.set_column_width(1, 17.5)
# Saving the modified Excel file
workbook.save(dataDir + "output.out.xls")
# Closing the file stream to free all resources
fstream.close()

Wie man die Spaltenbreite in Pixeln festlegt

Legen Sie die Breite einer Spalte fest, indem Sie die Methode set_column_width_pixel der Cells-Sammlung aufrufen. Die Methode set_column_width_pixel nimmt die folgenden Parameter an:

  • Spalte, der Index der Spalte, deren Breite Sie ändern.
  • Pixel, die gewünschte Spaltenbreite in Pixeln.
from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Source directory
sourceDir = RunExamples.Get_SourceDirectory()
outDir = RunExamples.Get_OutputDirectory()
# Load source Excel file
workbook = Workbook(sourceDir + "Book1.xlsx")
# Access first worksheet
worksheet = workbook.worksheets[0]
# Set the width of the column in pixels
worksheet.cells.set_column_width_pixel(7, 200)
workbook.save(outDir + "SetColumnWidthInPixels_Out.xlsx")

Wie man die Breite aller Spalten in einem Arbeitsblatt festlegt

Um dieselbe Spaltenbreite für alle Spalten im Arbeitsblatt festzulegen, verwenden Sie die Eigenschaft standard_width der Cells-Sammlung.

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)
# Creating a file stream containing the Excel file to be opened
fstream = open(dataDir + "book1.xls", "rb")
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Setting the width of all columns in the worksheet to 20.5
worksheet.cells.standard_width = 20.5
# Saving the modified Excel file
workbook.save(dataDir + "output.out.xls")
# Closing the file stream to free all resources
fstream.close()

Erweiterte Themen