Open XML: Use a template file

Within a previous article I have shown you a “Hello World” solution to create an Open XML file. Based on this article I will show you an adapted version of this application which uses an existing file. So you can prepare a file with all the styles and formats you need.

 
Copy the template file

At first I have created an empty word file with header and footer. You may also create a template file with the styles and formats of your needs.

Within the demo application I want to copy this file first to create the target file. So you can reuse the template. The following source code contains the commands to copy the template file.

//file name
string folder = @"<your folder>";
string targetFileName = folder + @"\Test.docx";
string templateFileNameTemplate = folder + @"\Template.docx";

//copy template file
if (File.Exists(targetFileName) == true)
{
    File.Delete(targetFileName);
}

File.Copy(templateFileNameTemplate, targetFileName);

 
Open file and add content

At next you have to open the new file and add your content. Like shown in the previous article, you have to create your text element, a run element and a paragraph. Then you can add you content to the existing file and save the changes. The following source code shows the according implementation.

//open file
using (var file = WordprocessingDocument.Open(targetFileName, true))
{
    //define content
    var text = new Text("Hello Open XML world");
    var run = new Run(text);
    var paragraph = new Paragraph(run);

    //save
    file.MainDocumentPart.Document.Body.AppendChild(paragraph);
    file.MainDocumentPart.Document.Save();
}

 
Example application

The following code shows the complete example, implemented as console application.

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

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

            //copy template file
            if (File.Exists(targetFileName) == true)
            {
                File.Delete(targetFileName);
            }

            File.Copy(templateFileNameTemplate, targetFileName);

            //open file
            using (var file = WordprocessingDocument.Open(targetFileName, true))
            {
                //define content
                var text = new Text("Hello Open XML world");
                var run = new Run(text);
                var paragraph = new Paragraph(run);

                //save
                file.MainDocumentPart.Document.Body.AppendChild(paragraph);
                file.MainDocumentPart.Document.Save();
            }
        }
    }
}
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