加密和解密 Excel 文件
Contents
[
Hide
]
Microsoft Excel (97 - 365) 使您能够加密/密码保护您的电子表格。它利用加密服务提供商提供的算法。加密服务提供商或 CSP 是一组具有不同属性的加密算法。默认的 CSP 是“Office 97/2000 Compatible”或“Week Encryption (XOR)”。选择合适的加密密钥长度也很重要。一些加密服务提供商不支持超过 40 或 56 位。这被认为是一种弱加密类型。但是,对于强加密类型,需要至少 128 位的密钥长度。 Microsoft Windows 包含也提供强加密类型的加密服务提供程序,例如“Microsoft 强加密提供程序”。举个例子,银行使用 128 位加密来加密与其网上银行系统的连接。 Aspose.Cells 允许您使用所需的加密类型加密/密码保护您的 excel 文件。
使用微软 Excel
在MS Excel(如MS Excel 2003)中,要实现文件加密设置,您可以尝试:
- 来自工具菜单,选择选项 , 然后选择安全标签。
- 输入打开密码然后点击先进的按钮。
- 选择加密类型并确认密码。
图:选项对话框
图:加密类型对话框
加密 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-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(EncryptingFiles.class) + "loading_saving/"; | |
// Instantiate a Workbook object by excel file path | |
Workbook workbook = new Workbook(dataDir + "Book1.xls"); | |
// Password protect the file. | |
workbook.getSettings().setPassword("1234"); | |
// Specify XOR encrption type. | |
workbook.setEncryptionOptions(EncryptionType.XOR, 40); | |
// Specify Strong Encryption type (RC4,Microsoft Strong Cryptographic | |
// Provider). | |
workbook.setEncryptionOptions(EncryptionType.STRONG_CRYPTOGRAPHIC_PROVIDER, 128); | |
// Save the excel file. | |
workbook.save(dataDir + "EncryptingFiles_out.xls"); | |
// Print message | |
System.out.println("Encryption applied successfully on output file."); |
用 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.setPassword("password"); | |
Workbook workbook = new Workbook("Book1.xlsx", loadOptions); | |
//Remove password. | |
workbook.getSettings().setPassword(null); | |
//Save the file. | |
workbook.save("Book1.xlsx"); |