Excel 文档属性(如标题、作者、类别等元数据)对于文件管理和信息检索至关重要。Spire.XLS for JavaScript 基于 WebAssembly 在浏览器端直接完成 Excel 文档属性的读取和删除操作,通过虚拟文件系统(VFS)管理输入输出文件,无需后端服务支持。它提供了完整的 API 用于访问和管理 DocumentProperties(标准文档属性)和 CustomDocumentProperties(自定义文档属性)。
Spire.XLS 组件将文档属性分为两类:标准文档属性和自定义文档属性。标准文档属性是由 Excel 预定义的内置属性,如标题、主题、作者等;而自定义文档属性则是用户根据需求自行添加的名称-值对,可以包含文本、数字、日期或布尔值。
本文介绍两个核心功能点:
有关安装和项目配置,请参考 React 项目中集成 Spire.XLS for JavaScript。以下示例默认已安装 Spire.XLS 并完成 WebAssembly 模块初始化。
读取 Excel 中的标准和自定义文档属性
读取文档属性是了解 Excel 文件元数据的第一步。通过 DocumentProperties 和 CustomDocumentProperties 集合,您可以轻松访问文件的所有属性信息。具体操作步骤如下:
- 创建
Workbook对象并加载已有的 Excel 文件。 - 通过
workbook.DocumentProperties获取标准文档属性集合。 - 遍历
DocumentProperties集合,读取每个属性的名称和值。 - 通过
workbook.CustomDocumentProperties获取自定义文档属性集合。 - 遍历
CustomDocumentProperties集合,读取每个自定义属性的名称和值。 - 将读取到的属性信息输出到文本文件。
下面是一个完整的代码示例,展示了在 React 中读取 Excel 文档属性:
function App() {
const readDocumentProperties = async () => {
// 获取 Spire.XLS WASM 模块
const xlsModule = window.wasmModule?.spirexls;
// 检查模块是否就绪
if (!xlsModule) {
alert('Spire.Xls is not ready yet');
return;
}
// 将示例文件加载到 VFS
await window.spire.FetchFileToVFS('Sample.xlsx', '', `${process.env.PUBLIC_URL}data/`);
const workbook = new xlsModule.Workbook();
workbook.LoadFromFile({ fileName: 'Sample.xlsx' });
// 获取常规 Excel 属性
let properties1 = workbook.DocumentProperties;
let sb = [];
sb.push("Excel Properties:");
for (let i = 0; i < properties1.Count; i++) {
let name = properties1.get(i).Name;
let obj = properties1.get(i).Value;
let t = properties1.get(i).PropertyType;
let value = null;
if (t === xlsModule.PropertyType.Double) {
value = xlsModule.Double.Convert(obj).Value;
} else if (t === xlsModule.PropertyType.DateTime) {
// 将 OADate 转换为 JavaScript Date 并格式化为日期字符串
let oaDate = xlsModule.DateTime.Convert(obj).Value;
let jsDate = new Date((oaDate - 25569) * 86400 * 1000);
value = jsDate.toLocaleDateString();
} else if (t === xlsModule.PropertyType.Bool) {
value = xlsModule.Boolean.Convert(obj).Value;
} else if (
t === xlsModule.PropertyType.Int ||
t === xlsModule.PropertyType.Int32
) {
value = xlsModule.Int32.Convert(obj).Value;
} else {
value = xlsModule.String.Convert(obj).Value;
}
sb.push(name + ": " + String(value));
}
sb.push("");
// 获取自定义属性
let properties2 = workbook.CustomDocumentProperties;
sb.push("Custom Properties:");
for (let i = 0; i < properties2.Count; i++) {
let name = properties2.get(i).Name;
let t = properties2.get(i).PropertyType;
let obj = properties2.get(i).Value;
let value = null;
if (t === xlsModule.PropertyType.Double) {
value = xlsModule.Double.Convert(obj).Value;
} else if (t === xlsModule.PropertyType.DateTime) {
// 将 OADate 转换为 JavaScript Date 并格式化为日期字符串
let oaDate = xlsModule.DateTime.Convert(obj).Value;
let jsDate = new Date((oaDate - 25569) * 86400 * 1000);
value = jsDate.toLocaleDateString();
} else if (t === xlsModule.PropertyType.Bool) {
value = xlsModule.Boolean.Convert(obj).Value;
} else if (
t === xlsModule.PropertyType.Int ||
t === xlsModule.PropertyType.Int32
) {
value = xlsModule.Int32.Convert(obj).Value;
} else {
value = xlsModule.String.Convert(obj).Value;
}
sb.push(name + ": " + String(value));
}
// 将属性信息保存到文本文件
const outputFileName = 'DocumentProperties.txt';
window.dotnetRuntime.Module.FS.writeFile(outputFileName, sb.join("\n"));
workbook.Dispose();
// 从 VFS 读取文件并触发下载
const fileArray = window.dotnetRuntime.Module.FS.readFile(outputFileName);
const blob = new Blob([fileArray], { type: 'text/plain' });
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>读取 Excel 文档属性</h1>
<button onClick={readDocumentProperties}>
生成
</button>
</div>
);
}
export default App;
使用 Spire.XLS for JavaScript 读取的文档属性

