Einfügen und Löschen von Zeilen und Spalten einer Excel Datei

Einführung

Beim Erstellen eines neuen Arbeitsblatts von Grund auf oder bei der Arbeit an einem vorhandenen Arbeitsblatt müssen möglicherweise zusätzliche Zeilen oder Spalten hinzugefügt werden, um mehr Daten aufzunehmen. Umgekehrt können auch Zeilen oder Spalten von bestimmten Positionen im Arbeitsblatt gelöscht werden. Um diese Anforderungen zu erfüllen, bietet Aspose.Cells für Python via .NET eine sehr einfache Reihe von Klassen und Methoden, die unten diskutiert werden.

Zeilen und Spalten verwalten

Aspose.Cells für Python via .NET bietet eine Klasse Workbook, die eine Microsoft Excel-Datei darstellt. Die Klasse Workbook enthält eine worksheets-Sammlung, die den Zugriff auf jedes Arbeitsblatt in einer Excel-Datei ermöglicht. Ein Arbeitsblatt wird durch die Klasse Worksheet dargestellt. Die Klasse Worksheet bietet eine cells-Sammlung, die alle Zellen im Arbeitsblatt darstellt.

Die cells-Sammlung bietet mehrere Methoden zur Verwaltung von Zeilen und Spalten in einem Arbeitsblatt. Einige davon werden unten diskutiert.

Zeilen und Spalten einfügen

Wie man eine Zeile einfügt

Fügen Sie eine Zeile in das Arbeitsblatt an einer beliebigen Stelle ein, indem Sie die Methode insert_row der Sammlung cells aufrufen. Die Methode insert_row übernimmt den Index der Zeile, an der die neue Zeile eingefügt wird.

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]
# Inserting a row into the worksheet at 3rd position
worksheet.cells.insert_row(2)
# Saving the modified Excel file
workbook.save(dataDir + "output.out.xls")
# Closing the file stream to free all resources
fstream.close()

Wie man mehrere Zeilen einfügt

Um mehrere Zeilen in ein Arbeitsblatt einzufügen, rufen Sie die Methode insert_rows der Sammlung cells auf. Die Methode insert_rows übernimmt zwei Parameter:

  • Zeilenindex, der Index der Zeile, ab der die neuen Zeilen eingefügt werden.
  • Anzahl der Zeilen, die insgesamt eingefügt werden müssen.
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]
# Inserting 10 rows into the worksheet starting from 3rd row
worksheet.cells.insert_rows(2, 10)
# Saving the modified Excel file
workbook.save(dataDir + "output.out.xls")
# Closing the file stream to free all resources
fstream.close()

Wie man eine Zeile mit Formatierung einfügt

Um eine Zeile mit Formatierungsoptionen einzufügen, verwenden Sie die Überladung von insert_rows, die InsertOptions als Parameter akzeptiert. Setzen Sie die copy_format_type-Eigenschaft der Klasse InsertOptions mit der CopyFormatType-Enumeration. Die CopyFormatType-Enumeration hat drei unten aufgeführte Elemente.

  • SAME_AS_ABOVE: Formatiert die Zeile genauso wie die darüberliegende Zeile.
  • SAME_AS_BELOW: Formatiert die Zeile genauso wie die darunterliegende Zeile.
  • CLEAR: Löscht die Formatierung.
from aspose.cells import CopyFormatType, InsertOptions, 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 Formatting options
insertOptions = InsertOptions()
insertOptions.copy_format_type = CopyFormatType.SAME_AS_ABOVE
# Inserting a row into the worksheet at 3rd position
worksheet.cells.insert_rows(2, 1, insertOptions)
# Saving the modified Excel file
workbook.save(dataDir + "InsertingARowWithFormatting.out.xls")
# Closing the file stream to free all resources
fstream.close()

Wie man eine Spalte einfügt

Entwickler können auch eine Spalte in das Arbeitsblatt an beliebiger Stelle einfügen, indem sie die Methode insert_column der Sammlung cells aufrufen. Die Methode insert_column erfordert den Index der Spalte, in die die neue Spalte eingefügt 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]
# Inserting a column into the worksheet at 2nd position
worksheet.cells.insert_column(1)
# Saving the modified Excel file
workbook.save(dataDir + "output.out.xls")
# Closing the file stream to free all resources
fstream.close()

Zeilen und Spalten löschen

Wie man mehrere Zeilen löscht

Um mehrere Zeilen aus einem Arbeitsblatt zu löschen, rufen Sie die Methode delete_rows der Sammlung cells auf. Die Methode delete_rows erfordert zwei Parameter:

  • Zeilenindex, der Index der Zeile, ab der die Zeilen gelöscht werden.
  • Anzahl der Zeilen, die insgesamt gelöscht werden müssen.
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.xlsx", "wb")
# 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]
# Deleting 10 rows from the worksheet starting from 3rd row
worksheet.cells.delete_rows(2, 10)
# Saving the modified Excel file
workbook.save(dataDir + "output.xlsx")
# Closing the file stream to free all resources
fstream.close()

Wie man eine Spalte löscht

Um eine Spalte aus dem Arbeitsblatt an beliebiger Stelle zu löschen, rufen Sie die Methode delete_column der Sammlung Cells auf. Die Methode delete_column erfordert den Index der zu löschenden Spalte.

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.xlsx", "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]
# Deleting a column from the worksheet at 5th position
worksheet.cells.delete_column(4)
# Saving the modified Excel file
workbook.save(dataDir + "output.xlsx")
# Closing the file stream to free all resources
fstream.close()