Summary: Learn how to transform XML data into a Word 2007 document by starting with an existing document that has the desired layout. (12 Printed Pages)

Applies to:2007 Microsoft Office system, Microsoft Office Word 2007, Microsoft Visual Studio 2008, Open XML Software Development Kit 2.0 for Microsoft Office

Michael Case, iSoftStone

December 2009

 

You can use XSL Transformations (XSLT) to transform XML data into the Open XML SDK 2.0 for Microsoft Office format that Microsoft Office Word 2007 uses, and create Word 2007 documents from XML data. To make it simpler to transform XML data into a Word 2007 document, start with an existing Word 2007 document that has the desired layout.

This Visual How To shows how to use XSLT to create a Word 2007 document. It shows how to create an XSL Transform file that is based on an existing Word 2007 document. The code sample shows how to use the XSL Transform file to create a Word 2007 document that is based on data that is stored in an XML file.

You must install the Open XML SDK 2.0 for Microsoft Office to follow the steps in this Visual How To.

Watch the Video

Length: 08:09 | Size: 10.06 MB | Type: WMV file

Code It | Read It | Explore It

Code It

 

Download the Sample Code

This Visual How To shows how to create a Windows C# console application that takes an XML data file, an XSL Transform file, and a Word 2007 document template, and then uses XSLT to create a well- formatted Word 2007 document that contains the data from the XML data file.

To illustrate how to create the solution described earlier, this section walks through the following steps:

  1. Creating an XML data file.

  2. Creating a Word 2007 template document that has the desired layout.

  3. Extracting the contents of the main document part of the Word 2007 template document.

  4. Creating an XSL Transform file that is based on the extracted content.

  5. Adding Transforms to the XSL Transform file.

  6. Creating a Windows console application solution in Visual Studio 2008.

  7. Adding references to the required assemblies to the Visual Studio solution.

  8. Adding code to the Visual Studio solution that creates a new Word 2007 document that uses the XML data and XSL Transform files.

Creating an XML Data File

The sample code for this Visual How To specifies that a personal movie library XML data file named MyMovies.xml exists in the C:\Temp directory.

To create the MyMovies.xml data source

  1. Start Visual Studio 2008.

  2. From the File menu, point to New and then click File.

  3. In the New File dialog box, select XML File.

  4. Copy the following XML example into the new file.

     
    <?xml version="1.0" encoding="UTF-8"?>
    <Movies>
    <Genre name="Action">
    <Movie>
    <Name>Crash</Name>
    <Released>2005</Released>
    </Movie>
    </Genre>
    <Genre name="Drama">
    <Movie>
    <Name>The Departed</Name>
    <Released>2006</Released>
    </Movie>
    <Movie>
    <Name>The Pursuit of Happyness</Name>
    <Released>2006</Released>
    </Movie>
    </Genre>
    <Genre name="Comedy">
    <Movie>
    <Name>The Bucket List</Name>
    <Released>2007</Released>
    </Movie>
    </Genre>
    </Movies>
  5. Save the document as C:\Temp\MyMovies.xml.

Creating a Word 2007 Template Document

The sample code uses an existing Word 2007 document to help simplify the creation of the XSL Transform file. The code specifies that a Word 2007 document named MyMoviesTemplate.docx is in the C:\Temp directory.

To create the MyMoviesTemplate.docx document

  1. Start Word 2007.

  2. Add text to the document and placeholders for the XML data following the layout in the example. The placeholders in the document shown in this section are Genre Name, Movie Title, and year. When the code performs the XSL transform, these are replaced with data that is pulled from the XML data file that you created earlier.

    Figure 1. Word 2007 Template Document

  3. Save the document as C:\Temp\MyMoviesTemplate.docx.

Extracting the Contents of the Main Document Part of the Template Document

