加密和解密 Excel 文件

使用微软 Excel

在MS Excel(如MS Excel 2003)中,要实现文件加密设置,您可以尝试:

  • 来自工具菜单,选择选项 , 然后选择安全标签。
  • 输入打开密码然后点击先进的按钮。
  • 选择加密类型并确认密码。

待办事项:图片_替代_文本

图:选项对话框

待办事项:图片_替代_文本

图:加密类型对话框

加密 Excel 文件

以下示例显示如何使用 Aspose.Cells API 加密/密码保护 excel 文件。

示例代码:

// 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解密是非常有用的,如下代码:

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