C#代码实现读取或删除Excel文档属性
Excel 文档属性(也称为元数据)能够提供有关文件内容、作者以及创建和修改历史等信息,有助于更高效地管理、分类和检索 Excel 文件。除了向 Excel 文件添加文档属性外,本文还将介绍如何在 C# 中读取或删除 Excel 文档属性。
安装 Excel 处理库
开始之前,需要在 .NET 项目中添加相应的 Excel 处理库引用。您可以下载所需的 DLL 文件并手动添加到项目中,也可以通过 NuGet 进行安装。
PM> Install-Package Spire.XLS
在 C# 中读取 Excel 的标准文档属性和自定义文档属性
Excel 文档属性主要分为以下两类:
- 标准属性(Standard Properties):Excel 文件内置的预定义属性,通常包括标题(Title)、主题(Subject)、作者(Author)、关键词(Keywords)等基本信息。
- 自定义属性(Custom Properties):用户根据实际需求添加的属性,用于记录和管理文件的附加信息。
借助 Excel 处理库,可以同时读取 Excel 文件中的标准文档属性和自定义文档属性。具体步骤如下:
- 创建一个
Workbook对象。 - 使用
Workbook.LoadFromFile()方法加载 Excel 文件。 - 创建一个
StringBuilder对象,用于存储读取结果。 - 通过
Workbook.DocumentProperties属性获取所有标准文档属性集合。 - 使用
BuiltInDocumentProperties类提供的属性获取指定的标准文档属性,并将其追加到StringBuilder对象中。 - 通过
Workbook.CustomDocumentProperties属性获取所有自定义文档属性集合。 - 遍历该集合。
- 使用
IDocumentProperty.Name和IDocumentProperty.Value属性获取每个自定义文档属性的名称和值,并将其追加到StringBuilder对象中。 - 将
StringBuilder中的内容写入 TXT 文件。
完整示例代码如下:
using Spire.Xls;
using Spire.Xls.Collections;
using Spire.Xls.Core;
using System.IO;
using System.Text;
namespace GetExcelProperties
{
class Program
{
static void Main(string[] args)
{
{
//创建 Workbook 实例
Workbook workbook = new Workbook();
//加载示例 Excel 文件
workbook.LoadFromFile("Budget Template.xlsx");
//创建 StringBuilder 实例
StringBuilder sb = new StringBuilder();
//获取所有标准文档属性的集合
BuiltInDocumentProperties standardProperties = workbook.DocumentProperties;
//获取指定的标准属性并追加到 StringBuilder 实例中
sb.AppendLine("Standard Document Properties:");
sb.AppendLine("Title: " + standardProperties.Title);
sb.AppendLine("Subject: " + standardProperties.Subject);
sb.AppendLine("Category: " + standardProperties.Category);
sb.AppendLine("Keywords: " + standardProperties.Keywords);
sb.AppendLine("Comments: " + standardProperties.Comments);
sb.AppendLine();
//获取所有自定义文档属性的集合
ICustomDocumentProperties customProperties = workbook.CustomDocumentProperties;
sb.AppendLine("Custom Document Properties:");
//遍历集合
for (int i = 0; i < customProperties.Count; i++)
{
//获取每个自定义文档属性的名称和值,并追加到 StringBuilder 实例中
string name = customProperties[i].Name;
string value = customProperties[i].Value.ToString();
sb.AppendLine(name + ": " + value);
}
//将 StringBuilder 实例中的内容写入文本文件
File.WriteAllText("GetExcelProperties.txt", sb.ToString());
}
}
}
}在 C# 中删除 Excel 的标准文档属性和自定义文档属性
您可以通过将标准文档属性的值设置为空来删除 Excel 文件中的标准文档属性。对于自定义文档属性,则可以使用 ICustomDocumentProperties.Remove() 方法将其删除。具体步骤如下:
- 创建一个
Workbook实例。 - 使用
Workbook.LoadFromFile()方法加载示例 Excel 文件。 - 通过
Workbook.DocumentProperties属性获取所有标准文档属性的集合。 - 通过
BuiltInDocumentProperties类对应的属性,将指定标准文档属性的值设置为空。 - 通过
Workbook.CustomDocumentProperties属性获取所有自定义文档属性的集合。 - 遍历该集合。
- 使用
ICustomDocumentProperties.Remove(string strName)方法,根据名称删除集合中的每个自定义文档属性。 - 使用
Workbook.SaveToFile()方法保存结果文件。
完整示例代码如下:
using Spire.Xls;
using Spire.Xls.Collections;
using Spire.Xls.Core;
namespace DeleteExcelProperties
{
class Program
{
static void Main(string[] args)
{
{
//创建 Workbook 实例
Workbook workbook = new Workbook();
//加载示例 Excel 文件
workbook.LoadFromFile("Budget Template.xlsx");
//获取所有标准文档属性的集合
BuiltInDocumentProperties standardProperties = workbook.DocumentProperties;
//将每个标准文档属性的值设置为空
standardProperties.Title = "";
standardProperties.Subject = "";
standardProperties.Category = "";
standardProperties.Keywords = "";
standardProperties.Comments = "";
//获取所有自定义文档属性的集合
ICustomDocumentProperties customProperties = workbook.CustomDocumentProperties;
//遍历集合
for (int i = customProperties.Count -1; i >=0; i--)
{
//根据名称从集合中删除每个自定义文档属性
customProperties.Remove(customProperties[i].Name);
}
//保存结果文件
workbook.SaveToFile("DeleteDocumentProperties.xlsx", ExcelVersion.Version2016);
}
}
}
}知识扩展
在 C# 中操作 Excel 文档属性,核心是通过 Workbook 对象访问 DocumentProperties(内置属性)和 CustomDocumentProperties(自定义属性)两套 API。无论使用哪个库,逻辑都是统一的:读取即访问属性并输出,删除则将内置属性值设为空字符串,自定义属性调用 Remove 方法移除。
使用 Spire.XLS(推荐,API 简洁)
Spire.XLS 的 BuiltInDocumentProperties 和 ICustomDocumentProperties 接口提供了完善的属性操作能力。
安装
PM> Install-Package Spire.XLS
读取文档属性
using Spire.Xls;
using Spire.Xls.Collections;
using System.Text;
class Program
{
static void Main()
{
// 创建 Workbook 对象并加载 Excel 文件
Workbook workbook = new Workbook();
workbook.LoadFromFile("示例文档.xlsx");
// 获取标准文档属性集合
BuiltInDocumentProperties standardProps = workbook.DocumentProperties;
// 获取自定义文档属性集合
ICustomDocumentProperties customProps = workbook.CustomDocumentProperties;
StringBuilder sb = new StringBuilder();
// 读取标准属性
sb.AppendLine("=== 标准文档属性 ===");
sb.AppendLine($"标题 (Title): {standardProps.Title}");
sb.AppendLine($"主题 (Subject): {standardProps.Subject}");
sb.AppendLine($"作者 (Author): {standardProps.Author}");
sb.AppendLine($"管理者 (Manager): {standardProps.Manager}");
sb.AppendLine($"公司 (Company): {standardProps.Company}");
sb.AppendLine($"类别 (Category): {standardProps.Category}");
sb.AppendLine($"关键词 (Keywords): {standardProps.Keywords}");
sb.AppendLine($"备注 (Comments): {standardProps.Comments}");
// 读取自定义属性
sb.AppendLine("\n=== 自定义文档属性 ===");
for (int i = 0; i < customProps.Count; i++)
{
sb.AppendLine($"{customProps[i].Name}: {customProps[i].Value}");
}
// 输出到控制台或文件
System.Console.WriteLine(sb.ToString());
}
}关键属性说明:Title(标题)、Subject(主题)、Author(作者)、Keywords(关键词)、Comments(备注)等是常用的内置文档属性。
删除文档属性
using Spire.Xls;
class Program
{
static void Main()
{
Workbook workbook = new Workbook();
workbook.LoadFromFile("示例文档.xlsx");
// 删除标准属性:将其值设置为空字符串
workbook.DocumentProperties.Title = "";
workbook.DocumentProperties.Subject = "";
workbook.DocumentProperties.Author = "";
workbook.DocumentProperties.Keywords = "";
workbook.DocumentProperties.Comments = "";
// 删除自定义属性:使用 Remove 方法
// 方法一:按名称删除
workbook.CustomDocumentProperties.Remove("自定义属性名称");
// 方法二:按索引删除
// workbook.CustomDocumentProperties.RemoveAt(0);
// 保存修改后的文件(可覆盖原文件或另存为新文件)
workbook.SaveToFile("文档属性已清理.xlsx", FileFormat.Version2016);
}
}使用 Aspose.Cells(企业级方案)
Aspose.Cells 通过 Workbook.BuiltInDocumentProperties 和 Workbook.CustomDocumentProperties 提供相同的操作能力。
安装
PM> Install-Package Aspose.Cells
读取文档属性
using Aspose.Cells;
using Aspose.Cells.Properties;
class Program
{
static void Main()
{
Workbook workbook = new Workbook("示例文档.xlsx");
// 获取内置属性集合
BuiltInDocumentPropertyCollection builtinProps = workbook.BuiltInDocumentProperties;
// 获取自定义属性集合
CustomDocumentPropertyCollection customProps = workbook.CustomDocumentProperties;
// 读取内置属性
System.Console.WriteLine($"标题: {builtinProps.Title}");
System.Console.WriteLine($"作者: {builtinProps.Author}");
System.Console.WriteLine($"关键词: {builtinProps.Keywords}");
// 遍历自定义属性
foreach (DocumentProperty prop in customProps)
{
System.Console.WriteLine($"{prop.Name}: {prop.Value}");
}
}
}删除文档属性
using Aspose.Cells;
class Program
{
static void Main()
{
Workbook workbook = new Workbook("示例文档.xlsx");
// 清空内置属性(将其值设为空)
workbook.BuiltInDocumentProperties.Title = "";
workbook.BuiltInDocumentProperties.Subject = "";
workbook.BuiltInDocumentProperties.Author = "";
// 删除自定义属性
workbook.CustomDocumentProperties.Remove("自定义属性名称");
// 保存修改后的文件
workbook.Save("文档属性已清理.xlsx");
}
}总结
本文介绍了如何在 C# 中删除 Excel 文件的标准文档属性和自定义文档属性。对于标准文档属性,可以通过将标题、主题、类别、关键词和备注等属性值设置为空来清除相关信息;对于自定义文档属性,则可以遍历属性集合,并根据属性名称逐个删除。
通过这种方式,您可以有效移除 Excel 文件中的元数据,减少不必要的信息暴露,保护文档隐私,并确保文件在共享或发布前不包含敏感的作者信息、历史记录或其他自定义属性。该方法适用于需要清理文档信息、规范文件管理或满足数据安全要求的场景。
到此这篇关于C#代码实现读取或删除Excel文档属性的文章就介绍到这了,更多相关C#读取或删除Excel文档属性内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!


最新评论