Arbeitsblätter von Microsoft Excel Dateien verwalten

Aspose.Cells bietet eine Klasse, Workbook, die eine Excel-Datei repräsentiert. Die Klasse Workbook enthält eine Sammlung von worksheets, die den Zugriff auf jedes Arbeitsblatt in der Excel-Datei ermöglicht.

Ein Arbeitsblatt wird durch die Klasse Worksheet repräsentiert. Die Klasse Worksheet bietet eine Vielzahl von Eigenschaften und Methoden zum Verwalten von Arbeitsblättern.

Hinzufügen von Arbeitsblättern zu einer neuen Excel-Datei

Um programmgesteuert eine neue Excel-Datei zu erstellen:

  1. Erstellen Sie ein Objekt der Klasse Workbook.
  2. Rufen Sie die Methode add der Klasse WorksheetCollection auf. Ein leeres Arbeitsblatt wird automatisch zur Excel-Datei hinzugefügt. Es kann durch Übergeben des Blattindex des neuen Arbeitsblatts an die Sammlung worksheets referenziert werden.
  3. Holen Sie sich eine Arbeitsblatt-Referenz.
  4. Arbeiten Sie an den Arbeitsblättern.
  5. Speichern Sie die neue Excel-Datei mit neuen Arbeitsblättern, indem Sie die Methode save der Klasse Workbook aufrufen.
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)
# Instantiating a Workbook object
workbook = Workbook()
# Adding a new worksheet to the Workbook object
i = workbook.worksheets.add()
# Obtaining the reference of the newly added worksheet by passing its sheet index
worksheet = workbook.worksheets[i]
# Setting the name of the newly added worksheet
worksheet.name = "My Worksheet"
# Saving the Excel file
workbook.save(dataDir + "output.out.xls")

Hinzufügen von Arbeitsblättern zu einer Designer-Tabelle

Der Prozess zum Hinzufügen von Arbeitsblättern zu einem Designer-Arbeitsblatt ist derselbe wie das Hinzufügen eines neuen Arbeitsblatts, mit der Ausnahme, dass die Excel-Datei bereits existiert und daher geöffnet werden sollte, bevor Arbeitsblätter hinzugefügt werden. Ein Designer-Arbeitsblatt kann durch die Klasse Workbook geöffnet werden.

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)
# Adding a new worksheet to the Workbook object
i = workbook.worksheets.add()
# Obtaining the reference of the newly added worksheet by passing its sheet index
worksheet = workbook.worksheets[i]
# Setting the name of the newly added worksheet
worksheet.name = "My Worksheet"
# Saving the Excel file
workbook.save(dataDir + "output.xlsx")

Arbeitsblätter über Blattnamen abrufen

Greifen Sie auf jedes Arbeitsblatt zu, indem Sie dessen Namen oder Index angeben.

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")
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Accessing a worksheet using its sheet name
worksheet = workbook.worksheets.get("Sheet1")
cell = worksheet.cells.get("A1")
print(cell.value)

Arbeitsblätter über Blattnamen entfernen

Um Arbeitsblätter aus einer Datei zu entfernen, rufen Sie die Methode remove_by_name der Klasse WorksheetCollection auf. Geben Sie den Blattnamen an die Methode remove_by_name an, um ein bestimmtes Arbeitsblatt zu entfernen.

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)
# Removing a worksheet using its sheet name
workbook.worksheets.remove_by_name("Sheet1")
# Save workbook
workbook.save(dataDir + "output.out.xls")

Arbeitsblätter über Blattindex entfernen

Das Entfernen von Arbeitsblättern nach Namen funktioniert gut, wenn der Name des Arbeitsblatts bekannt ist. Wenn der Arbeitsblattname nicht bekannt ist, verwenden Sie die Methode remove_by_index, die den Tabellenindex des Arbeitsblatts anstelle seines Tabellennamens akzeptiert.

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)
# Removing a worksheet using its sheet index
workbook.worksheets.remove_by_index(0)
# Save workbook
workbook.save(dataDir + "output.out.xls")

Blätter aktivieren und eine Zelle in Arbeitsblatt aktivieren

Manchmal benötigen Sie ein bestimmtes Arbeitsblatt, das aktiv und angezeigt wird, wenn ein Benutzer eine Microsoft Excel-Datei in Excel öffnet. Ebenso möchten Sie möglicherweise eine bestimmte Zelle aktivieren und die Bildlaufleisten so einstellen, dass die aktive Zelle angezeigt wird. Aspose.Cells ist in der Lage, all diese Aufgaben zu erledigen.

Ein aktives Tabellenblatt ist ein Blatt, an dem Sie arbeiten: Der Name des aktiven Blattes auf der Registerkarte ist standardmäßig fett gedruckt.

Eine aktive Zelle ist eine ausgewählte Zelle, in die Daten eingegeben werden, wenn Sie mit der Eingabe beginnen. Es ist jeweils nur eine Zelle aktiv. Die aktive Zelle ist durch einen starken Rahmen hervorgehoben.

Blätter aktivieren und eine Zelle aktivieren

Aspose.Cells bietet spezifische API-Aufrufe zur Aktivierung eines Blattes und einer Zelle an. Zum Beispiel ist die Eigenschaft Aspose.Cells.WorksheetCollection.active_sheet_index nützlich, um das aktive Blatt in einer Arbeitsmappe festzulegen. Ebenso wird die Eigenschaft Aspose.Cells.Worksheet.active_cell verwendet, um eine aktive Zelle im Arbeitsblatt festzulegen und abzurufen.

Um sicherzustellen, dass die horizontalen oder vertikalen Bildlaufleisten auf die Zeilen- und Spaltenindexposition eingestellt sind, um bestimmte Daten anzuzeigen, verwenden Sie die Eigenschaften Aspose.Cells.Worksheet.first_visible_row und Aspose.Cells.Worksheet.first_visible_column.

Das folgende Beispiel zeigt, wie ein Arbeitsblatt aktiviert und eine aktive Zelle darin markiert wird. In der generierten Ausgabe werden die Bildlaufleisten gescrollt, um die 2. Zeile und 2. Spalte als erste sichtbare Zeile und Spalte zu zeigen.

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(".")
# Instantiate a new Workbook.
workbook = Workbook()
# Get the first worksheet in the workbook.
worksheet1 = workbook.worksheets[0]
# Get the cells in the worksheet.
cells = worksheet1.cells
# Input data into B2 cell.
cells.get(1, 1).put_value("Hello World!")
# Set the first sheet as an active sheet.
workbook.worksheets.active_sheet_index = 0
# Set B2 cell as an active cell in the worksheet.
worksheet1.active_cell = "B2"
# Set the B column as the first visible column in the worksheet.
worksheet1.first_visible_column = 1
# Set the 2nd row as the first visible row in the worksheet.
worksheet1.first_visible_row = 1
# Save the excel file.
workbook.save(dataDir + "output.xls")