To simplify the creation of the XSL Transform file, use the contents of the Word 2007 template document that you created in the previous step as a starting point. Word 2007 documents use the Open Packaging Conventions (OPC). This means that Word documents are really .zip files that contain XML, binary, and other kinds of files. By adding the .zip extension to the end of the Word 2007 document name, you can then use tools such as WinZip or Windows Explorer to examine and extract the contents of the document.

To create the XSL Transform file, the Word 2007 template document that you created earlier is temporarily renamed with a .zip extension. The document.xml file is then extracted from the template document to the C:\Temp directory, renamed to MyMovies.xslt and converted from XML to XSLT.

To extract the contents of the main document part of the template document

  1. Start Windows Explorer and navigate to the folder that contains the MyMoviesTemplate.docx file that you created earlier.

  2. Rename MyMoviesTemplate.docx to MyMoviesTemplate.docx.zip to gain access to the underlying Open XML files.

  3. Use Windows Explorer to explore the contents of the MyMoviesTemplate.docx ZIP file. Navigate to the Word folder in the .zip file and copy the document.xml file to the C:\Temp folder and rename the file to MyMovies.xslt.

    Figure 2. Copying the Document.XML File

  4. Rename MyMoviesTemplate.docx.zip back to MyMoviesTemplate.docx.

Creating an XSL Transform File Based on the Extracted Content

The next step is to convert the MyMovies.xslt file to an XSL Transform file.

To create the MyMovies.xslt XSL transform file

  1. In Visual Studio 2008, open the MyMovies.xslt that you created in the previous step.

  2. Do the following to convert the document structure of MyMovies.xslt from XML to XSLT:

    Remove the following line from the top of the document. 
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>

    Add the following line to the top of the document. 
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    Close the style sheet element by adding the following line to the very end of the document. 
    </xsl:stylesheet>

  3. Add an <xsl:template> element around the existing <w:document> element.

     
    <xsl:template match="/">
    <w:document ...>
    <w:body>
    ...
    </w:body>
    </w:document>
    </xsl:template>
  4. Save the changes to the MyMovies.xslt file.

Adding Transforms to the XSL Transform File

In the next step, you add XSL Transform elements to list the name of each genre and information for each movie within the genre in the XML data file. This transform uses the xsl:value-of and xsl:for-each elements.

  • Use the xsl:value-of elements to replace the Genre Name, Movie Title, and (year) placeholders in the Word 2007 template document.

  • Use two xsl:for-each elements to list each genre and its movies. When you place the xsl:for-each elements, make sure that you include all Open XML elements related to the genre or movie text you repeat to ensure that valid Open XML format is output by the transformation.

The following example is a fragment from the contents of the original document.xml. It shows the placeholder text from the template document.

 
...
<w:p w:rsidR="00EC137C" w:rsidRPr="00BF ...
<w:pPr>
<w:pStyle w:val="Heading2"/>
</w:pPr>
<w:r w:rsidRPr="00BF350E">
<w:t>Genre Name</w:t>
</w:r>
</w:p>
<w:p w:rsidR="00EC137C" w:rsidRPr="00EC1 ...
<w:pPr>
<w:pStyle w:val="ListParagraph"/>
<w:numPr>
<w:ilvl w:val="0"/>
<w:numId w:val="1"/>
</w:numPr>
</w:pPr>
<w:r w:rsidRPr="00BF350E">
<w:rPr>
<w:b/>
</w:rPr>
<w:t>Movie Title</w:t>
</w:r>
<w:r w:rsidR="00C46B60">
<w:t xml:space="preserve"> (year)</w:t>
</w:r>
</w:p>
...

The following example is an XSLT fragment of the modified MyMovies.xslt file. It shows the changes that were made to include the XSL Transform elements.

 
 
