加密和解密Excel文件
Contents
[
Hide
]
Microsoft Excel (97 - 365 ) 可以对电子表格进行加密 / 密码保护。它利用由加密服务提供商提供的算法。加密服务提供商或CSP是具有不同属性的一组密码算法。默认的CSP是"Office 97/2000 Compatible"或"Week Encryption (XOR)"。 选择适当的加密密钥长度也很重要。一些加密服务提供商不支持超过40或56位。那被认为是一种弱加密类型。但是,对于强加密类型,需要最少128位密钥长度。Microsoft Windows包含提供强加密类型的加密服务提供商,例如’Microsoft Strong Cryptographic Provider'。简单来说,银行用128位加密来加密与其Internet Banking Systems的连接。 Aspose.Cells允许您使用所需的加密类型对Excel文件进行加密 / 密码保护。
使用MS 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文件进行解密
使用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
//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"); |