本次代碼測試環(huán)境
- Excel版本:2013
- 編譯環(huán)境:IntelliJ IDEA2018
- JDK版本:1.8.0
- Excel庫工具:spire.xls.jar 4.3.4
- Pfx數(shù)字證書(及密碼)
下面分別以三個代碼塊來展示如何實(shí)現(xiàn)。
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");
//添加數(shù)字簽名
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");
//添加數(shù)字簽名
wb.addDigitalSignature(new CertificateAndPrivateKey("cer.pfx","123654yes!"), "The Author is Mia",new Date());
//保存文檔
wb.saveToFile("AddSignature.xlsx",ExcelVersion.Version2013);
wb.dispose();
}
}
簽名添加結(jié)果:
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("未簽名文檔!");
}
}
}
檢測結(jié)果:
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();
}
}
執(zhí)行程序代碼后,生成的結(jié)果文檔中不再有簽名。
關(guān)于如何使用Java代碼實(shí)現(xiàn)Excel文檔中的添加、刪除和檢測的功能介紹就到此結(jié)束了,更多關(guān)于Java 其他內(nèi)容資料請關(guān)注W3Cschool其它相關(guān)文章!