从流中打开字处理文档

本主题演示如何使用 Open XML SDK for Office 中的类以编程方式从流中打开Word处理文档。

何时从流中打开文档

如果你有一个应用程序(如 SharePoint 应用程序)使用流输入/输出来处理文档,并且希望使用 Open XML SDK 来处理其中一个文档,则这样做很容易实现。 如果文档存在,并且可以使用 Open XML SDK 打开文档,则尤其如此。 但是,假设在您的代码中此时的文档为打开流,那么您需要在何处利用 SDK 来处理文档? 这就是本主题要讨论的方案。 示例代码中的示例方法接受开放流作为参数,然后使用 Open XML SDK 向流后面的文档添加文本。

创建 WordprocessingDocument 对象

在 Open XML SDK 中WordprocessingDocument, 类表示Word文档包。 若要使用Word文档,请先从文档创建 类的WordprocessingDocument实例,然后使用该实例。 在基于文档创建实例后,您即可获得对包含文档文本的主文档部件的访问权限。 每个 Open XML 包都会包含一些数量的部件。 至少, WordprocessingDocument 必须包含充当文档main文本容器的 main 文档部件。 包中还可以包含其他部件。 请注意,在Word文档中,main文档部件中的文本在WordprocessingML包中使用标记表示为 XML。

若要从文档创建类实例, Open(Stream, Boolean) 请调用 方法。提供了多种 Open 方法,每个方法都有不同的签名。 本主题中的示例代码使用 Open 具有需要两个参数的签名的方法。 第一个参数采用要从中打开文档的流的句柄。 第二个参数为 truefalse ,表示是否打开流进行编辑。

下面的代码示例调用 Open 方法。

// Open a WordProcessingDocument based on a stream.
using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(stream, true))
{

WordProcessingML 文档的结构

文档的基本文档结构WordProcessingML由 和 body 元素组成document,后跟一个或多个块级元素(例如 p表示段落)。 段落包含一个或多个 r 元素。 r代表 run,它是具有一组通用属性(如格式设置)的文本区域。 运行包含一个或多个 t 元素。 元素 t 包含文本范围。 下面的代码示例演示 WordprocessingML 包含文本“示例文本”的文档的标记。

    <w:document xmlns:w="http://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 强类型类创建文档结构和内容。 可以在 命名空间中找到 这些类。 下表列出了对应于 、、bodyrpt 元素的类的document类名。

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

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

示例代码的工作方式

打开 Word 文档包后,即可向主文档部件中添加文本。 若要访问main文档部件的正文,请分配对文档正文的引用,如以下代码段所示。

// Assign a reference to the document body.
MainDocumentPart mainDocumentPart = wordprocessingDocument.MainDocumentPart ?? wordprocessingDocument.AddMainDocumentPart();
mainDocumentPart.Document ??= new Document();
Body body = mainDocumentPart.Document.Body ?? mainDocumentPart.Document.AppendChild(new Body());

访问 main 文档部件的正文时,请通过添加 、 RunText 类的Paragraph实例来添加文本。 这会生成所需的 WordprocessingML 标记。 示例代码中的下列行添加段落和一段连续文本。

// Add new text.
Paragraph para = body.AppendChild(new Paragraph());
Run run = para.AppendChild(new Run());
run.AppendChild(new Text(txt));

示例代码

此处所示的示例OpenAndAddToWordprocessingStream方法可用于从已打开的流中打开Word文档,并使用 Open XML SDK 追加一些文本。 通过传递用于打开流的句柄作为第一个参数,并传递要添加的文本作为第二个参数,可以调用此方法。 例如,下面的代码示例打开第一个参数中指定的文件,并将第二个参数中的文本添加到其中。

string filePath = args[0];
string txt = args[1];

using (FileStream fileStream = new FileStream(filePath, FileMode.Open))
{
    OpenAndAddToWordprocessingStream(fileStream, txt);
}

注意

请注意, OpenAddAddToWordprocessingStream 方法不会关闭传递给它的流。 调用代码必须通过将方法调用包装在 语句中 using 或显式调用 Dispose 来执行此操作。

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

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


static void OpenAndAddToWordprocessingStream(Stream stream, string txt)
{
    // Open a WordProcessingDocument based on a stream.
    using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(stream, true))
    {

        // Assign a reference to the document body.
        MainDocumentPart mainDocumentPart = wordprocessingDocument.MainDocumentPart ?? wordprocessingDocument.AddMainDocumentPart();
        mainDocumentPart.Document ??= new Document();
        Body body = mainDocumentPart.Document.Body ?? mainDocumentPart.Document.AppendChild(new Body());

        // Add new text.
        Paragraph para = body.AppendChild(new Paragraph());
        Run run = para.AppendChild(new Run());
        run.AppendChild(new Text(txt));
    }
    // Caller must close the stream.
}