通过提供文件名创建字处理文档

本主题演示如何使用 Open XML SDK for Office 中的类以编程方式创建字处理文档。


创建 WordprocessingDocument 对象

在 Open XML SDK 中WordprocessingDocument, 类表示Word文档包。 若要创建Word文档,请创建 类的WordprocessingDocument实例并用部件填充它。 该文档至少必须具有一个充当文档主文本的容器的主文档部件。 在此包中,使用 WordprocessingML 标记将文本表示为 XML 形式。

若要创建类实例, Create(String, WordprocessingDocumentType) 请调用 方法。 提供了多种 Create 方法,每个方法都有不同的签名。 本主题中的示例代码使用 Create 具有需要两个参数的签名的方法。 第一个参数接受一个完整的路径字符串,它表示你想要创建的文档。 第二个参数是 枚举的成员 WordprocessingDocumentType 。 此参数表示文档的类型。 例如,每个文档、模板以及启用了各种文档和模板的宏都有不同的枚举成员 WordProcessingDocumentType

注意

请仔细选择适当的 WordProcessingDocumentType ,并验证保存的文件是否具有正确匹配的文件扩展名。 WordProcessingDocumentType如果 与文件扩展名不匹配,则当您在 Microsoft Word 中打开文件时会出现错误。

调用 Create 方法的代码是语句的一 using 部分,后跟括号块,如以下代码示例所示。

using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(filepath, WordprocessingDocumentType.Document))
{

在 v3.0.0+ 中, Close() 已删除 方法,转而依赖于 using 语句。 它确保在 Dispose() 到达右大括号时自动调用 方法。 using 语句后面的块为 using 语句中创建或指定的对象设定范围。 WordprocessingDocument由于 Open XML SDK 中的 类会自动保存并关闭对象作为其IDisposable实现的一using部分,并且由于Dispose()在退出块时会自动调用,因此无需显式调用 Save()Dispose() ,只要使用 语句。

在创建了 Word 文档包后,您即可向其中添加部件。 若要添加 main 文档部件,AddMainDocumentPart请调用 类的 WordprocessingDocument 方法。 执行此操作后,您可以开始添加文档结构和文本。


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

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

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


生成 WordprocessingML 标记

若要使用 Open XML SDK 创建基本文档结构,请实例化 Document 类,将其分配给 Document main 文档部件的 属性,然后添加 、 ParagraphRunText 类的Body实例。 以下示例代码列表中对此进行了演示,并执行生成必需的 WordprocessingML 标记的工作。 虽然示例列表中的代码调用 AppendChild 每个类的 方法,但有时可以使用以下代码示例中显示的技术使代码更短且更易于阅读。

    mainPart.Document = new Document(
       new Body(
          new Paragraph(
             new Run(
                new Text("Create text in body - CreateWordprocessingDocument")))));

示例代码

方法CreateWordprocessingDocument可用于创建基本Word文档。 可以通过将完整路径作为唯一参数传递来调用此方法。 下面的代码示例在 Public Documents 文件夹中创建 Invoice.docx 文件。

CreateWordprocessingDocument(args[0]);

文件扩展名 .docx 与对 Create 方法的调用中 WordprocessingDocumentType.Document 参数指定的文件的类型相匹配。

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

static void CreateWordprocessingDocument(string filepath)
{
    // Create a document by supplying the filepath. 
    using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(filepath, WordprocessingDocumentType.Document))
    {
        // Add a main document part. 
        MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();

        // Create the document structure and add some text.
        mainPart.Document = new Document();
        Body body = mainPart.Document.AppendChild(new Body());
        Paragraph para = body.AppendChild(new Paragraph());
        Run run = para.AppendChild(new Run());
        run.AppendChild(new Text("Create text in body - CreateWordprocessingDocument"));
    }

另请参阅