<!-- for-each loop added for Genre.  This loop includes the Open XML elements for the paragraph the Genre placeholder is in and all paragraphs for the Movies. -->
<xsl:for-each select="Movies/Genre">
<w:p w:rsidR="00EC137C" w:rsidRPr="00BF ...
<w:pPr>
<w:pStyle w:val="Heading2"/>
</w:pPr>
<w:r w:rsidRPr="00BF350E">
<w:t>
<!-- Genre Name placeholder replaced by the Genre's Name attribute in the XML data file. -->
<xsl:value-of select="@name"/>
</w:t>
</<xsl:value-of select w:r>
</w:p>
<!-- for-each loop added for Movie. This loop includes the Open XML elements that define the paragraph as a bulleted list. -->
<xsl:for-each select="Movie">
<w:p w:rsidR="00EC137C" w:rsidRPr="00EC1 ...
<w:pPr>
<w:pStyle w:val="ListParagraph"/>
<w:numPr>
<w:ilvl w:val="0"/>
<w:numId w:val="1"/>
</w:numPr>
</w:pPr>
<w:r w:rsidRPr="00BF350E">
<w:rPr>
<w:b/>
</w:rPr>
<w:t>
<!-- Movie Title placeholder replaced by the Movie's Name element in the XML data file. -->
<xsl:value-of select="Name"/>
</w:t>
</w:r>
<w:r w:rsidR="00C46B60">
<!-- Year placeholder replaced by the Movie's Released element in the XML data file. -->
<w:t xml:space="preserve"> (<xsl:value-of select="Released"/>)
</w:t>
</w:r>
</w:p>
</xsl:for-each>
</xsl:for-each>
...

Be sure that you save the modified MyMovies.xslt file after you make any changes.

Creating a Console Application in Visual Studio 2008

This Visual How To uses a Windows console application as the framework for the sample code. The console application type was selected only for its simplicity. Other application types could use the same approach presented here.

To create a Windows console application in Visual Studio 2008

  1. Start Microsoft Visual Studio 2008.

  2. From the File menu, point to New and then click Project.

  3. In the New Project dialog box select the Visual C# Windows type in the Project types pane.

  4. In the Templates pane, select Console Application.

  5. Name the project and solution XSLTWordDocument.

    Figure 3. Creating the Solution

  6. Click OK to create the solution.

Adding References to Required Assemblies

This Visual How To uses the strongly typed classes that are provided with the Open XML SDK 2.0 to access content in a Word 2007 document. You must install the Open XML SDK 2.0 before completing the following steps.

To add a Reference to the OpenXml Assemblies

  1. From the Visual Studio Project menu, click Add Reference.

  2. On the .NET tab, select the DocumentFormat.OpenXml assembly and then click OK to add the reference.

  3. On the .NET tab, add a reference to the WindowsBase assembly.

Adding the Sample Code to the Solution

Replace the contents of the Program.cs source file with the following example.

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using System.IO;
using System.Xml;
using System.Xml.Xsl; using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing; namespace XSLTWordDocument
{
class Program
{
/// <summary>
/// This application uses the xslt file created from the
/// original Word 2007 template document to transform xml data
/// into a valid Open XML 2.0 Wordprocessing format.
/// The application then updates the output document with the
/// new content using the Open XML SDK version 2.0.
/// </summary>
static void Main(string[] args)
{
//Declare variables for file locations.
string rootPath = @"C:\Temp";
string xmlDataFile = rootPath + @"\MyMovies.xml";
string xsltFile = rootPath + @"\MyMovies.xslt";
string templateDocument = rootPath + @"\MyMoviesTemplate.docx";
string outputDocument = rootPath + @"\MyMovies.docx"; //Create a writer for the output of the Xsl Transformation.
StringWriter stringWriter = new StringWriter();
XmlWriter xmlWriter = XmlWriter.Create(stringWriter); //Create the Xsl Transformation object.
XslCompiledTransform transform = new XslCompiledTransform();
transform.Load(xsltFile); //Transform the xml data into Open XML 2.0 Wordprocessing format.
transform.Transform(xmlDataFile, xmlWriter); //Create an Xml Document of the new content.
XmlDocument newWordContent = new XmlDocument();
newWordContent.LoadXml(stringWriter.ToString()); //Copy the Word 2007 source document to the output file.
System.IO.File.Copy(templateDocument, outputDocument, true); //Use the Open XML SDK version 2.0 to open the output
// document in edit mode.
using (WordprocessingDocument output =
WordprocessingDocument.Open(outputDocument, true))
{
//Using the body element within the new content XmlDocument
// create a new Open Xml Body object.
Body updatedBodyContent =
new Body(newWordContent.DocumentElement.InnerXml); //Replace the existing Document Body with the new content.
output.MainDocumentPart.Document.Body = updatedBodyContent; //Save the updated output document.
output.MainDocumentPart.Document.Save();
}
}
}
}

