向已签名的Excel文件添加数字签名
Contents
[
Hide
]
可能的使用场景
Aspose.Cells 提供了一个方法,您可以使用它向已经签名的Excel文件添加数字签名。
请注意,向已签名的Excel文档添加数字签名时,如果原始文档是由Aspose.Cells生成的,它会正常工作。但如果原始文档是由其他引擎(如Microsoft Excel等)生成的,Aspose.Cells在加载和重新保存后就无法保持文件不变,这将使原始签名无效。
如何向已经签名的Excel文件添加数字签名
以下示例代码演示了如何使用 Workbook.AddDigitalSignature(DigitalSignatureCollection digitalSignatureCollection) 方法向已签名的Excel文件添加数字签名。请查看此代码中使用的 示例Excel文件。该文件已经数字签名。请查看代码生成的 输出Excel文件。这里我们使用了一个名为 AsposeDemo.pfx 的演示证书,密码为 aspose。屏幕截图展示了示例代码对示例Excel文件执行后的效果。
示例代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Certificate file and its password | |
string certFileName = sourceDir + "AsposeDemo.pfx"; | |
string password = "aspose"; | |
//Load the workbook which is already digitally signed to add new digital signature | |
Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(sourceDir + "sampleDigitallySignedByCells.xlsx"); | |
//Create the digital signature collection | |
Aspose.Cells.DigitalSignatures.DigitalSignatureCollection dsCollection = new Aspose.Cells.DigitalSignatures.DigitalSignatureCollection(); | |
//Create new certificate | |
System.Security.Cryptography.X509Certificates.X509Certificate2 certificate = new System.Security.Cryptography.X509Certificates.X509Certificate2(certFileName, password); | |
//Create new digital signature and add it in digital signature collection | |
Aspose.Cells.DigitalSignatures.DigitalSignature signature = new Aspose.Cells.DigitalSignatures.DigitalSignature(certificate, "Aspose.Cells added new digital signature in existing digitally signed workbook.", DateTime.Now); | |
dsCollection.Add(signature); | |
//Add digital signature collection inside the workbook | |
workbook.AddDigitalSignature(dsCollection); | |
//Save the workbook and dispose it. | |
workbook.Save(outputDir + "outputDigitallySignedByCells.xlsx"); | |
workbook.Dispose(); | |