Hello Open XML world

The Open XML SDK allows you to create Office documents without the need of an installed Office application. Since 2007 the Open XML file format is used in Office products. This format is very flexible and powerful. But unfortunately this also results in a lot work if you want to know each detail about the format. The according documentation ECMA-376 contains several thousand pages.

But luckily you can use a nice SDK to work with Open XML files. So you don’t have to understand the file format in each technical detail. You will find the Open XML SDK as well as the Open XML Productivity tool in the Microsoft development center. The productivity tool facilitates the work with the SDK.

Within this article I want to show you the classical “Hello World” example. This should help you getting started with Open XML implementation. Within further articles I will also show you some other common use cases for the work with Open XML files.

 
Set up the project

As described above, you have to download and install the Open XML SDK first. Then you can create a new project, for example a console application. To use Open XML you have to add the following references to your project:

DocumentFormat.OpenXml
WindowsBase

 
Structure of an Open XML document

An Open XML document may contain a lot of different content information, so called stories. Only one of these different content types is required. All other are optional. So for the “Hello world” example we have to add the required story, the main document story.

The document story itself contains tree like content. The document element is the root element and contains a body element. This body element is a container and contains a collection of block-level structures. So it may contain different paragraphs.

A paragraph is the basic block-oriented element in Word. All text content within a document is contained within paragraphs. The different content elements within the paragraph are represented by a run element. A run element is the basic container for a document’s content. Therefore different content elements which belong together and should for example have the same style or format should be placed in one run. So a run may contain different elements like text, images, tabs, footnotes fields, etc.

In summary this sounds like you have to create a difficult structure. But if you work a while with Open XML documents you will see that this concept is logical.

 
Hello World example

If we want to create a word document with the text “Hello World”, we have to put the text into the structure described above. So we have to create the following structure:

Document -> Body -> Paragraph -> Run -> Text

 
The following source code shows the according implementation.

var text = new Text("Hello Open XML world");
var run = new Run(text);
var paragraph = new Paragraph(run);
var body = new Body(paragraph);
var document = new Document(body);

 

Of course, we want to write the content into a file. To do this you have to create a new file, create the main document story, set the story content and save the file. The following source code shows an according example. (“fileName” and “document” are variables, see the final complete example)

using (var file = WordprocessingDocument.Create(
    fileName, WordprocessingDocumentType.Document))
{
    file.AddMainDocumentPart();
    file.MainDocumentPart.Document = document;
    file.MainDocumentPart.Document.Save();
}

 

So let’s combine the two source code snippets in one final example application.

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

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            //file name
            string folder = @"<your folder>";
            string fileName = folder + @"\Test.docx";

                        //define content
            var text = new Text("Hello Open XML world");
            var run = new Run(text);
            var paragraph = new Paragraph(run);
            var body = new Body(paragraph);
            var document = new Document(body);

            //create file
            using (var file = WordprocessingDocument.Create(
                fileName, WordprocessingDocumentType.Document))
            {
                file.AddMainDocumentPart();
                file.MainDocumentPart.Document = document;
                file.MainDocumentPart.Document.Save();
            }
        }
    }
}

 
Summary

The Open XML file format is a very flexible but also complex format. But with the Microsoft Open XML SDK you get an easy to use tool to work with such files. With a basic knowledge about the main structure of these files you are able to create, read and change files without the need to understand the full Open XML specification.

Advertisements
Dieser Beitrag wurde unter .NET, C#, Open XML veröffentlicht. Setze ein Lesezeichen auf den Permalink.

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s