数字签名是确保 Excel 文件来源可信、内容未被篡改的重要手段。Spire.XLS for JavaScript 基于 WebAssembly 在浏览器端直接完成数字签名检测与删除操作,通过虚拟文件系统(VFS)管理输入输出文件,无需后端服务支持。
本文介绍两个核心功能点:
有关安装和项目配置,请参考 React 项目中集成 Spire.XLS for JavaScript。以下示例默认已安装 Spire.XLS 并完成 WebAssembly 模块初始化。
检测 Excel 文件是否已签名
在处理已签名的 Excel 文件前,先检测其签名状态可以避免误操作。Spire.XLS 通过 IsDigitallySigned 属性判断工作簿是否包含数字签名:首先通过 FetchFileToVFS 将字体文件和目标 Excel 文件载入 WASM 虚拟文件系统;然后实例化 Workbook 加载文件;最后通过 IsDigitallySigned 属性获取签名状态。
下面是一个完整的代码示例,展示了检测 Excel 文件是否包含数字签名:
function App() {
const detectDigitalSignature = async () => {
// 获取 Spire.XLS WASM 模块
const xlsModule = window.wasmModule?.spirexls;
// 检查模块是否就绪
if (!xlsModule) {
alert('Spire.Xls is not ready yet');
return;
}
// 将字体和 Excel 文件载入 VFS
await window.spire.FetchFileToVFS('arial.ttf', '/Library/Fonts/', `${process.env.PUBLIC_URL}/font/`);
const inputFileName = 'Sample.xlsx';
await window.spire.FetchFileToVFS(inputFileName, '', `${process.env.PUBLIC_URL}data/`);
// 加载工作簿
const workbook = new xlsModule.Workbook();
workbook.LoadFromFile({ fileName: inputFileName });
// 检测是否包含数字签名
const isSigned = workbook.IsDigitallySigned;
// 释放资源
workbook.Dispose();
// 弹出检测结果
alert(isSigned ? '文件已签名' : '文件未签名');
};
return (
<div style={{ textAlign: 'center', height: '300px' }}>
<h1>Detect Digital Signature</h1>
<button onClick={detectDigitalSignature}>
Detect
</button>
</div>
);
}
export default App;
检测结果弹窗提示文件是否已签名

删除 Excel 文件中的数字签名
当需要更新签名信息、更换证书或取消文档的数字认证时,需从 Excel 文件中移除现有的数字签名。使用 Spire.XLS 时,核心流程分为三个阶段:首先通过 FetchFileToVFS 将字体文件和已签名的 Excel 文件载入 WASM 虚拟文件系统;然后实例化 Workbook 加载该文件,调用 RemoveAllDigitalSignatures 方法一次性移除工作簿中的所有数字签名;最后通过 SaveToFile 保存移除签名后的工作簿文件。
下面是一个完整的代码示例,展示了删除 Excel 文件中的所有数字签名:
function App() {
const removeDigitalSignatures = async () => {
// 获取 Spire.XLS WASM 模块
const xlsModule = window.wasmModule?.spirexls;
// 检查模块是否就绪
if (!xlsModule) {
alert('Spire.Xls is not ready yet');
return;
}
// 将字体和 Excel 文件载入 VFS
await window.spire.FetchFileToVFS('arial.ttf', '/Library/Fonts/', `${process.env.PUBLIC_URL}/font/`);
const inputFileName = 'Sample.xlsx';
await window.spire.FetchFileToVFS(inputFileName, '', `${process.env.PUBLIC_URL}data/`);
// 加载已签名的工作簿
const workbook = new xlsModule.Workbook();
workbook.LoadFromFile({ fileName: inputFileName });
// 移除所有数字签名
workbook.RemoveAllDigitalSignatures();
// 保存移除签名后的工作簿
const outputFileName = 'SignatureRemoved.xlsx';
workbook.SaveToFile({ fileName: outputFileName, version: xlsModule.ExcelVersion.Version2010 });
// 释放资源
workbook.Dispose();
// 从 VFS 读取文件,触发浏览器下载
const fileArray = window.dotnetRuntime.Module.FS.readFile(outputFileName);
const blob = new Blob([fileArray], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = outputFileName;
a.click();
URL.revokeObjectURL(url);
};
return (
<div style={{ textAlign: 'center', height: '300px' }}>
<h1>Remove Digital Signatures</h1>
<button onClick={removeDigitalSignatures}>
Remove Signatures
</button>
</div>
);
}
export default App;
删除数字签名后的输出文档

常见问题
能否只检测某个工作表是否有签名,而不是整个工作簿?
原因:数字签名是应用于整个工作簿的,而非单个工作表。
解决:数字签名作用于整个工作簿级别,无法针对单个工作表检测或删除签名。IsDigitallySigned 和 RemoveAllDigitalSignatures 都是工作簿级别的方法。
批量处理多个 Excel 文件时,如何逐个检测或删除签名?
原因:实际项目中经常需要处理大量文件,手动逐个操作效率低。
解决:可以使用循环批量处理:
const files = ['report1.xlsx', 'report2.xlsx', 'report3.xlsx'];
for (const file of files) {
await window.spire.FetchFileToVFS(file, '', dataPath);
const wb = new xlsModule.Workbook();
wb.LoadFromFile({ fileName: file });
if (wb.IsDigitallySigned) {
wb.RemoveAllDigitalSignatures();
}
wb.SaveToFile({ fileName: `unsigned_${file}`, version: xlsModule.ExcelVersion.Version2016 });
wb.Dispose();
}
获取免费许可证
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。









