Cifrar y Descifrar archivos ODS
Cifrar con OpenOffice Calc
- Selecciona Guardar como y Haz clic en la casilla Guardar con Contraseña.
- Haz clic en el botón Guardar.
- Escribe tu contraseña deseada en los campos Introducir Contraseña para Abrir y Confirmar Contraseña en la ventana Establecer Contraseña que se abre.
- Haz clic en el botón Aceptar para guardar el archivo.
Cifrado/Descifrado de archivo ODS:
Para cifrar un archivo ODS, carga el archivo y pasa la contraseña real a WorkbookSettings.setPassword() antes de guardarlo. El archivo ODS cifrado de salida solo se puede abrir en OpenOffice. Para descifrar un archivo ODS, carga el archivo proporcionando la contraseña en la LoadOptions.setPassword(). Una vez que el archivo se carga, llama a la función Workbook.unprotect() con la contraseña real como argumento y finalmente pasa null a Workbook.getWorkbookSettings().setPassword().
Código de Ejemplo:
// 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."); | |
} |