使用 C# 代码读取或删除 Excel 文档属性

Excel 文档属性(也称为元数据)能够提供有关文件内容、作者以及创建和修改历史等信息,有助于更高效地管理、分类和检索 Excel 文件。除了向 Excel 文件添加文档属性外,本文还将介绍如何在 C# 中读取或删除 Excel 文档属性。

安装 Excel 处理库

开始之前,需要在 .NET 项目中添加相应的 Excel 处理库引用。您可以下载所需的 DLL 文件并手动添加到项目中,也可以通过 NuGet 进行安装。

cs 复制代码
PM> Install-Package Spire.XLS

在 C# 中读取 Excel 的标准文档属性和自定义文档属性

Excel 文档属性主要分为以下两类:

  • 标准属性(Standard Properties):Excel 文件内置的预定义属性,通常包括标题(Title)、主题(Subject)、作者(Author)、关键词(Keywords)等基本信息。
  • 自定义属性(Custom Properties):用户根据实际需求添加的属性,用于记录和管理文件的附加信息。

借助 Excel 处理库,可以同时读取 Excel 文件中的标准文档属性和自定义文档属性。具体步骤如下:

  1. 创建一个 Workbook 对象。
  2. 使用 Workbook.LoadFromFile() 方法加载 Excel 文件。
  3. 创建一个 StringBuilder 对象,用于存储读取结果。
  4. 通过 Workbook.DocumentProperties 属性获取所有标准文档属性集合。
  5. 使用 BuiltInDocumentProperties 类提供的属性获取指定的标准文档属性,并将其追加到 StringBuilder 对象中。
  6. 通过 Workbook.CustomDocumentProperties 属性获取所有自定义文档属性集合。
  7. 遍历该集合。
  8. 使用 IDocumentProperty.NameIDocumentProperty.Value 属性获取每个自定义文档属性的名称和值,并将其追加到 StringBuilder 对象中。
  9. StringBuilder 中的内容写入 TXT 文件。

完整示例代码如下:

cs 复制代码
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() 方法将其删除。具体步骤如下:

  1. 创建一个 Workbook 实例。
  2. 使用 Workbook.LoadFromFile() 方法加载示例 Excel 文件。
  3. 通过 Workbook.DocumentProperties 属性获取所有标准文档属性的集合。
  4. 通过 BuiltInDocumentProperties 类对应的属性,将指定标准文档属性的值设置为空。
  5. 通过 Workbook.CustomDocumentProperties 属性获取所有自定义文档属性的集合。
  6. 遍历该集合。
  7. 使用 ICustomDocumentProperties.Remove(string strName) 方法,根据名称删除集合中的每个自定义文档属性。
  8. 使用 Workbook.SaveToFile() 方法保存结果文件。

完整示例代码如下:

cs 复制代码
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 文件的标准文档属性和自定义文档属性。对于标准文档属性,可以通过将标题、主题、类别、关键词和备注等属性值设置为空来清除相关信息;对于自定义文档属性,则可以遍历属性集合,并根据属性名称逐个删除。

通过这种方式,您可以有效移除 Excel 文件中的元数据,减少不必要的信息暴露,保护文档隐私,并确保文件在共享或发布前不包含敏感的作者信息、历史记录或其他自定义属性。该方法适用于需要清理文档信息、规范文件管理或满足数据安全要求的场景。

相关推荐
元Y亨H5 小时前
Pandas 解析 Excel 导致的内存溢出(MemoryError)
python·excel
姜小二10 小时前
QAxObject实现读写excel
嵌入式硬件·excel
E_ICEBLUE12 小时前
Python 拆分 Excel 文件:使用 Spire.XLS 实现按工作表、行、列和条件拆分
python·excel
蓝创工坊Blue Foundry12 小时前
批量提取图片中的数字:怎样整理成一张可核对的 Excel
python·ai·ocr·excel·paddlepaddle
噢,我明白了1 天前
java中Excel的导入和导出(EasyExcel)
java·开发语言·excel
REST_30271 天前
告别Excel孤岛:一款任务链路可视化工具带来的真实改变
大数据·微服务·云计算·编辑器·excel·动态规划
蓝创工坊Blue Foundry2 天前
多个同模板 PDF,怎样批量提取同一类字段到 Excel
运维·数据库·pdf·自动化·ocr·excel
蓝创工坊Blue Foundry2 天前
图片文字提取到 Excel:批量任务如何先定义要交付的字段
运维·服务器·开发语言·数据库·自动化·ocr·excel
用户298698530143 天前
Python 实现 Excel 与 Markdown 互转的实用指南
后端·python·excel
2501_930707783 天前
如何使用C#代码在 Excel 中编辑或删除批注
excel