删除 Excel 中的标准和自定义文档属性
在某些场景下,您可能需要清除 Excel 文件中的敏感或过时的元数据信息。Spire.XLS for JavaScript 允许您通过简单的 API 调用删除标准和自定义文档属性。具体操作步骤如下:
- 创建
Workbook对象并加载已有的 Excel 文件。 - 通过
workbook.DocumentProperties获取标准文档属性集合。 - 将标准文档属性的值设为空字符串以清除其内容。
- 通过
workbook.CustomDocumentProperties获取自定义文档属性集合。 - 遍历集合,使用
Remove()方法删除每个自定义属性。 - 保存修改后的工作簿为新的 Excel 文件。
下面是一个完整的代码示例,展示了在 React 中删除 Excel 文档属性:
function App() {
const deleteDocumentProperties = async () => {
// 获取 Spire.XLS WASM 模块
const xlsModule = window.wasmModule?.spirexls;
// 检查模块是否就绪
if (!xlsModule) {
alert('Spire.Xls is not ready yet');
return;
}
// 将示例文件加载到 VFS
await window.spire.FetchFileToVFS('Sample.xlsx', '', `${process.env.PUBLIC_URL}data/`);
const workbook = new xlsModule.Workbook();
workbook.LoadFromFile({ fileName: 'Sample.xlsx' });
// 获取标准文档属性集合,并将各属性值设为空
let standardProperties = workbook.DocumentProperties;
standardProperties.Title = "";
standardProperties.Subject = "";
standardProperties.Manager = "";
standardProperties.Category = "";
standardProperties.Keywords = "";
standardProperties.Comments = "";
standardProperties.Author = "";
standardProperties.Company = "";
// 获取自定义文档属性集合,遍历并删除所有属性
let customProperties = workbook.CustomDocumentProperties;
for (let i = customProperties.Count - 1; i >= 0; i--) {
customProperties.Remove(customProperties.get(i).Name);
}
// 保存工作簿
const outputFileName = 'DeleteProperties.xlsx';
workbook.SaveToFile(outputFileName);
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>删除 Excel 文档属性</h1>
<button onClick={deleteDocumentProperties}>
生成
</button>
</div>
);
}
export default App;
使用 Spire.XLS for JavaScript 删除文档属性后的结果

常见问题
为什么标准文档属性不能像自定义属性一样使用 Remove() 删除?
原因:标准文档属性是 Excel 文件结构的一部分,每个属性都有固定的定义位置,无法从集合中移除。
解决方案:将标准属性的值设为空字符串以清除其内容,而非移除属性本身:
standardProperties.Title = "";
standardProperties.Author = "";
如何处理属性值为日期/布尔值/数字等非文本类型时的读取问题?
原因:直接使用 String.Convert() 处理日期或布尔值属性时,可能得到格式不符合预期的结果。
解决方案:根据 PropertyType 判断属性类型,并使用对应的类型转换方法:
if (t === xlsModule.PropertyType.DateTime) {
let oaDate = xlsModule.DateTime.Convert(obj).Value;
let jsDate = new Date((oaDate - 25569) * 86400 * 1000);
value = jsDate.toLocaleDateString();
} else if (t === xlsModule.PropertyType.Bool) {
value = xlsModule.Boolean.Convert(obj).Value;
}
获取免费许可证
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。







