用JavaScript通过C++对ODS文件进行加密与解密
Contents
[
Hide
]
OpenOffice.org 是一个功能齐全的办公套件,支持密码保护和文件加密。但加密的 ODS 文件只能在提供密码后由 OpenOffice 打开。Excel 不能打开加密的 ODS 文件,可能会出现警告。与其他文件类型不同,ODS 文件的加密选项不适用。
Aspose.Cells 允许你加密和解密 ODS 文件。解密的 ODS 文件可以在 Excel 和 OpenOffice 中打开。
在OpenOffice Calc中加密
- 选择另存为,并点击带密码保存框。
- 点击保存按钮。
- 在打开密码窗口中的输入打开文件的密码和确认密码字段中键入所需的密码。
- 点击确定按钮以保存文件。
使用C++的Aspose.Cells for JavaScript加密ODS文件
要加密 ODS 文件,加载文件并在保存前将 WorkbookSettings.password 设置为实际密码。输出的加密 ODS 文件只能在 OpenOffice 中打开。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Protect ODS File</title>
</head>
<body>
<h1>Protect ODS File Example</h1>
<input type="file" id="fileInput" accept=".ods" />
<button id="runExample">Protect and Download ODS</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an ODS file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiating a Workbook object by opening the uploaded ODS file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Password protect the file (converted from getSettings().setPassword -> settings.password)
workbook.settings.password = "1234";
// Saving the ODS file
const outputData = workbook.save(SaveFormat.Ods);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'outputEncryptedODSFile.ods';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Encrypted ODS File';
document.getElementById('result').innerHTML = '<p style="color: green;">File protected successfully! Click the download link to get the encrypted file.</p>';
});
</script>
</html>
使用C++的Aspose.Cells for JavaScript解密ODS文件
要解密ODS文件,请在LoadOptions.password中提供密码后加载文件。一旦加载完成,将WorkbookSettings.password字符串设置为空。
<!DOCTYPE html>
<html>
<head>
<title>Decrypt ODS Example</title>
</head>
<body>
<h1>Decrypt ODS Example</h1>
<input type="file" id="fileInput" accept=".ods" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, LoadOptions, LoadFormat, Utils } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an ODS file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Open an encrypted ODS file with load options
const loadOptions = new LoadOptions(LoadFormat.Ods);
// Set original password
loadOptions.password = "1234";
// Load the encrypted ODS file with the appropriate load options
const workbook = new Workbook(new Uint8Array(arrayBuffer), loadOptions);
// Set the password to null (remove password from settings)
workbook.settings.password = null;
// Save the decrypted ODS file and provide download link
const outputData = workbook.save(SaveFormat.Ods);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'outputDecryptedODSFile.ods';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Decrypted ODS File';
document.getElementById('result').innerHTML = '<p style="color: green;">Decryption completed successfully! Click the download link to get the decrypted file.</p>';
});
</script>
</html>