Excelファイルの暗号化

Microsoft Excel の使用

Microsoft Excel(ここではMicrosoft Excel 2003)でファイルの暗号化設定を行うには:

  1. ツールメニューからオプションを選択します。ダイアログが表示されます。
  2. セキュリティタブを選択します。
  3. パスワードを入力し、詳細をクリックします。
  4. 暗号化方式を選択し、パスワードを確認します。

Aspose.Cells を使用した暗号化

この例は、Aspose.Cells for Python via .NET APIを使用してExcelファイルを暗号化し、パスワード保護する方法を示しています。

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

修正パスワードを指定するオプション

この例は、既存のファイルに対してAspose.Cells for 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")

暗号化されたファイルのパスワードを確認します

暗号化されたファイルのパスワードを確認するには、Aspose.Cells for Python via .NETがverify_passwordメソッドを提供します。これらのメソッドは、ファイルストリームと検証したいパスワードの2つのパラメータを受け取ります。 以下のコードスニペットは、提供されたパスワードが有効かどうかを確認するverify_passwordメソッドの使用を示しています。

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

ODSファイルの暗号化/復号化

Aspose.Cells for Python via .NETは、ODSファイルの暗号化と復号化をサポートします。復号化されたODSファイルはExcelとOpenOfficeの両方で開けますが、暗号化されたODSファイルはパスワード入力後にOpenOfficeでのみ開くことが可能です。Excelでは暗号化されたODSファイルを開けず、警告が出ることがあります。ODSファイルの暗号化には、ファイルをロードして実際のパスワードを設定し(WorkbookSettings.passwordに設定)、保存します。出力された暗号化ODSファイルは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")

ODSファイルを復号化するには、LoadOptions.passwordによるパスワードの提供でファイルを読み込みます。ファイルが読み込まれたら、WorkbookSettings.password文字列を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")