Différentes façons d ouvrir des fichiers

Comment ouvrir un fichier Excel via un chemin

Les développeurs peuvent ouvrir un fichier Microsoft Excel en utilisant son chemin d’accès local en le spécifiant dans le constructeur de la classe Workbook. Il suffit de transmettre le chemin dans le constructeur sous forme de chaîne. Aspose.Cells pour Python via .NET détectera automatiquement le type de format de fichier.

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!")

Comment ouvrir un fichier Excel via un flux

Il est également simple d’ouvrir un fichier Excel en tant que flux. Pour ce faire, utilisez une version surchargée du constructeur qui prend l’objet Stream contenant le fichier.

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()

Comment ouvrir un fichier avec des données uniquement

Pour ouvrir un fichier avec uniquement des données, utilisez les classes LoadOptions et LoadFilter pour définir l’attribut et les options associés des classes pour charger le fichier modèle.

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!")