加密和解密 ODS 个文件

使用 OpenOffice Calc 加密

  1. 选择另存为并点击用密码保存盒子。
  2. 点击救球按钮。
  3. 在两者中输入您想要的密码输入密码打开确认密码打开的“设置密码”窗口中的字段。
  4. 点击好的按钮保存文件。

加密/解密 ODS 文件:

为了加密 ODS 文件,加载文件并将实际密码传递给工作簿设置.setPassword()在保存之前。输出的加密文件ODS只能在OpenOffice中打开。要解密 ODS 文件,请通过在LoadOptions.setPassword() 方法.加载文件后,调用函数工作簿.unprotect() 以实际密码作为参数,最后将 null 传递给Workbook.getWorkbookSettings().setPassword().

示例代码:

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
static String sourceDir = Utils.Get_SourceDirectory();
static String outputDir = Utils.Get_OutputDirectory();
public static void main(String[] args) throws Exception {
//Encrypt an ODS file
//Encrypted ODS file can only be opened in OpenOffice as Excel does not support encrypted ODS files
//Initialize loading options
LoadOptions loadOptions = new LoadOptions(LoadFormat.ODS);
// Instantiate a Workbook object.
// Open an ODS file.
Workbook workbook = new Workbook(sourceDir + "sampleODSFile.ods", loadOptions);
//Encryption options are not effective for ODS files
// Password protect the file.
workbook.getSettings().setPassword("1234");
// Save the excel file.
workbook.save(outputDir + "outputEncryptedODSFile.ods");
//Decrypt ODS file
//Decrypted ODS file can be opened both in Excel and OpenOffice
// Set original password
loadOptions.setPassword("1234");
// Load the encrypted ODS file with the appropriate load options
Workbook encrypted = new Workbook(sourceDir + "sampleEncryptedODSFile.ods", loadOptions);
// Unprotect the workbook
encrypted.unprotect("1234");
// Set the password to null
encrypted.getSettings().setPassword(null);
// Save the decrypted ODS file
encrypted.save(outputDir + "outputDecryptedODSFile.ods");
// Print message
System.out.println("Encryption and Decryption applied successfully on ODS file.");
}