RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Generating Microsoft Office Documents with the Open XML SDK : Page 2

The Open XML SDK provides a comprehensive set of classes that make generating and manipulating Microsoft Office documents much simpler and faster than was possible with older Office file formats.


Developing Applications with the Open XML SDK

This article uses the Open XML SDK version 2.0 CTP release. Download and install the SDK using the instructions on the download link page.

Set Up the Environment

After installing the SDK on your development machine, create a new project and add a reference to the SDK DocumentFormat.OpenXml assembly by clicking References Add Reference. Click the .NET tab, and then select the DocumentFormat.OpenXml assembly. If you don't see the assembly in the .NET tab, you can add it manually from your installation folder.

The Open XML SDK uses .NET packaging APIs internally, so you also need to add a reference to System.IO.Packaging.Package.

Finally, add a reference to the WindowsBase assembly.

In your code, you'll want to add these three main namespaces:

  • DocumentFormat.OpenXml: General Open XML related functions.
  • DocumentFormat.OpenXml.Packaging: APIs related to packaging.
  • DocumentFormat.OpenXml.Wordprocessing: APIs for working on Microsoft Word (.docx) documents. The assembly also exposes namespaces for other Office client applications such as Excel and PowerPoint.

Creating a Basic Word Document

To get started, you'll create a basic Word document which will serve to show you the basic infrastructure for Word documents. Remember that an Open XML package is composed of parts (Main Document Part, Image Part, Video Part, Document Properties Part, etc.). Each part is represented by at least one file.

The WordprocessingDocument class represents a Word package. You can use the class to create a new package or open an existing package:

WordprocessingDocument doc = WordprocessingDocument.Create 
   (@"BasicWordDoc.docx", WordprocessingDocumentType.Document);

The Main Document part contains the text of the document—and it's the only required part. You can add a main document part to a package using the following code:

MainDocumentPart mainPart = doc.AddMainDocumentPart();
mainPart.Document = new Document();

The various parts of the document itself are arranged hierarchically as follows:


The SDK exposes classes for each of these components, which makes it simple to generate a Word document programmatically. For example, to create a document containing the text "Hello World," you walk up the hierarchy, creating the appropriate objects at each step, passing each object to the constructor of its parent object:

Text textFirstLine = new Text("Hello World");
Run run = new Run(textFirstLine);
Paragraph para = new Paragraph(run);
Body body = new Body(para);

After creating the object hierarchy, you can append the Body object to the document created earlier:


At this point, you can save the main document part and close the package:


Here's the complete code for a C# console application that generates the "Hello World" example:

static void Main(string[] args)
   /* Create the package and main document part */
   WordprocessingDocument doc = 
      MainDocumentPart mainPart = doc.AddMainDocumentPart();
      mainPart.Document = new Document();
   /* Create the contents */
   Text textFirstLine = new Text("Hello World");
   Run run = new Run(textFirstLine);
   Paragraph para = new Paragraph(run);
   Body body = new Body(para);
   /* Save the results and close */

If you execute the application, it will create a new document. If you open that with Word, you'll see the document shown in Figure 1.

Figure 1. Basic Document: This basic Word document was created programmatically.

As you can see, the process to create a basic document is straightforward; however, most documents are more complicated, containing styled text and other formatting.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date