The sample code shown here takes the Word 2007 template document, the XSL Transform file that was created from the Word 2007 template document, and the XML data file, and then creates a new Word 2007 document that contains the XML data. The solution uses the XSL Transform file to transform the XML data into the Open XML format. The Open XML SDK 2.0 is then used to create a new Word 2007 document that contains the new content.

Figure 4. Word 2007 Output Document

Read It

 

Starting with a Word 2007 document that already contains the layout makes it easy for you to create a Word 2007 document that contains XML data. Once you create the Word 2007 document, you can extract the contents and XSLT elements that you added to replace values or to repeat information.

The sample code in this Visual How To transforms XML data into the Open XML Wordprocessing format by using an XSL Transform file that was created from a template Word 2007 document. The Open XML SDK 2.0 is then used to create a new Word 2007 document that has content based on the XML data.

The following example shows the transformation of XML data into the Open XML Wordprocessing format. The code uses anXslCompiledTransform object to load the XSLT and calls its Transform method to transform the XML data. The results of the transformation are then loaded into an XmlDocument object.

 
  //Create a writer for the output of the Xsl Transformation.
StringWriter stringWriter = new StringWriter();
XmlWriter xmlWriter = XmlWriter.Create(stringWriter); //Create the Xsl Transformation object.
XslCompiledTransform transform = new XslCompiledTransform();
transform.Load(xsltFile); //Transform the xml data into Open XML 2.0 Wordprocessing format.
transform.Transform(xmlDataFile, xmlWriter); //Create an Xml Document of the new content.
XmlDocument newWordContent = new XmlDocument();
newWordContent.LoadXml(stringWriter.ToString());

The following example shows the creation of the output Word 2007 document and then opening it for editing by using the Open XML SDK 2.0. First the template document is copied to a new file. Then, the WordprocessingDocument class is used to open the document in edit mode.

 
  //Copy the Word 2007 template document to the output file.
