Zeilen und Spalten automatisch anpassen
Automatische Anpassung
Aspose.Cells für Python via .NET bietet eine Workbook-Klasse, die eine Microsoft Excel-Datei repräsentiert. Die Workbook-Klasse enthält eine worksheets-Sammlung, die den Zugriff auf jede Arbeitsmappe in einer Excel-Datei ermöglicht. Eine Arbeitsmappe wird durch die Worksheet-Klasse repräsentiert. Die Worksheet-Klasse bietet eine Vielzahl von Eigenschaften und Methoden zur Verwaltung einer Arbeitsmappe. Dieser Artikel beschäftigt sich mit der Verwendung der Worksheet-Klasse zur automatischen Anpassung von Reihen oder Spalten.
AutoFit Zeile - Einfach
Der einfachste Ansatz, um die Breite und Höhe einer Zeile automatisch anzupassen, ist das Aufrufen der Methode auto_fit_row der Klasse Worksheet. Die Methode auto_fit_row erhält einen Zeilenindex (der zu ändernden Zeile) als Parameter.
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(".") | |
InputPath = dataDir + "Book1.xlsx" | |
# Creating a file stream containing the Excel file to be opened | |
fstream = open(InputPath, "rb") | |
# Opening the Excel file through the file stream | |
workbook = Workbook(fstream) | |
# Accessing the first worksheet in the Excel file | |
worksheet = workbook.worksheets[0] | |
# Auto-fitting the 3rd row of the worksheet | |
worksheet.auto_fit_row(1) | |
# Saving the modified Excel file | |
workbook.save(dataDir + "output.xlsx") | |
# Closing the file stream to free all resources | |
fstream.close() |
Wie man eine Zeile in einem Zellenbereich automatisch anpasst
Eine Zeile besteht aus vielen Spalten. Aspose.Cells für Python via .NET ermöglicht Entwicklern, eine Zeile anhand des Inhalts in einem Zellenbereich innerhalb der Zeile automatisch anzupassen, indem sie eine überladene Version der Methode auto_fit_row aufrufen. Diese Methode nimmt die folgenden Parameter an:
- Zeilenindex, der Index der zu automatisch anzupassenden Zeile.
- Erster Spaltenindex, der Index der ersten Spalte der Zeile.
- Letzter Spaltenindex, der Index der letzten Spalte der Zeile.
Die Methode auto_fit_row prüft die Inhalte aller Spalten in der Zeile und passt dann die Zeile automatisch an.
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(".") | |
InputPath = dataDir + "Book1.xlsx" | |
# Creating a file stream containing the Excel file to be opened | |
fstream = open(InputPath, "rb") | |
# Opening the Excel file through the file stream | |
workbook = Workbook(fstream) | |
# Accessing the first worksheet in the Excel file | |
worksheet = workbook.worksheets[0] | |
# Auto-fitting the 3rd row of the worksheet | |
worksheet.auto_fit_row(1, 0, 5) | |
# Saving the modified Excel file | |
workbook.save(dataDir + "output.xlsx") | |
# Closing the file stream to free all resources | |
fstream.close() |
Wie man eine Spalte in einem Zellenbereich automatisch anpasst
Eine Spalte besteht aus vielen Zeilen. Es ist möglich, eine Spalte basierend auf dem Inhalt in einem Zellenbereich in der Spalte automatisch anzupassen, indem eine überladene Version der Methode auto_fit_column aufgerufen wird, die die folgenden Parameter verwendet:
- Spaltenindex, der Index der zu automatisch anzupassenden Spalte.
- Erster Zeilenindex, der Index der ersten Zeile der Spalte.
- Letzter Zeilenindex, der Index der letzten Zeile der Spalte.
Die auto_fit_column-Methode überprüft den Inhalt aller Zeilen in der Spalte und passt die Spalte dann automatisch an.
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(".") | |
InputPath = dataDir + "Book1.xlsx" | |
# Creating a file stream containing the Excel file to be opened | |
fstream = open(InputPath, "rb") | |
# Opening the Excel file through the file stream | |
workbook = Workbook(fstream) | |
# Accessing the first worksheet in the Excel file | |
worksheet = workbook.worksheets[0] | |
# Auto-fitting the Column of the worksheet | |
worksheet.auto_fit_column(4, 4, 6) | |
# Saving the modified Excel file | |
workbook.save(dataDir + "output.xlsx") | |
# Closing the file stream to free all resources | |
fstream.close() |
Wie man Zeilen für zusammengeführte Zellen automatisch anpasst
Mit Aspose.Cells for Python via .NET ist es möglich, Zeilen auch für Zellen automatisch anzupassen, die mithilfe der API AutoFitterOptions zusammengeführt wurden. Die Klasse AutoFitterOptions bietet die Eigenschaft auto_fit_merged_cells_type, die zum automatischen Anpassen von Zeilen für zusammengeführte Zellen verwendet werden kann. auto_fit_merged_cells_type akzeptiert eine AutoFitMergedCellsType-Auflistung, die die folgenden Elemente enthält.
- NONE: Zusammengeführte Zellen ignorieren.
- FIRST_LINE: Erweitert nur die Höhe der ersten Zeile.
- LAST_LINE: Erweitert nur die Höhe der letzten Zeile.
- EACH_LINE: Erweitert nur die Höhe jeder Zeile.
from aspose.cells import AutoFitMergedCellsType, AutoFitterOptions, Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Output directory | |
outputDir = RunExamples.Get_OutputDirectory() | |
# Instantiate a new Workbook | |
wb = Workbook() | |
# Get the first (default) worksheet | |
_worksheet = wb.worksheets[0] | |
# Create a range A1:B1 | |
range = _worksheet.cells.create_range(0, 0, 1, 2) | |
# Merge the cells | |
range.merge() | |
# Insert value to the merged cell A1 | |
_worksheet.cells.get(0, 0).value = "A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog....end" | |
# Create a style object | |
style = _worksheet.cells.get(0, 0).get_style() | |
# Set wrapping text on | |
style.is_text_wrapped = True | |
# Apply the style to the cell | |
_worksheet.cells.get(0, 0).set_style(style) | |
# Create an object for AutoFitterOptions | |
options = AutoFitterOptions() | |
# Set auto-fit for merged cells | |
options.auto_fit_merged_cells_type = AutoFitMergedCellsType.EACH_LINE | |
# Autofit rows in the sheet(including the merged cells) | |
_worksheet.auto_fit_rows(options) | |
# Save the Excel file | |
wb.save(outputDir + "AutofitRowsforMergedCells.xlsx") |
Sie können auch die überladenen Versionen der auto_fit_rows- und auto_fit_columns-Methoden verwenden, die einen Bereich von Zeilen/Spalten und eine Instanz von AutoFitterOptions akzeptieren, um die ausgewählten Zeilen/Spalten entsprechend Ihrer gewünschten AutoFitterOptions automatisch anzupassen.
Die Signaturen der genannten Methoden lauten wie folgt: