用数字证书为VBA代码项目数字签名(C++)
Contents
[
Hide
]
您可以使用Aspose.Cells及其Workbook.VbaProject.Sign()方法对VBA代码项目进行数字签名。请按照以下步骤检查您的Excel文件是否已用证书进行数字签名。
- 点击开发工具标签中的Visual Basic以打开Visual Basic for Applications IDE。
- 在Visual Basic for Applications IDE中点击工具 > 数字签名…。
它将显示数字签名表单,显示文档是否已使用证书进行数字签名。
用C++为VBA代码项目进行数字签名并使用证书
以下示例代码演示如何使用Workbook.VbaProject.Sign()方法。此示例代码的输入和输出文件都已列出。您可以使用任何Excel文件和任何证书测试此代码。
- 用于示例代码的源Excel文件
- 示例pfx文件用于创建数字签名。请在计算机上安装它以运行此代码。其密码为1234。
- 示例代码生成的输出Excel文件
#include <iostream>
#include <ctime>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
using namespace Aspose::Cells::DigitalSignatures;
int main()
{
Aspose::Cells::Startup();
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
U16String password(u"1234");
U16String pfxPath = srcDir + u"sampleDigitallySignVbaProjectWithCertificate.pfx";
U16String comment(u"Signing Digital Signature using Aspose.Cells");
Vector<uint8_t> certData;
std::time_t now = std::time(nullptr);
std::tm* now_tm = std::localtime(&now);
int year = now_tm->tm_year + 1900;
int month = now_tm->tm_mon + 1;
int day = now_tm->tm_mday;
int hour = now_tm->tm_hour;
int minute = now_tm->tm_min;
int second = now_tm->tm_sec;
DigitalSignature digitalSignature(certData, password, comment, Date{year, month, day, hour, minute, second, 0});
U16String inputFilePath = srcDir + u"sampleDigitallySignVbaProjectWithCertificate.xlsm";
Workbook workbook(inputFilePath);
workbook.GetVbaProject().Sign(digitalSignature);
U16String outputFilePath = outDir + u"outputDigitallySignVbaProjectWithCertificate.xlsm";
workbook.Save(outputFilePath);
std::cout << "VBA project digitally signed successfully!" << std::endl;
Aspose::Cells::Cleanup();
}