加密和解密Excel文件

使用Microsoft Excel

在Microsoft Excel(例如Microsoft Excel 2003)中设置文件加密设置:

  1. 工具菜单中选择选项。会出现一个对话框。
  2. 选择安全选项卡。
  3. 输入密码并点击高级
  4. 选择加密类型并确认密码。

使用Aspose.Cells对Excel文件进行加密

以下示例演示如何使用 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 API 设置现有文件的修改密码Microsoft Excel选项。

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对Excel文件进行解密

使用 Aspose.Cells for Python via .NET API 非常容易打开密码保护的Excel文件并进行解密,示例如下:

from aspose.cells import LoadOptions, Workbook
# Open encrypted file with password.
loadOptions = LoadOptions()
loadOptions.password = "password"
workbook = Workbook("Book1.xlsx", loadOptions)
# Remove password.
workbook.settings.password = None
# Save the file.
workbook.save("Book1.xlsx")

高级主题