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. 转:【微信小程序】实现锚点定位楼层跳跃的实例

    微信小程序实现楼层锚点跳跃,点击不同的锚点进行位置跳跃:  利用:scroll-into-view 来实现: 效果图:   wxml: <scroll-view class="cont ...

  2. POS配置

    1.配置 (1) 房产局 (2) 银联端 2.运行 (1)   房产端 右击BizMAPSSP项目---Run As—run congfigurations—Java Application—选择Bi ...

  3. 转 Android开发学习笔记:浅谈WebView

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://liangruijun.blog.51cto.com/3061169/647456 ...

  4. ItelliJ基于Gradle创建及发布Web项目(一)

    背景:安装IntelliJ,去官网下载. 创建WEB项目 1. File->New Project,在弹出的选项框中勾选Web,如下图. IntelliJ默认使用Gradle,感谢Gradle. ...

  5. 最短作业优先(SJF)

    1. 最短作业优先: 最短作业优先(SJF)是一种调度任务请求的调度策略.每个任务请求包含有请求时间(即向系统提交的请求的时间)和持续时间(即完成任务所需时间). 当前任务完成后,SJF策略会选择最短 ...

  6. Django Rest Framework(分页、视图、路由、渲染器)

    一.分页 试问如果当数据量特别大的时候,你是怎么解决分页的? 方式a.记录当前访问页数的数据id 方式b.最多显示120页等 方式c.只显示上一页,下一页,不让选择页码,对页码进行加密 1.基于lim ...

  7. angularJs 页面定时刷新

    angularJs 页面定时刷新 页面定时刷新并在页面离开时停止自动刷新 var autoRefresh; //自动刷新 autoRefresh = $interval($scope.loadData ...

  8. filebeat+kafka失败

    filebeat端配置 #----------------------------- Kafka output -------------------------------- output.kafk ...

  9. python正则表达式匹配时间和IP地址

    t = '19:16:30' mt = re.match(r'^(0[0-9]|1[0-9]|2[0-3]|[0-9])\:(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[ ...

  10. 删除节点removeChild()

    http://www.imooc.com/code/1700 删除节点removeChild() removeChild() 方法从子节点列表中删除某个节点.如删除成功,此方法可返回被删除的节点,如失 ...