Verschiedene Möglichkeiten, Dateien zu öffnen
Öffnen einer Datei über einen Pfad
Entwickler können eine Microsoft Excel-Datei auf dem lokalen Computer öffnen, indem sie sie im Workbook-Klassenkonstruktor angeben. Geben Sie einfach den Pfad im Konstruktor als String an. Aspose.Cells erkennt automatisch den Dateiformattyp.
import aspose.cells | |
from aspose.cells import Workbook | |
# Opening a File via a Path | |
# The path to the documents directory. | |
dataDir = "" | |
# Opening through Path | |
# Creating a Workbook object and opening an Excel file using its file path | |
workbook = Workbook(dataDir + "Input.xlsx") | |
print("Workbook opened using path successfully!") |
Öffnen einer Datei über einen Stream
Es ist auch einfach, eine Excel-Datei als Stream zu öffnen. Verwenden Sie dazu eine überladene Version des Konstruktors, die das BufferStream-Objekt enthält, das die Datei enthält.
import io | |
import aspose.cells | |
from aspose.cells import Workbook, CellsHelper, License, SaveFormat | |
with open('Input.xlsx', 'rb') as file: | |
input_stream = io.BytesIO(file.read()) | |
workbook = Workbook(input_stream) | |
out_stream = io.BytesIO() | |
workbook.save(out_stream, SaveFormat.XLSX) | |
out_bytes = out_stream.getvalue() | |
print(out_bytes) | |
out_stream.close() | |
input_stream.close() | |
print("Workbook opened using stream successfully!") |
Öffnen einer Datei nur mit Daten
Um eine Datei nur mit Daten zu öffnen, verwenden Sie die Klassen LoadOptions und LoadFilter, um das entsprechende Attribut und die Optionen der Klassen für die zu ladende Vorlagendatei festzulegen.
import aspose.cells | |
from aspose.cells import Workbook, LoadOptions, LoadFormat, LoadFilter, LoadDataFilterOptions | |
# Opening a File with Data only | |
# The path to the documents directory. | |
dataDir = "" | |
# 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 | |
workbook = Workbook(dataDir + "Input.xlsx", loadOptions) | |
print("File data imported successfully!") |