加密和解密 Excel 文件
Contents
[
Hide
]
Microsoft Excel (97 - 365) 使您能够加密和密码保护您的电子表格。它使用加密服务提供商或 CSP 提供的算法,这是一组具有不同属性的加密算法。默认 CSP 是“Office 97/2000 兼容”或“弱加密 (XOR)”。选择合适的加密密钥长度很重要。一些 CSP 不支持超过 40 或 56 位。这被认为是弱加密。对于强加密,需要至少 128 位的密钥长度。 Microsoft Windows 包含也提供强加密类型的 CSP,例如“Microsoft Strong Cryptographic Provider”。给您一个概念,银行使用 128 位加密来加密与其网上银行系统的连接。
Aspose.Cells 允许您使用所需的加密类型加密和密码保护 Microsoft Excel 文件。
使用 Microsoft Excel
要在 Microsoft Excel 中设置文件加密设置(此处为 Microsoft Excel 2003):
- 来自工具菜单,选择选项.将出现一个对话框。
- 选择安全标签。
- 输入密码并点击先进的
- 选择加密类型并确认密码。
用 Aspose.Cells 加密 Excel 文件
以下示例显示如何使用 Aspose.Cells API 加密和密码保护 excel 文件。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |
指定修改选项的密码
以下示例显示了如何设置修改密码Microsoft 现有文件的 Excel 选项使用 Aspose.Cells API。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |
用 Aspose.Cells 解密 Excel 文件
打开密码保护的excel文件并使用Aspose.Cells API解密是非常有用的,如下代码:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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"); |