本次代码测试环境
- Excel版本:2013
- 编译环境:IntelliJ IDEA2018
- JDK版本:1.8.0
- Excel库工具:spire.xls.jar 4.3.4
- Pfx数字证书(及密码)
下面分别以三个代码块来展示如何实现。
1. 添加签名import com.spire.xls.*;
import com.spire.xls.digital.CertificateAndPrivateKey;
import java.util.Date;
public class AddSignature {
public static void main(String[] args) throws Exception {
//加载Excel工作簿
Workbook wb = new Workbook();
wb.loadFromFile("test.xlsx");
//添加数字签名
wb.addDigitalSignature(new CertificateAndPrivateKey("cer.pfx","123654yes!"), "The Author is Mia",new Date());
//保存文档
wb.saveToFile("AddSignature.xlsx",ExcelVersion.Version2013);
wb.dispose();
}
}
import com.spire.xls.*;
import com.spire.xls.digital.CertificateAndPrivateKey;
import java.util.Date;
public class AddSignature {
public static void main(String[] args) throws Exception {
//加载Excel工作簿
Workbook wb = new Workbook();
wb.loadFromFile("test.xlsx");
//添加数字签名
wb.addDigitalSignature(new CertificateAndPrivateKey("cer.pfx","123654yes!"), "The Author is Mia",new Date());
//保存文档
wb.saveToFile("AddSignature.xlsx",ExcelVersion.Version2013);
wb.dispose();
}
}
签名添加结果:
2.检测是否签名
import com.spire.xls.*;
public class DetectSignature {
public static void main(String[] args) {
//加载Excel工作簿
Workbook wb = new Workbook();
wb.loadFromFile("AddSignature.xlsx");
//判断文档是否签名
boolean signature = wb.isDigitallySigned();
if(signature==true)
{
System.out.println("文档已签名!");
}
else
{
System.out.println("未签名文档!");
}
}
}
检测结果:
3.删除签名
import com.spire.xls.*;
public class RemoveSignature {
public static void main(String[] args) {
//加载Excel工作簿
Workbook wb = new Workbook();
wb.loadFromFile("AddSignature.xlsx");
//删除签名
wb.removeAllDigitalSignatures();
//保存文档
wb.saveToFile("RemoveSignature.xlsx",ExcelVersion.Version2013);
wb.dispose();
}
}
执行程序代码后,生成的结果文档中不再有签名。
关于如何使用Java代码实现Excel文档中的添加、删除和检测的功能介绍就到此结束了,更多关于Java 其他内容资料请关注W3Cschool其它相关文章!