Crittografare file Excel

Utilizzando Microsoft Excel

Per impostare le impostazioni di crittografia del file in Microsoft Excel (qui Microsoft Excel 2003):

  1. Dal menu Strumenti, seleziona Opzioni. Verrà visualizzata una finestra di dialogo.
  2. Selezionare la scheda Sicurezza.
  3. Immetti una password e clicca su Avanzate
  4. Scegliere il tipo di crittografia e confermare la password.

Crittografia con Aspose.Cells

Il seguente esempio mostra come criptografare e proteggere con password un file excel utilizzando l’API Aspose.Cells per Python via .NET.

from aspose.cells import EncryptionType, 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(".")
# Instantiate a Workbook object.
# Open an excel file.
workbook = Workbook(dataDir + "Book1.xls")
# Specify XOR encryption type.
workbook.set_encryption_options(EncryptionType.XOR, 40)
# Specify Strong Encryption type (RC4,Microsoft Strong Cryptographic Provider).
workbook.set_encryption_options(EncryptionType.STRONG_CRYPTOGRAPHIC_PROVIDER, 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

Il seguente esempio mostra come impostare l’opzione Password per modificare di Microsoft Excel per un file esistente usando l’API Aspose.Cells per Python via .NET.

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(".")
# Instantiate a Workbook object.
# Open an excel file.
workbook = Workbook(dataDir + "Book1.xls")
# Set the password for modification.
workbook.settings.write_protection.password = "1234"
# Save the excel file.
workbook.save(dataDir + "SpecifyPasswordToModifyOption.out.xls")

Verifica la password del file crittografato

Per verificare la password del file criptato, Aspose.Cells per Python via .NET offre il metodo verify_password. Questi metodi accettano due parametri, il flusso del file e la password da verificare. Il seguente frammento di codice dimostra l’uso del metodo verify_password per verificare se la password fornita è valida o meno.

from aspose.cells import FileFormatUtil
# 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(".")
# Create a Stream object
fstream = open(dataDir + "EncryptedBook1.xlsx", "rb")
isPasswordValid = FileFormatUtil.verify_password(fstream, "1234")
print("Password is Valid: " + str(isPasswordValid))

Criptazione/decriptazione di file ODS

Aspose.Cells per Python via .NET consente di criptare e decriptare file ODS. Il file ODS decriptato può essere aperto sia in Excel che in OpenOffice, tuttavia il file ODS criptato può essere aperto solo in OpenOffice dopo aver fornito la password. Excel non può aprire il file ODS criptato e potrebbe mostrare un messaggio di avviso. Le opzioni di crittografia non sono applicabili ai files ODS, a differenza di altri tipi di file. Per criptare un file ODS, carica il file e imposta il valore WorkbookSettings.password sulla password effettiva prima di salvarlo. Il file ODS criptato risultante può essere aperto solo in OpenOffice.

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.
sourceDir = RunExamples.Get_SourceDirectory()
# Output directory
outputDir = RunExamples.Get_OutputDirectory()
# Open an ODS file
workbook = Workbook(sourceDir + "sampleODSFile.ods")
# Password protect the file
workbook.settings.password = "1234"
# Save the ODS file
workbook.save(outputDir + "outputEncryptedODSFile.ods")

Per decifrare un file ODS, caricare il file fornendo una password in LoadOptions.password. Una volta caricato il file, impostare la stringa WorkbookSettings.password su null.

from aspose.cells import 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.
sourceDir = RunExamples.Get_SourceDirectory()
# Output directory
outputDir = RunExamples.Get_OutputDirectory()
# Open an encrypted ODS file
loadOptions = LoadOptions(LoadFormat.ODS)
# Set original password
loadOptions.password = "1234"
# Load the encrypted ODS file with the appropriate load options
workbook = Workbook(sourceDir + "sampleEncryptedODSFile.ods", loadOptions)
# Set the password to null
workbook.settings.password = None
# Save the decrypted ODS file
workbook.save(outputDir + "outputDecryptedODSFile.ods")