加密 Excel 文件

使用 Microsoft Excel

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

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

使用 Aspose.Cells 加密

以下示例显示如何使用 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 for .NET提供验证密码方法。这些方法接受两个参数,文件流和需要验证的密码。 下面的代码片段演示了使用验证密码验证提供的密码是否有效的方法。

// 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);
// Create a Stream object
FileStream fstream = new FileStream(dataDir + "EncryptedBook1.xlsx", FileMode.Open);
bool isPasswordValid = FileFormatUtil.VerifyPassword(fstream, "1234");
Console.WriteLine("Password is Valid: " + isPasswordValid);

用 Aspose.Cells 加密/解密 ODS 文件

Aspose.Cells 允许加密和解密 ODS 文件。解密后的ODS文件在Excel和OpenOffice中都可以打开,而加密后的ODS文件只有在提供密码后才能用OpenOffice打开。 Excel 无法打开加密的 ODS 文件,可能会引发警告消息。与其他文件类型不同,加密选项不适用于 ODS 文件。要加密 ODS 文件,加载文件并设置工作簿设置.密码在保存之前将值设置为实际密码。输出的加密文件ODS只能在OpenOffice中打开。

// 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 sourceDir = RunExamples.Get_SourceDirectory();
//Output directory
string outputDir = RunExamples.Get_OutputDirectory();
// Open an ODS file
Workbook workbook = new Workbook(sourceDir + "sampleODSFile.ods");
// Password protect the file
workbook.Settings.Password = "1234";
// Save the ODS file
workbook.Save(outputDir + "outputEncryptedODSFile.ods");

要解密 ODS 文件,请通过在加载选项.密码.加载文件后,设置工作簿设置.密码字符串为空。

// 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 sourceDir = RunExamples.Get_SourceDirectory();
//Output directory
string outputDir = RunExamples.Get_OutputDirectory();
// Open an encrypted ODS file
Aspose.Cells.LoadOptions loadOptions = new Aspose.Cells.LoadOptions(Aspose.Cells.LoadFormat.Ods);
// Set original password
loadOptions.Password = "1234";
// Load the encrypted ODS file with the appropriate load options
Workbook workbook = new Workbook(sourceDir + "sampleEncryptedODSFile.ods", loadOptions);
// Set the password to null
workbook.Settings.Password = null;
// Save the decrypted ODS file
workbook.Save(outputDir + "outputDecryptedODSFile.ods");