System.IO.File.Copy(templateDocument, outputDocument, true); //Use the Open XML SDK version 2.0 to open the output
// document in edit mode.
using (WordprocessingDocument output =
WordprocessingDocument.Open(outputDocument, true))
{

The following example shows the use of the Open XML SDK 2.0 to modify the output Word 2007 document. It uses a Body object to update the Word 2007 document's content to the results of the transformation shown previously. The root element of the transformation results is aw:document element, which contains the w:body element that is needed to create a Body object. To create the Body object the w:body element of the transformation is used.

 
    //Using the body element within the new content XmlDocument
// create a new Open Xml Body object.
Body updatedBodyContent =
new Body(newWordContent.DocumentElement.InnerXml); //Replace the existing Document Body with the new content.
output.MainDocumentPart.Document.Body = updatedBodyContent; //Save the updated output document.
output.MainDocumentPart.Document.Save();

Using XSLT and Open XML to Create a Word 2007 Document的更多相关文章

  1. Embedding Documents in Word 2007 by Using the Open XML SDK 2.0 for Microsoft Office

    Download the sample code This visual how-to article presents a solution that creates a Word 2007 doc ...

  2. User Word Automation Services and Open XML SDK to generate word files in SharePoint2010

    SharePoint 2010 has established a new service called "Word Automation Services" to operate ...

  3. C# 读写xml、excel、word、ppt、access

    C# 读写xml.excel.word.access 这里只是起个头,不做深入展开,方便以后用到参考 读写xml,主要使用.net  的xml下的document using System;using ...

  4. asp.net导出excel-一行代码实现excel、xml、pdf、word、html、csv等7种格式文件导出功能而且美观-SNF快速开发平台

    分享: 腾讯微博  新浪微博   搜狐微博   网易微博  腾讯朋友  百度贴吧  豆瓣   QQ好友  人人网 作者:王春天  原文地址:http://www.cnblogs.com/spring_ ...

  5. XML Parser Errors See Details for more Information XML Parser Error on line 1: Document root ele

    1.错误描写叙述 XML Parser Errors See Details for more Information XML Parser Error on line 1: Document roo ...

  6. C# : 操作Word文件的API - (将C# source中的xml注释转换成word文档)

    这篇博客将要讨论的是关于: 如何从C#的source以及注释, 生成一份Word格式的关于各个类,函数以及成员变量的说明文档. 他的大背景如下...... 最近的一个项目使用C#, 分N个模块, 在项 ...

  7. java对word文档的操作(提取标题和内容等)-直接操作或poi工具包或freemarker+xml或html转word

    1,java自带工具包实现对word的排版和写入 import java.awt.Color; import java.io.FileNotFoundException; import java.io ...

  8. Freemarker + xml 实现Java导出word

    前言 最近做了一个调查问卷导出的功能,需求是将维护的题目,答案,导出成word,参考了几种方案之后,选择功能强大的freemarker+固定格式之后的wordxml实现导出功能.导出word的代码是可 ...

  9. Adding Form Fields to a MS Word Document

    Configuring a Word Merge in SmartSimple is a three-step process: Create the MS Word document that wi ...

随机推荐

  1. 【LeetCode】68. Text Justification

    Text Justification Given an array of words and a length L, format the text such that each line has e ...

  2. 【LeetCode】96. Unique Binary Search Trees (2 solutions)

    Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...

  3. HTML5 学习笔记 表单属性

    HTML5新的表单属性 HTML5 的form和input 标签添加了几个新的属性 <form>新属性 autocomplete novalidate input 新属性 autocomp ...

  4. C# AutoResetEvent

    文章转载自:C# AutoResetEvent AutoResetEvent 常常被用来在两个线程之间进行信号发送 AutoResetEvent是.net线程简易同步方法中的一种,两个线程共享相同的A ...

  5. blender, 同时选择多个顶点

    法1:按MRB(鼠标右键)选中第一个顶点,再按shift+MRB依次选择其余顶点. 法2:按B,光标变为横纵两条虚线,此时可按MLB(鼠标左键)框选顶点.按MRB结束框选. 法3:按C,光标变为虚线圆 ...

  6. sqlzoo练习答案--SUM and COUNT

    World Country Profile: Aggregate functions This tutorial is about aggregate functions such as COUNT, ...

  7. shell脚本之函数的使用

    把代码封装成函数,相当于造了一个“轮子”,之后就直接重复使用即可. 函数的创建 shell中函数的创建有2种方式 1.使用function关键字 语法 function test { ... } 2. ...

  8. 2、Reactive Extensions for .NET(译)

    实验3-引入 .net 中的 events 到 Rx 目标:前面实验中的使用各种工厂构造方法创建一个 可观察序列是一个部分.把 .net 中现有的异步数据源进行关联 是更重要的事情.在这次实验中我们将 ...

  9. SpringBoot 整合 Security5

    https://my.oschina.net/yunduansing/blog/2032475 https://blog.csdn.net/SWPU_Lipan/article/details/805 ...

  10. 用广搜实现的spfa

    用广搜实现的spfa,如果是用一般的最短路,会发现构图很麻烦,因为它不是路径带权值,而是自身带权值.写起来只要注意,在点出队列的生活将其标记为0,在要压入队列的时候,判断其标记是否为0,为0表示队列中 ...