Verschiedene Möglichkeiten, Dateien zu öffnen

Wie man eine Excel-Datei über einen Pfad öffnet

Entwickler können eine Microsoft Excel-Datei anhand ihres Dateipfads auf dem lokalen Computer öffnen, indem sie sie im Konstruktor der Workbook-Klasse angeben. Übergeben Sie einfach den Pfad als String im Konstruktor. Aspose.Cells for Python via .NET erkennt automatisch den Dateiformattyp.

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(".")
# Opening through Path
# Creating a Workbook object and opening an Excel file using its file path
workbook1 = Workbook(dataDir + "Book1.xlsx")
print("Workbook opened using path successfully!")

Wie man eine Excel-Datei über einen Stream öffnet

Es ist auch einfach, eine Excel-Datei als Stream zu öffnen. Verwenden Sie dazu eine überladene Version des Konstruktors, die das Stream-Objekt enthält, das die Datei enthält.

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(".")
# Opening through Stream
# Create a Stream object
fstream = open(dataDir + "Book2.xls", "rb")
# Creating a Workbook object, open the file from a Stream object
# That contains the content of file and it should support seeking
workbook2 = Workbook(fstream)
print("Workbook opened using stream successfully!")
fstream.close()

Wie man eine Datei nur mit Daten öffnet

Um eine Datei nur mit den Daten zu öffnen, verwenden Sie die LoadOptions- und LoadFilter-Klassen, um die relevanten Attribute und Optionen der Klassen für die zu ladende Vorlagendatei festzulegen.

from aspose.cells import LoadDataFilterOptions, LoadFilter, LoadFormat, LoadOptions, 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(".")
# Load only specific sheets with data and formulas
# Other objects, items etc. would be discarded
# Instantiate LoadOptions specified by the LoadFormat
loadOptions = LoadOptions(LoadFormat.XLSX)
# Set LoadFilter property to load only data & cell formatting
loadOptions.load_filter = LoadFilter(LoadDataFilterOptions.CELL_DATA)
# Create a Workbook object and opening the file from its path
book = Workbook(dataDir + "Book1.xlsx", loadOptions)
print("File data imported successfully!")