[Office]使用 OpenXML SDK 建立 Word 文件檔

  • 37877
  • 0
  • 2012-02-16

[Office]使用 OpenXML SDK 建立 Word 文件檔

 

一、簡介

在 Microsoft Office 2007 版本以後的原始文件格式為 Open XML,Open XML 是一種 Ecma 標準,其中文字處理檔案的標記語言稱為 WordprocessingML。本文說明如何使用 OpenXML SDK 建立 Word 檔以及 WordprocessingML 的結構。

關於如何在 Visual Studio 2008 專案加入 Open XML SDK 參考,請參考先前的文章 [Office][C#] NPOI、OpenXML SDK、OpenOffice.org SDK 寫入資料到 EXCEL 檔案

並先於專案前 using 相關 namespace

 

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

 

 

 

二、WordprocessingDocument 類別

在 OpenXML SDK 中,WordprocessingDocument  類別作為 Word 文件封包。要建立 Word 文件,必須先實例化 WordprocessingDocument 類別並且將資料填入。要填入的資料最少要包含 WordprocessingML 主文件部分必要結構。我們可以透過 WordprocessingDocument  類別中的 Create 方法建立 Word 文件,其中最簡單的多載是傳入兩個參數,分別是 filePath (檔案路徑名稱) 與 WordprocessingDocumentType (Word 文件類型)。

string filepath = @"C:\test.xlsx";
WordprocessingDocument wordDocument = WordprocessingDocument.Create(filepath, WordprocessingDocumentType.Document);

在 WordprocessingDocumentType 部分,可以指定的文件類型有 :

  • Document : Word 文件 (*.docx)
  • MacroEnabledDocument : Word 啟用巨集的文件 (*.docm)
  • MacroEnabledTemplate : Word 啟用巨集的範本 (*.dotm)
  • Template : Word 範本(*.dotx)

請注意檔案副檔名需配合 WordprocessingDocumentType,否則透過 Microsoft Office 開啟檔案會判斷錯誤。在程式撰寫上,MSDN 建議使用 using 的方式,好處是在程式執行完離開括號時會自動做 Dispose() 資源釋放,如以下程式碼所示 :

string filepath = @"C:\test.xlsx";
WordprocessingDocument wordDocument = WordprocessingDocument.Create(filepath, WordprocessingDocumentType.Document)
{
    // 插入其它程式碼
}

產生 Word 文件封包後,使用 WordprocessingDocument  類別中的 AddMainDocumentPart 方法填入主文件部分,之後就可以將 Word 相關結構與文字陸續加入。對於 OpenXML WordProcessingML 文件格式而言,最基本的是 document 和 body 元素。document 包含文件主要宣告 http://schemas.openxmlformats.org/wordprocessingml/2006/main  namespace,通常簡寫為 w,緊接著是 body 本文內容,而我們可以在 body 中加入多個段落 Paragraph (P),並在每個段落中加入不同的文字屬性 Run (R) 範圍,並填入文字資料 Text (t)

舉個例子,如下的 docx Word 文件檔案

image

其 WordprocessingML 如下所示 :

<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:body>
    <w:p>
      <w:r>
        <w:t>在 body 本文內容產生 text 文字</w:t>
      </w:r>
    </w:p>
  </w:body>
</w:document>

 

在 OpenXML SDK 2.0 產生 WordprocessingML 元素結構,可以使用 DocumentFormat.OpenXml.Wordprocessing namespace 來產生對應結構 ,參考下表 :

WordprocessingML 元素 Open XML SDK 2.0 類別 描述
document Document 主文件宣告
body Body 本文的內容
p Paragraph 一個段落
r Run 一群屬性相同文字範圍
t Text 文字內容

 

 

三、程式撰寫

接著透過 OpenXML SDK 2.0 產生檔名為 text.docx 的 Word 文件,文件內容如下所示 :

image

請參考程式碼與註解說明,並配合先前 WordprocessingML 元素結構。

            // 檔案路徑檔名、請注意副檔名與 WordprocessingDocumentType.Document 一致
            string filepath = @"C:\test.docx";

            // 建立 WordprocessingDocument 類別,透過 WordprocessingDocument 類別中的 Create 方法建立 Word 文件
            using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(filepath, WordprocessingDocumentType.Document))
            {
                // 建立 MainDocumentPart 類別物件 mainPart,加入主文件部分 
                MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
                // 實例化 Document(w) 部分
                mainPart.Document = new Document();
                // 建立 Body 類別物件,於加入 Doucment(w) 中加入 Body 內文
                Body body = mainPart.Document.AppendChild(new Body());
                // 建立 Paragraph 類別物件,於 Body 本文中加入段落 Paragraph(p)
                Paragraph paragraph = body.AppendChild(new Paragraph());
                // 建立 Run 類別物件,於 段落 Paragraph(p) 中加入文字屬性 Run(r) 範圍
                Run run = paragraph.AppendChild(new Run());
                // 在文字屬性 Run(r) 範圍中加入文字內容
                run.AppendChild(new Text("在 body 本文內容產生 text 文字"));
            }

 

 

四、結語

本文說明使用 OpenXML SDK 2.0 產生 Word 文件檔案,並針對 WordprocessingML 結構進行說明,了解這些知識對於之後編輯 Word 文件與程式除錯會有幫助。

 

 

五、相關參考

MSDN - How to: Create a Word Processing Document by Providing a Filename

MSDN - WordprocessingML 文件的組織結構

X's Blog - WORD 2007 WordprocessingML (Part 1)