Ouvrir des fichiers de différentes versions de Microsoft Excel

Ouverture de fichiers de différentes versions de Microsoft Excel

Une application doit souvent être capable d’ouvrir des fichiers Microsoft Excel créés dans différentes versions, comme Microsoft Excel 95, 97, ou Microsoft Excel 2007/2010/2013/2016/2019 et Office 365. Vous pourriez avoir besoin de charger un fichier dans l’un des plusieurs formats, y compris XLS, XLSX, XLSM, XLSB, SpreadsheetML, TabDelimited ou TSV, CSV, ODS, etc. Utilisez le constructeur, ou spécifiez l’attribut de type de format de fichier de la classe Workbook en utilisant l’énumération FileFormatType.

L’énumération FileFormatType contient de nombreux formats de fichier prédéfinis, dont certains sont donnés ci-dessous.

Types de formats de fichier Description
CSV Représente un fichier CSV
EXCEL_97_TO_2003 Représente un fichier Excel 97 - 2003
XLSX Représente un fichier XLSX Excel 2007/2010/2013/2016/2019 et Office 365
XLSM Représente un fichier XLSM Excel 2007/2010/2013/2016/2019 et Office 365
Xltx Représente un modèle de fichier XLTX Excel 2007/2010/2013/2016/2019 et Office 365
XLTX Représente un fichier XLTX activé par macro Excel 2007/2010/2013/2016/2019 et Office 365
XLSB Représente un fichier XLSB binaire Excel 2007/2010/2013/2016/2019 et Office 365
SPREADSHEET_ML Représente un fichier SpreadsheetML
TSV Représente un fichier de valeurs séparées par des tabulations
TAB_DELIMITED Représente un fichier texte tabulé
ODS Représente un fichier ODS
HTML Représente un fichier HTML
M_HTML Représente un fichier MHTML

Ouverture de fichiers Microsoft Excel 95/5.0

Pour ouvrir un fichier Microsoft Excel 95/5.0, utilisez LoadOptions et configurez l’attribut lié à la classe LoadOptions pour le fichier de modèle à charger. Un fichier d’exemple pour tester cette fonctionnalité peut être téléchargé en suivant le lien suivant :

Fichier Excel95

import aspose.cells
import aspose.pyio
from aspose.cells import Workbook, LoadOptions, LoadFormat
from aspose.pyio import BufferStream
# Get the Excel file into stream
with open('Excel95.xls', 'rb') as f:
data = f.read()
databuff = BufferStream(data)
# Instantiate LoadOptions specified by the LoadFormat.
loadOptions = LoadOptions(LoadFormat.EXCEL_97_TO_2003)
# Create a Workbook object and opening the file from the stream
wbExcel95 = Workbook(databuff, loadOptions);
print("Microsoft Excel 95/5.0 workbook opened successfully!")

Ouverture de fichiers Microsoft Excel 97 - 2003

Pour ouvrir un fichier Microsoft Excel 97 - 2003, utilisez LoadOptions et définissez l’attribut correspondant pour la classe LoadOptions pour le modèle de fichier à charger.

import aspose.cells
import aspose.pyio
from aspose.cells import Workbook, LoadOptions, LoadFormat
from aspose.pyio import BufferStream
# Get the Excel file into stream
with open('Excel03.xls', 'rb') as f:
data = f.read()
databuff = BufferStream(data)
# Instantiate LoadOptions specified by the LoadFormat.
loadOptions = LoadOptions(LoadFormat.EXCEL_97_TO_2003)
# Create a Workbook object and opening the file from the stream
wbExcel03 = Workbook(databuff, loadOptions);
print("Microsoft Excel 97 - 2003 workbook opened successfully!")

Ouverture de fichiers Microsoft Excel 2007/2010/2013/2016/2019 et Office 365 XLSX

Pour ouvrir un format Microsoft Excel 2007/2010/2013/2016/2019 et Office 365, c’est-à-dire XLSX ou XLSB, spécifiez le chemin du fichier. Vous pouvez également utiliser LoadOptions et définir l’attribut/les options correspondants de la classe LoadOptions pour le modèle de fichier à charger.

import aspose.cells
from aspose.cells import Workbook, LoadOptions, LoadFormat
# The path to the documents directory.
dataDir = ""
# Opening Microsoft Excel 2007 Xlsx Files
loadOptions2 = LoadOptions(LoadFormat.XLSX)
# Create a Workbook object and opening the file from its path
wbExcel07 = Workbook(dataDir + "Input.xlsx", loadOptions2)
print("Microsoft Excel 2007 - Office365 workbook opened successfully!")

Ouverture de fichiers Excel chiffrés

Il est possible de créer des fichiers Excel chiffrés à l’aide de Microsoft Excel. Pour ouvrir un fichier chiffré, utilisez LoadOptions et définissez ses attributs et options (par exemple, donner un mot de passe) pour le modèle de fichier à charger. Un fichier d’exemple pour tester cette fonctionnalité peut être téléchargé à partir du lien suivant:

Encrypted Excel

import aspose.cells
from aspose.cells import Workbook, LoadOptions, LoadFormat
# The path to the documents directory.
dataDir = ""
# Opening Microsoft Excel 2007 Xlsx Files
loadOptions = LoadOptions(LoadFormat.XLSX)
# Specify the password
loadOptions.password = "1234"
# Create a Workbook object and opening the file from its path
wbEncrypted = Workbook(dataDir + "EncryptedExcel.xlsx", loadOptions)
print("Encrypted excel file opened successfully!")

Aspose.Cells prend également en charge l’ouverture de fichiers Microsoft Excel 2007, 2010, 2013, 2016, 2019, Office 365 protégés par mot de passe.