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 jpype
import asposecells
jpype.startJVM()
from asposecells.api 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!")
jpype.shutdownJVM()

Ö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 jpype
import asposecells
jpype.startJVM()
from asposecells.api import Workbook
from jpype import java
fis = java.io.FileInputStream("Input.xlsx")
workbook = Workbook(fis)
print("Workbook opened using stream successfully!!")
workbook.save("Output.pdf")
fis.close()
jpype.shutdownJVM()

Öffnen einer Datei nur mit Daten

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.

import jpype
import asposecells
jpype.startJVM()
from asposecells.api 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.setLoadFilter(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!")
jpype.shutdownJVM()