用JavaScript通过C++用证书数字签署VBA代码项目
Contents
[
Hide
]
您可以使用Aspose.Cells及其VbaProject.sign(DigitalSignature)方法对VBA代码项目进行数字签名。请按照以下步骤检查您的Excel文件是否已用证书进行数字签名。
- 从“开发人员”选项卡中单击“Visual Basic”以打开“Visual Basic for Applications IDE”
- 从“Visual Basic for Applications IDE”中单击“工具” > “数字签名…”
将显示“数字签名表单”,显示文档是否已使用证书进行数字签名。
用JavaScript用证书数字签署VBA代码项目
以下示例代码演示了如何使用VbaProject.sign(DigitalSignature)方法。这是示例代码的输入和输出文件。你可以使用任何Excel文件和任何证书来测试此代码。
- 用于示例代码的源Excel文件
- 示例pfx文件用于创建数字签名。请在计算机上安装它以运行此代码。其密码为1234。
- 示例代码生成的输出Excel文件
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Sign VBA Project with Digital Signature</h1>
<div>
<label for="fileInput">Select Excel Workbook (.xlsm): </label>
<input type="file" id="fileInput" accept=".xlsm,.xls,.xlsx" />
</div>
<div>
<label for="pfxInput">Select PFX Certificate (.pfx): </label>
<input type="file" id="pfxInput" accept=".pfx" />
</div>
<button id="runExample">Sign VBA Project</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, DigitalSignature } = 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');
const pfxInput = document.getElementById('pfxInput');
const resultDiv = document.getElementById('result');
if (!fileInput.files.length || !pfxInput.files.length) {
resultDiv.innerHTML = '<p style="color: red;">Please select both the Excel workbook and the PFX certificate files.</p>';
return;
}
const workbookFile = fileInput.files[0];
const pfxFile = pfxInput.files[0];
// Read files as ArrayBuffer
const [wbArrayBuffer, pfxArrayBuffer] = await Promise.all([
workbookFile.arrayBuffer(),
pfxFile.arrayBuffer()
]);
// Prepare bytes
const wbBytes = new Uint8Array(wbArrayBuffer);
const pfxBytes = new Uint8Array(pfxArrayBuffer);
// Set Digital Signature parameters
const password = "1234";
const comment = "Signing Digital Signature using Aspose.Cells";
const digitalSignature = new DigitalSignature(pfxBytes, password, comment, new Date());
// Create workbook object from excel file
const workbook = new Workbook(wbBytes);
// Sign VBA Code Project with Digital Signature
workbook.vbaProject.sign(digitalSignature);
// Save the workbook as XLSM
const outputData = workbook.save(SaveFormat.Xlsm);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'outputDigitallySignVbaProjectWithCertificate.xlsm';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Signed Workbook';
resultDiv.innerHTML = '<p style="color: green;">Workbook signed successfully! Click the download link to download the signed workbook.</p>';
});
</script>
</html>