加密和解密ODS文件
Contents
[
Hide
]
OpenOffice.org是一个功能齐全的办公套件,支持对文件进行密码保护和加密。然而,加密的ODS文件只能在提供密码后才能在OpenOffice中打开。Excel无法打开加密的ODS文件,可能会弹出警告消息。与其他文件类型不同,加密选项不适用于ODS文件。
Aspose.Cells允许对ODS文件进行加密和解密。解密的ODS文件可以同时在Excel和OpenOffice中打开。
在OpenOffice Calc中加密
- 选择另存为并点击加上密码保存框。
- 点击保存按钮。
- 在打开密码窗口中的输入打开文件的密码和确认密码字段中键入所需的密码。
- 点击确定按钮以保存文件。
加密/解密ODS文件
要加密ODS文件,请加载文件并在保存之前传递实际密码到 WorkbookSettings.setPassword()。输出的加密ODS文件只能在OpenOffice中打开。要解密ODS文件,请在 LoadOptions.setPassword() 中提供密码加载文件。一旦文件加载完成,使用实际密码调用函数 Workbook.unprotect(),最后将null传递给 Workbook.getWorkbookSettings().setPassword()。
示例代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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."); | |
} |