加密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");

指定修改密码选项

下面的示例显示了如何使用Aspose.Cells API为现有文件设置修改密码 Microsoft 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");
// Set the password for modification.
workbook.Settings.WriteProtection.Password = "1234";
// Save the excel file.
workbook.Save(dataDir + "SpecifyPasswordToModifyOption.out.xls");

验证加密文件的密码

要验证加密文件的密码,Aspose.Cells for .NET 提供了 VerifyPassword 方法。这些方法接受两个参数,文件流和需要验证的密码。 以下代码片段演示了使用VerifyPassword方法来验证提供的密码是否有效。

// 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文件进行加密,请加载文件并在保存之前将WorkbookSettings.Password值设置为实际密码。输出的加密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文件,通过在LoadOptions.Password中提供密码来加载文件。一旦文件加载完成,将WorkbookSettings.Password字符串设置为null。

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