你是如何得到主题的? Word文档中的标题(不打开它)?

时间:2011-03-24 13:48:25

标签: c# ms-word ms-office

我想从Word文档中读取标题和主题字段,但不希望有启动Word的开销。

如果在Windows资源管理器中显示标题和主题列,然后导航到其中包含Word文档的文件夹,则会显示此信息。使用什么机制(除了Shell扩展),因为它的快速(但我不知道你是否真的需要安装Word才能工作),所以我猜它不会启动Word并打开每个文档。

我找到了Dsofile.dll的链接,我认为我可以使用它,但这对.doc和.docx文件有用吗?这是唯一的方法吗?

2 个答案:

答案 0 :(得分:5)

嗯......因为人们可能会认为“.doc”文件的时间正在传递,这里有一种方法可以从“.docx”文件中获取主题和标题(或“.xlsx”该文件的文件)

using System;
using System.IO;
using System.IO.Packaging; // Assembly WindowsBase.dll

namespace ConsoleApplication16
{
  class Program
  {
     static void Main(string[] args)
     {
        String path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
        String file = Path.Combine(path, "Doc1.docx");

        Package docx = Package.Open(file, FileMode.Open, FileAccess.Read);
        String subject = docx.PackageProperties.Subject;
        String title = docx.PackageProperties.Title;
        docx.Close();
     }
  }
}

我希望这对某人有用。

答案 1 :(得分:1)

您也可以通过XML阅读:How to extract information from Office files by using Office file formats and schemas

以下是关于如何以编程方式阅读Word文档的another example

在某种程度上,你必须要查看文件内的一种方式!