从包中获取文档部件的内容

本主题演示如何使用 Open XML SDK for Office 中的类以编程方式检索 Wordprocessing 文档中文档部件的内容。


包和文档部件

Open XML 文档以包的形式存储,其格式由 ISO/IEC 29500-2(该链接可能指向英文页面) 定义。 包可以具有多个彼此之间存在关系的部件。 部件之间的关系控制文档的类别。 如果文档的包关系项包含与主文档部件的关系,可将文档定义为字处理文档。 如果文档的包关系项包含与演示文稿部件的关系,可将文档定义为演示文稿文档。 如果文档的包关系项包含与工作簿部件的关系,可将文档定义为电子表格文档。 在本操作方法主题中,您将使用字处理文档包。


获取 WordprocessingDocument 对象

该代码首先打开包文件,方法是将文件名传递到一个重载的 Open () 方法 (Visual Basic .NET Shared 方法或 采用字符串的 WordprocessingDocument 类的 C# 静态方法) ,该字符串和布尔值指定是否应在读/写模式下打开文件。 在此示例中,布尔值为 false,指定应以只读模式打开文件以避免意外更改。

    // Open a Wordprocessing document for editing.
    using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, false))
    {
          // Insert other code here.
    }

using 语句提供典型 .Create, .Save, .Close 序列的建议备选序列。 它确保在遇到右大括号时会自动调用 Dispose 方法(Open XML SDK 用来清理资源的内部方法)。 using 语句后面的块为 using 语句中创建或指定的对象设定范围,在此示例中这个范围就是 wordDoc。 由于 Open XML SDK 中的 WordprocessingDocument 类会在其实现 System.IDisposable 的过程中自动保存和关闭对象,并且由于在您退出块时会自动调用 Dispose 方法,因此只要您使用 using,就不必明确调用 SaveClose


WordProcessingML 文档的结构

WordProcessingML 文档的基本文档结构由 documentbody 元素组成,后跟一个或多个块级元素,如表示段落的 p。 一个段落包含一个或多个 r 元素。 r 代表一段连续文本,它是具有一组共同属性(如格式设置)的文本区域。 一段连续文本包含一个或多个 t 元素。 t 元素包含一个文本区域。 以下代码示例显示包含文本"Example text."的文档的 WordprocessingML 标记。

    <w:document xmlns:w="https://schemas.openxmlformats.org/wordprocessingml/2006/main">
      <w:body>
        <w:p>
          <w:r>
            <w:t>Example text.</w:t>
          </w:r>
        </w:p>
      </w:body>
    </w:document>

使用 Open XML SDK,可以使用与 WordprocessingML 元素对应的强类型类创建文档结构和内容。 这些类可在 DocumentFormat.OpenXml.Wordprocessing 命名空间中找到。 下表列出了 documentbodyprt 元素所对应类的类名称。

WordprocessingML 元素 Open XML SDK 类 说明
document Document 主文档部件的根元素
body Body 块级结构(如段落、表格、批注和 ISO/IEC 29500 规范中指定的其他项)的容器。
p Paragraph 段落。
r Run 一段连续文本。
t Text 文本范围。

有关 WordprocessingML 文档的各个部分和元素的整体结构的详细信息,请参阅 WordprocessingML 文档的结构


Comments 元素

在本操作方法中,您将处理注释。 因此,熟悉注释>元素的结构<非常有用。 ISO/IEC 29500(该链接可能指向英文页面) 规范中的以下信息在处理此元素时会很有用。

此元素指定当前文档中定义的所有注释。 它是 WordprocessingML 文档注释部分的根元素。对于 WordprocessingML 文档中注释部分的内容,请考虑以下 WordprocessingML 片段:

    <w:comments>
      <w:comment … >
        …
      </w:comment>
    </w:comments>

在此示例中, comments 元素包含此文档指定的单个注释。

© ISO/IEC29500: 2008.

以下 XML 架构片段定义此元素的内容。

    <complexType name="CT_Comments">
       <sequence>
           <element name="comment" type="CT_Comment" minOccurs="0" maxOccurs="unbounded"/>
       </sequence>
    </complexType>

示例代码的工作方式

打开源文件进行读取后,通过实例化 MainDocumentPart 创建一个 mainPart 对象。 然后创建对文档的 WordprocessingCommentsPart 部件的引用。

    // To get the contents of a document part.
    public static string GetCommentsFromDocument(string document)
    {
        string comments = null;

        using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))
        {
            MainDocumentPart mainPart = wordDoc.MainDocumentPart;
            WordprocessingCommentsPart WordprocessingCommentsPart = mainPart.WordprocessingCommentsPart;

然后可以使用 StreamReader 对象读取文档的 WordprocessingCommentsPart 部件的内容并返回它的内容。

    using (StreamReader streamReader = new StreamReader(WordprocessingCommentsPart.GetStream()))
            {
                comments = streamReader.ReadToEnd();
            }
        }
        return comments;

示例代码

以下代码检索 WordProcessing 文档包中包含的 WordprocessingCommentsPart 部件的内容。 可以通过调用 GetCommentsFromDocument 方法来运行程序,如以下示例所示。

    string document = @"C:\Users\Public\Documents\MyPkg5.docx";
    GetCommentsFromDocument(document);

以下是使用 C# 和 Visual Basic 编写的完整示例代码。


using DocumentFormat.OpenXml.Packaging;
using System;
using System.IO;

GetCommentsFromDocument(args[0]);

// To get the contents of a document part.
static string GetCommentsFromDocument(string document)
{
    string? comments = null;

    using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, false))
    {
        if (wordDoc is null)
        {
            throw new ArgumentNullException(nameof(wordDoc));
        }

        MainDocumentPart mainPart = wordDoc.MainDocumentPart ?? wordDoc.AddMainDocumentPart();
        WordprocessingCommentsPart WordprocessingCommentsPart = mainPart.WordprocessingCommentsPart ?? mainPart.AddNewPart<WordprocessingCommentsPart>();

        using (StreamReader streamReader = new StreamReader(WordprocessingCommentsPart.GetStream()))
        {
            comments = streamReader.ReadToEnd();
        }
    }

    return comments;
}

另请参阅

Open XML SDK 类库参考