向已签名的Excel文件添加数字签名

可能的使用场景

Aspose.Cells 提供了一个方法,您可以使用它向已经签名的Excel文件添加数字签名。

如何向已经签名的Excel文件添加数字签名

以下示例代码演示了如何使用 Workbook.AddDigitalSignature(DigitalSignatureCollection digitalSignatureCollection) 方法向已签名的Excel文件添加数字签名。请查看此代码中使用的 示例Excel文件。该文件已经数字签名。请查看代码生成的 输出Excel文件。这里我们使用了一个名为 AsposeDemo.pfx 的演示证书,密码为 aspose。屏幕截图展示了示例代码对示例Excel文件执行后的效果。

todo:image_alt_text

示例代码

// 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();