本文将介绍如何使用Spire.XLS for .NET在Excel中添加和删除数字签名。
添加数字签名
C#
using System;
using System.Security.Cryptography.X509Certificates;
using Spire.Xls;
using Spire.Xls.Core.MergeSpreadsheet.Interfaces;
namespace Add_Digital_Signature
{
class Program
{
static void Main(string[] args)
{
//创建Workbook实例
Workbook workbook = new Workbook();
//加载Excel文档
workbook.LoadFromFile("Sample.xlsx");
//添加数字签名
X509Certificate2 cert = new X509Certificate2("gary.pfx", "e-iceblue");
DateTime certtime = new DateTime(2020, 7, 1, 7, 10, 36);
IDigitalSignatures signature = workbook.AddDigitalSignature(cert, "e-iceblue", certtime);
//保存结果文档
workbook.SaveToFile("AddDigitalSignature.xlsx", FileFormat.Version2013);
}
}
}
VB.NET
Imports System
Imports System.Security.Cryptography.X509Certificates
Imports Spire.Xls
Imports Spire.Xls.Core.MergeSpreadsheet.Interfaces
Namespace Add_Digital_Signature
Class Program
Private Shared Sub Main(ByVal args As String())
Dim workbook As Workbook = New Workbook()
workbook.LoadFromFile("Sample.xlsx")
Dim cert As X509Certificate2 = New X509Certificate2("gary.pfx", "e-iceblue")
Dim certtime As DateTime = New DateTime(2020, 7, 1, 7, 10, 36)
Dim signature As IDigitalSignatures = workbook.AddDigitalSignature(cert, "e-iceblue", certtime)
workbook.SaveToFile("AddDigitalSignature.xlsx", FileFormat.Version2013)
End Sub
End Class
End Namespace
添加数字签名后的文档截图:
删除数字签名
C#
using Spire.Xls;
namespace Delete_Digital_Signature
{
class Program
{
static void Main(string[] args)
{
//创建Workbook实例
Workbook workbook = new Workbook();
//加载Excel文档
workbook.LoadFromFile("AddDigitalSignature.xlsx");
//删除文档中的数字签名
workbook.RemoveAllDigitalSignatures();
//保存结果文档
workbook.SaveToFile("RemoveDigitalSignature.xlsx", FileFormat.Version2013);
}
}
}
VB.NET
Imports Spire.Xls
Namespace Delete_Digital_Signature
Class Program
Private Shared Sub Main(ByVal args As String())
Dim workbook As Workbook = New Workbook()
workbook.LoadFromFile("AddDigitalSignature.xlsx")
workbook.RemoveAllDigitalSignatures()
workbook.SaveToFile("RemoveDigitalSignature.xlsx", FileFormat.Version2013)
End Sub
End Class
End Namespace
删除数字签名后的文档截图: