将数字签名添加到已签名的 Excel 文件
Contents
[
Hide
]
可能的使用场景
Aspose.Cells 提供了Workbook.addDigitalSignature(DigitalSignatureCollection digitalSignatureCollection) 方法,可用于将数字签名添加到已签名的 Excel 文件。
请注意,在为已签名的 Excel 文档添加数字签名时,如果原始文档是 Aspose.Cells 生成的文档,则效果很好。但如果原文档是其他引擎生成的(如Microsoft Excel等),Aspose.Cells加载后重新保存后无法保持原来的文件原样,这样会使原签名失效。
将数字签名添加到已签名的 Excel 文件
下面的示例代码解释了如何使用[Workbook.addDigitalSignature(DigitalSignatureCollection digitalSignatureCollection)](https://reference.aspose.com/cells/java/com.aspose.cells/workbook#addDigitalSignature(com.aspose.cells.DigitalSignatureCollection)方法向已签名的 Excel 文件添加数字签名。请检查示例 Excel 文件在这段代码中使用。此文件已经过数字签名。请检查输出Excel文件由代码生成。我们使用了名为Aspose测试.pfx在这个有密码的代码中提出.截图显示了示例代码在示例Excel文件上执行后的效果。
示例代码
This file contains hidden or 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-Java | |
// Certificate file and its password | |
String certFileName = "AsposeTest.pfx"; | |
String password = "aspose"; | |
// Load the workbook which is already digitally signed to add new digital signature | |
Workbook workbook = new Workbook(srcDir + "sampleDigitallySignedByCells.xlsx"); | |
// Create the digital signature collection | |
DigitalSignatureCollection dsCollection = new DigitalSignatureCollection(); | |
// Create new digital signature and add it in digital signature collection | |
// ------------------------------------------------------------ | |
// --------------Begin::creating signature--------------------- | |
// Load the certificate into an instance of InputStream | |
InputStream inStream = new FileInputStream(srcDir + certFileName); | |
// Create an instance of KeyStore with PKCS12 cryptography | |
java.security.KeyStore inputKeyStore = java.security.KeyStore.getInstance("PKCS12"); | |
// Use the KeyStore.load method to load the certificate stream and its password | |
inputKeyStore.load(inStream, password.toCharArray()); | |
// Create an instance of DigitalSignature and pass the instance of KeyStore, password, comments and time | |
DigitalSignature signature = new DigitalSignature(inputKeyStore, password, | |
"Aspose.Cells added new digital signature in existing digitally signed workbook.", | |
com.aspose.cells.DateTime.getNow()); | |
dsCollection.add(signature); | |
// ------------------------------------------------------------ | |
// --------------End::creating signature----------------------- | |
// Add digital signature collection inside the workbook | |
workbook.addDigitalSignature(dsCollection); | |
// Save the workbook and dispose it. | |
workbook.save(outDir + "outputDigitallySignedByCells.xlsx"); | |
workbook.dispose(); |