使用 Aspose.Cells 加密 Excel 文件

使用 Microsoft Excel

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

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

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

  1. 选择加密类型并确认密码。

    加密类型对话框

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

使用 Aspose.Cells 加密

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

C#

 string FilePath = @"..\..\..\Sample Files\";

string srcFileName = FilePath + "Encrypting Excel Files.xlsx";

string destFileName = FilePath + "Result Encrypting Excel Files.xlsx";

//Open an excel file.

Workbook workbook = new Workbook(srcFileName);

//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(destFileName);

下载运行代码

下载示例代码