How to sign Excel spreadsheets and their macros using C#
How to sign Excel spreadsheets and their macros using C#
Leave feedback
On this page
You can sign spreadsheets, as well as, Visual Basic for Applications (VBA) macro embedded into spreadsheets with digital certificates. Signing a workbook confirms the identity of the signer and the validity of the content. This enhances security and authentication. Modifying a signed spreadsheet invalidates the signature. When opening a signed workbook, other users could be sure that it came from a reliable source and no one has modified it since.
Obtaining a digital certificate
A digital certificate is a cryptographic key pair that consists of a public key and a private key, issued by a trusted third party known as a Certificate Authority (CA). There are many commercial third-party certificate authorities from which you can either purchase a digital certificate or obtain a free digital certificate. Many institutions, governments, and corporations can also issue their own certificates.
You can create your own digital certificate for personal use or testing purposes with the SelfCert.exe tool that is provided with Microsoft Office. However, this certificate is not authenticated by a Certificate Authority (CA).
In this article, we will use a self-created test certificate.
Keep in mind that for production environments, you should obtain a certificate from a trusted CA to ensure secure and trusted communication.
What Excel files could be signed?
You can sign the following files and projects:
workbooks (XLSX),
templates (XLTX),
macro-enabled workbooks (XLSM),
macro-enabled templates (XLTM),
VBA macro projects within workbooks or templates.
Signing spreadsheets with digital certificates in C#
To sign the content of a particular spreadsheet or template:
Instantiate the SignatureΒ class providing a path to the source document or document stream.
Create the DigitalSignOptions object instance providing a path to the certificate. Specify the certificate password using the DigitalSignOptions.Password property.
Invoke the Sign method to process the document, providing the output file path and sign options.
usingGroupDocs.Signature;usingGroupDocs.Signature.Options;stringfilePath=@"C:\sample.xlsx";// NOTE: Put here actual path for your document and certificatestringcertificatePath=@"C:\MrSmithSignature.pfx";stringpassword="1234567890";stringfileName=Path.GetFileName(filePath);stringoutputFilePath=Path.Combine(@"C:\output",fileName);// Sign a spreadsheetusing(Signaturesignature=newSignature(filePath)){// Setup digital signature optionsDigitalSignOptionssignOptions=newDigitalSignOptions(certificatePath);signOptions.Signature.Comments="Test Signature";signOptions.Password=password;signature.Sign(outputFilePath,signOptions);}
A spreadsheet signed with a digital certificate would look like below:
Signing macro projects within spreadsheets with digital certificates in C#
You can sign just the macros embedded into the spreadsheet, or sign both the spreadsheet’s content and macros.
To sign only the macros:
Instantiate the SignatureΒ class providing a path to the source document or document stream.
Add the DigitalVBA object instance as a sign options extension.
Invoke the Sign method to process the document, providing the output file path and sign options.
usingGroupDocs.Signature;usingGroupDocs.Signature.Options;usingGroupDocs.Signature.Domain.Extensions;stringfilePath=@"C:\sample.xlsm";// NOTE: Put here actual path for your document and certificatestringcertificatePath=@"C:\MrSmithSignature.pfx";stringpassword="1234567890";stringfileName=Path.GetFileName(filePath);stringoutputFilePath=Path.Combine(@"C:\output",fileName);// Sign macros within the spreadsheetusing(Signaturesignature=newSignature(filePath)){//Create digital signature options without digital certificateDigitalSignOptionssignOptions=newDigitalSignOptions();//Add extension for signing VBA project digitallyDigitalVBAdigitalVBA=newDigitalVBA(certificatePath,password);//Set to true only for signing VBA projectdigitalVBA.SignOnlyVBAProject=true;digitalVBA.Comments="Signed VBA macros";signOptions.Extensions.Add(digitalVBA);signature.Sign(outputFilePath,signOptions);}
To sign both the content and macros:
Instantiate the SignatureΒ class providing a path to the source document or document stream.
Create the DigitalSignOptions object instance providing a path to the certificate. Specify the certificate password using the DigitalSignOptions.Password property.
Create the DigitalVBA object instance providing certificate path and password as constructor parameters.
Add the DigitalVBA object instance as a sign options extension.
Invoke the Sign method to process the document, providing the output file path and sign options.
usingGroupDocs.Signature;usingGroupDocs.Signature.Options;usingGroupDocs.Signature.Domain.Extensions;stringfilePath=@"C:\sample.xlsm";// NOTE: Put here actual path for your document and certificatestringcertificatePath=@"C:\MrSmithSignature.pfx";stringpassword="1234567890";stringfileName=Path.GetFileName(filePath);stringoutputFilePath=Path.Combine(@"C:\output",fileName);// Sign macros within the spreadsheetusing(Signaturesignature=newSignature(filePath)){// Setup digital signature optionsDigitalSignOptionssignOptions=newDigitalSignOptions(certificatePath);signOptions.Signature.Comments="Test Signature";signOptions.Password=password;//Add extension for signing VBA project digitallyDigitalVBAdigitalVBA=newDigitalVBA(certificatePath,password);digitalVBA.Comments="Signed VBA macros";signOptions.Extensions.Add(digitalVBA);signature.Sign(outputFilePath,signOptions);}
Get a Free API License
In order to use the API without evaluation limitations, you can get a free temporary license.
Conclusion
In this article, we learned the reasons for signing Excel spreadsheets and macros embedded into them. Leveraging C# within .NET applications significantly enhances the efficiency of these tasks.
In addition, you can use the Digital Signature - XLSX online application to sign your Excel files with GroupDocs.Signature for free.
Read the documentation to learn how to use GroupDocs.Signature in your .NET applications. Also, you may discuss any questions or issues at the GroupDocs forum.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.