加密和解密 Excel 文件

使用 Microsoft Excel

要在 Microsoft Excel 中设置文件加密设置(此处为 Microsoft Excel 2003):

  1. 来自工具菜单,选择选项.将出现一个对话框。
  2. 选择安全标签。
  3. 输入密码并点击先进的
  4. 选择加密类型并确认密码。

用 Aspose.Cells 加密 Excel 文件

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

// 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。

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

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

推进主题