Crittografia di file Excel

Utilizzando Microsoft Excel

Per configurare le impostazioni di crittografia dei file in Microsoft Excel (qui Microsoft Excel 2003):

  1. DalUtensili menù, selezionareOpzioniApparirà una finestra di dialogo.
  2. Seleziona ilSicurezza scheda.
  3. Immettere una password e fare clicAvanzate
  4. Scegli il tipo di crittografia e conferma la password.

Crittografia con Aspose.Cells

L’esempio seguente mostra come crittografare e proteggere con password un file excel utilizzando Aspose.Cells API.

// 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");

Specificare la password per modificare l’opzione

L’esempio seguente mostra come impostare ilPassword da modificare Microsoft Opzione di Excel per un file esistente utilizzando Aspose.Cells API.

// 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");

Verificare la password del file crittografato

Per verificare la password del file cifrato, Aspose.Cells for .NET fornisce ilVerifica la password metodo. Questi metodi accettano due parametri, il flusso di file e la password che deve essere verificata. Il seguente frammento di codice illustra l’uso diVerifica la password metodo per verificare se la password fornita è valida o meno.

// 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);
// Create a Stream object
FileStream fstream = new FileStream(dataDir + "EncryptedBook1.xlsx", FileMode.Open);
bool isPasswordValid = FileFormatUtil.VerifyPassword(fstream, "1234");
Console.WriteLine("Password is Valid: " + isPasswordValid);

Crittografia/decrittografia del file ODS con Aspose.Cells

Aspose.Cells consente di crittografare e decrittografare il file ODS. Il file ODS decrittografato può essere aperto sia in Excel che in OpenOffice, tuttavia il file ODS crittografato può essere aperto solo da OpenOffice dopo aver fornito la password. Excel non è in grado di aprire il file crittografato ODS e potrebbe generare un messaggio di avviso. Le opzioni di crittografia non sono applicabili per il file ODS a differenza di altri tipi di file. Per crittografare un file ODS, caricare il file e impostare l’estensioneImpostazioni cartella di lavoro.Password valore alla password effettiva prima di salvarla. Il file crittografato di output ODS può essere aperto solo in OpenOffice.

// 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 sourceDir = RunExamples.Get_SourceDirectory();
//Output directory
string outputDir = RunExamples.Get_OutputDirectory();
// Open an ODS file
Workbook workbook = new Workbook(sourceDir + "sampleODSFile.ods");
// Password protect the file
workbook.Settings.Password = "1234";
// Save the ODS file
workbook.Save(outputDir + "outputEncryptedODSFile.ods");

Per decrittografare un file ODS, caricare il file fornendo una password nel fileLoadOptions.Password . Una volta caricato il file, imposta il fileImpostazioni cartella di lavoro.Password stringa a null.

// 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 sourceDir = RunExamples.Get_SourceDirectory();
//Output directory
string outputDir = RunExamples.Get_OutputDirectory();
// Open an encrypted ODS file
Aspose.Cells.LoadOptions loadOptions = new Aspose.Cells.LoadOptions(Aspose.Cells.LoadFormat.Ods);
// Set original password
loadOptions.Password = "1234";
// Load the encrypted ODS file with the appropriate load options
Workbook workbook = new Workbook(sourceDir + "sampleEncryptedODSFile.ods", loadOptions);
// Set the password to null
workbook.Settings.Password = null;
// Save the decrypted ODS file
workbook.Save(outputDir + "outputDecryptedODSFile.ods");