Chiffrer et déchiffrer les fichiers Excel

Utilisation de Microsoft Excel

Pour définir les paramètres de chiffrement de fichier dans Microsoft Excel (ici Microsoft Excel 2003) :

  1. Dans le menu Outils, sélectionnez Options. Une boîte de dialogue apparaîtra.
  2. Sélectionnez l’onglet Sécurité.
  3. Saisissez un mot de passe et cliquez sur Avancé
  4. Choisissez le type de chiffrement et confirmez le mot de passe.

Chiffrer un fichier Excel avec Aspose.Cells

L’exemple suivant montre comment chiffrer et protéger par mot de passe un fichier Excel à l’aide de l’API Aspose.Cells.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiate a Workbook object.
// Open an excel file.
Workbook workbook = new Workbook(dataDir + "Book1.xls");
// Specify XOR encryption type.
workbook.SetEncryptionOptions(EncryptionType.XOR, 40);
// Specify Strong Encryption type (RC4,Microsoft Strong Cryptographic Provider).
workbook.SetEncryptionOptions(EncryptionType.StrongCryptographicProvider, 128);
// Password protect the file.
workbook.Settings.Password = "1234";
// Save the excel file.
workbook.Save(dataDir + "encryptedBook1.out.xls");

Option de spécification du mot de passe pour modifier

L’exemple suivant montre comment définir l’option Mot de passe pour modifier de Microsoft Excel pour un fichier existant à l’aide de l’API Aspose.Cells.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiate a Workbook object.
// Open an excel file.
Workbook workbook = new Workbook(dataDir + "Book1.xls");
// Set the password for modification.
workbook.Settings.WriteProtection.Password = "1234";
// Save the excel file.
workbook.Save(dataDir + "SpecifyPasswordToModifyOption.out.xls");

Déchiffrer le fichier Excel avec Aspose.Cells

Il est très facile d’ouvrir un fichier Excel protégé par mot de passe et de le déchiffrer en utilisant l’API Aspose.Cells avec les codes suivants :

//Open encrypted file with password.
LoadOptions loadOptions = new LoadOptions();
loadOptions.Password = "password";
Workbook workbook = new Workbook("Book1.xlsx", loadOptions);
//Remove password.
workbook.Settings.Password = null;
//Save the file.
workbook.Save("Book1.xlsx");

Sujets avancés