如何用 C# 编辑 Excel 工作簿元数据
using IronXL 的 Metadata 属性在 C# 中编辑 Excel 元数据,以编程方式设置 keywords 及其他文档属性,无需 Microsoft Interop,实现自动化电子表格组织和可搜索性。
Excel 电子表格的元数据包括有关 modification date 和其他相关详细信息的信息。 元数据提供上下文信息,有助于组织和分类电子表格。 它简化了文件搜索和管理,尤其是在处理多个电子表格文件时。 无论您是创建新的电子表格,还是加载现有的工作簿,IronXL.Excel 都能让元数据管理无懈可击。
通过IronXL的Metadata接口设置、修改和保存Keywords等属性。 无需互操作——只需几行简洁直观的 C# 代码即可立即上手。
-
1Install IronXL with NuGet Package Manager
-
2复制并运行这段代码。
IronXL.WorkBook.Load("input.xlsx").Metadata.Title = "Financial Summary"; // Then save your update to a new file IronXL.WorkBook.Load("input.xlsx").SaveAs("output.xlsx");C# -
3部署到您的生产环境中进行测试
通过免费试用立即在您的项目中开始使用IronXL
最小工作流程(5 个步骤)
- 下载 C# 库以编辑工作簿元数据
- 加载现有电子表格或创建全新的电子表格
- 使用
Metadata属性访问和修改元数据信息 - 查看和修改电子表格数据
- 导出具有编辑后元数据属性的电子表格
如何编辑工作簿元数据属性?
要编辑电子表格文件的作者姓名,请设置Author属性为所需的数据字符串。 例如,workBook.Metadata.Author = "Your Name"。 可以访问和检索Metadata属性中的元数据信息。 这种方法与包括CSV格式在内的各种电子表格文件类型无缝兼容。
哪些属性可以通过编程修改?
using IronXL;
using System;
WorkBook workBook = WorkBook.Load("sample.xlsx");
// Set author
workBook.Metadata.Author = "Your Name";
// Set comments
workBook.Metadata.Comments = "Monthly report";
// Set title
workBook.Metadata.Title = "July";
// Set keywords
workBook.Metadata.Keywords = "Report";
// Read the creation date of the excel file
DateTime? creationDate = workBook.Metadata.Created;
// Read the last printed date of the excel file
DateTime? printDate = workBook.Metadata.LastPrinted;
workBook.SaveAs("editedMetadata.xlsx");Imports IronXL
Imports System
Dim workBook As WorkBook = WorkBook.Load("sample.xlsx")
' Set author
workBook.Metadata.Author = "Your Name"
' Set comments
workBook.Metadata.Comments = "Monthly report"
' Set title
workBook.Metadata.Title = "July"
' Set keywords
workBook.Metadata.Keywords = "Report"
' Read the creation date of the excel file
Dim creationDate As DateTime? = workBook.Metadata.Created
' Read the last printed date of the excel file
Dim printDate As DateTime? = workBook.Metadata.LastPrinted
workBook.SaveAs("editedMetadata.xlsx")对于更复杂的情况,您可以将元数据编辑与其他 Excel 操作结合起来。 下面是一个演示批量处理多个 Excel 文件的综合示例:
using IronXL;
using System;
using System.IO;
public class BatchMetadataProcessor
{
public static void ProcessFinancialReports(string folderPath)
{
// Get all Excel files in the directory
string[] excelFiles = Directory.GetFiles(folderPath, "*.xlsx");
foreach (string filePath in excelFiles)
{
// Load the workbook
WorkBook workBook = WorkBook.Load(filePath);
// Update metadata based on file content
string fileName = Path.GetFileNameWithoutExtension(filePath);
// Set consistent metadata across all reports
workBook.Metadata.Author = "Finance Department";
workBook.Metadata.Company = "Your Company Name";
workBook.Metadata.Category = "Financial Reports";
// Set dynamic metadata based on filename
if (fileName.Contains("Q1"))
{
workBook.Metadata.Title = "Q1 Financial Report";
workBook.Metadata.Keywords = "Q1, Finance, Quarterly";
}
else if (fileName.Contains("Q2"))
{
workBook.Metadata.Title = "Q2 Financial Report";
workBook.Metadata.Keywords = "Q2, Finance, Quarterly";
}
// Add timestamp to comments
workBook.Metadata.Comments = $"Processed on {DateTime.Now:yyyy-MM-dd HH:mm}";
// Set the subject based on worksheet content
WorkSheet sheet = workBook.DefaultWorkSheet;
workBook.Metadata.Subject = $"Report containing {sheet.RowCount} data rows";
// Save with updated metadata
string outputPath = Path.Combine(folderPath, "processed", fileName + "_updated.xlsx");
workBook.SaveAs(outputPath);
}
}
}Imports IronXL
Imports System
Imports System.IO
Public Class BatchMetadataProcessor
Public Shared Sub ProcessFinancialReports(folderPath As String)
' Get all Excel files in the directory
Dim excelFiles As String() = Directory.GetFiles(folderPath, "*.xlsx")
For Each filePath As String In excelFiles
' Load the workbook
Dim workBook As WorkBook = WorkBook.Load(filePath)
' Update metadata based on file content
Dim fileName As String = Path.GetFileNameWithoutExtension(filePath)
' Set consistent metadata across all reports
workBook.Metadata.Author = "Finance Department"
workBook.Metadata.Company = "Your Company Name"
workBook.Metadata.Category = "Financial Reports"
' Set dynamic metadata based on filename
If fileName.Contains("Q1") Then
workBook.Metadata.Title = "Q1 Financial Report"
workBook.Metadata.Keywords = "Q1, Finance, Quarterly"
ElseIf fileName.Contains("Q2") Then
workBook.Metadata.Title = "Q2 Financial Report"
workBook.Metadata.Keywords = "Q2, Finance, Quarterly"
End If
' Add timestamp to comments
workBook.Metadata.Comments = $"Processed on {DateTime.Now:yyyy-MM-dd HH:mm}"
' Set the subject based on worksheet content
Dim sheet As WorkSheet = workBook.DefaultWorkSheet
workBook.Metadata.Subject = $"Report containing {sheet.RowCount} data rows"
' Save with updated metadata
Dim outputPath As String = Path.Combine(folderPath, "processed", fileName & "_updated.xlsx")
workBook.SaveAs(outputPath)
Next
End Sub
End Class保存时,现有元数据会发生什么变化?
当您使用 IronXL 保存或导出 Excel 文件时,您未明确修改的任何元数据属性都会保留其原始值。 只有您更改过的属性才会在保存的文件中更新。这种选择性更新方法可确保在处理过程中不会意外丢失有价值的现有元数据。 下图显示了使用 IronXL.Excel 编辑后,元数据在 Excel 文档属性面板中的显示效果:

如果您需要在设置新值之前清除现有的元数据,只需为要重置的属性指定空字符串或空值即可。 在准备用于外部分发的文件时,如果要删除公司内部信息,这一点尤其有用。
IronXL 中提供哪些元数据字段?
并非所有元数据属性都可以编辑。 有些属性只能获取。 了解哪些属性支持不同的操作对于有效管理元数据至关重要。 在使用受密码保护的工作簿时,成功解密工作簿后仍可访问和修改元数据。
哪些属性支持读写操作?
| 属性 | 说明 | 业务 | 常见用例 |
|---|---|---|---|
Author | 文档创建者姓名 | 设置、修改、检索 | 跟踪文档所有权、合规性 |
Comments | 有关文件的其他说明 | 设置、修改、检索 | 版本说明、处理说明 |
LastPrinted | 最后一次打印操作的日期/时间 | 设置、修改、检索 | 打印历史跟踪、审计跟踪 |
Keywords | 可搜索关键词 | 设置、修改、检索 | 文档分类、搜索优化 |
Category | 文档类别分类 | 设置、修改、检索 | 文件组织、部门分类 |
Created | 文档创建日期 | 设置、修改、检索 | 文件年龄跟踪、存档决策 |
ModifiedDate | 最后修改日期 | 设置、修改、检索 | 变更跟踪、版本控制 |
Subject | 文件主题描述 | 设置、修改、检索 | 内容概括、快速识别 |
Title | 文件标题 | 设置、修改、检索 | 文件识别、报告 |
哪些属性是只读的?
| 属性 | 说明 | 典型值 |
|---|---|---|
ApplicationName | 创建文件的应用程序名称 | "Microsoft Excel"、"IronXL.Excel" |
CustomProperties | 用户自定义属性 | 因文件而异 |
Company | 与文件相关的公司名称 | 系统中的组织名称 |
Manager | 文件属性中的管理器名称 | 从原始文件中提取 |
Template | 用于创建文档的模板 | 模板文件名或 "正常 "文件名 |
有关高级元数据操作和完整的 API 文档,请参阅 IronXL API 参考。 如果您在元数据处理方面遇到任何问题,请查阅我们的故障排除指南或探索用于生产部署的许可选项。
常见问题解答
如何用 C# 编程编辑 Excel 元数据?
IronXL 在 WorkBook 类上提供了一个简单的元数据属性,允许您以编程方式编辑 Excel 元数据。无需 Microsoft Interop,您就可以轻松设置标题、作者、主题和关键词等属性。只需加载工作簿并访问 workBook.Metadata 即可修改任何元数据属性。
我可以修改 Excel 文件中的哪些元数据属性?
通过 IronXL,您可以修改各种元数据属性,包括作者、标题、主题、关键词、类别、评论、状态、经理和公司。该库还提供对创建和修改日期的只读访问,可对电子表格进行全面的元数据管理。
编辑 Excel 元数据需要安装 Microsoft Office 吗?
不,IronXL 不需要安装 Microsoft Office 或 Interop。它是一个独立的 C# 库,可以独立读取、写入和修改 Excel 文件及其元数据,因此非常适合服务器环境或未安装 Office 的系统。
能否批量处理多个 Excel 文件的元数据?
是的,IronXL 支持 Excel 文件的批处理。您可以遍历一个目录中的多个电子表格,使用 WorkBook.Load() 加载每个电子表格,修改其元数据属性,然后将其保存。这对于整理大量电子表格文件特别有用。
哪些 Excel 文件格式支持元数据编辑?
IronXL 的元数据编辑功能可与各种电子表格文件格式无缝兼容,包括 XLSX、XLS 和 CSV 文件。该库可在内部处理特定格式的细节,无论文件类型如何,您都可以使用相同的元数据属性界面。
编辑后如何保存元数据更改?
using IronXL 修改元数据属性后,只需调用 Save() 方法更新现有文件,或调用 SaveAs() 方法用更新后的元数据创建新文件。程序库会自动持久保存所有元数据更改以及电子表格数据修改。
How does IronXL handle metadata for password-protected workbooks?
IronXL can access and modify metadata for password-protected workbooks once they are successfully decrypted, allowing secure handling while maintaining data privacy.
What happens if I want to clear existing metadata before saving?
To clear existing metadata before saving, you can assign empty strings or null values to the metadata properties you want to reset, which is useful for preparing documents for external distribution.
Can metadata be updated based on the dynamic content of a file?
Yes, IronXL allows you to set dynamic metadata based on file content or existing file properties, making it adaptable for batch processing or specific content conditions.
Are there any properties in IronXL that are read-only?
Yes, certain properties like `ApplicationName`, `CustomProperties`, `Company`, `Manager`, and `Template` are read-only and cannot be modified through IronXL.

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。