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. 转:教会你如何编写makefile文件

    最近一直在学习makefile是如何编写的.当我们写的程序文件比较少的时候,敲入gcc /g++,当你在大型工程中,在一个个编译文件的话,你可能就会很郁闷.linux有一个自带的make命令,它让你的 ...

  2. Android开发之定位系统

    2013-07-04 定位系统 全球定位系统(Global Positioning System, GPS), 又称全球卫星定位系统. 最少只需其中3颗卫星,就能迅速确定用户组地球所处的位置及海拔高度 ...

  3. 【centos6.5】CentOS 6.5系统安装配置LAMP(Apache+PHP5+MySQL)服务器环境(linux公社)

    准备篇: 1.配置防火墙,开启80端口.3306端口vi /etc/sysconfig/iptables-A INPUT -m state --state NEW -m tcp -p tcp --dp ...

  4. js特效,轻松实现内容的无缝平滑滚动

    <!DOCTYPE   html   PUBLIC   "-//W3C//DTD   XHTML   1.0   Transitional//EN"   "http ...

  5. C# 图片转换为base64

    WebRequest webRequest = WebRequest.Create("验证码url"); WebResponse webResponse = webRequest. ...

  6. C# 项目发布到IIS后不能用log4net写日志

    在代码中正确配置了log4net后,IIS上仍然不能写日志的情况下,只需在写日志的目录添加 IIS_IUSRS 用户,并赋与读写权限即可.

  7. POJ3187 Backward Digit Sums 【暴搜】

    Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4487   Accepted: 25 ...

  8. webapi应用架构详解

    webapi适用场景 常见的应用包括以下四类,PC客户端程序,APP程序,网站程序,H5程序.这些应用需要的数据,服务可由同一个接口服务程序提供,这样,大大提高了产品多平台设计开发的效率,避免了重复的 ...

  9. 【C语言】18-变量类型

    一.变量的作用域 C语言根据变量作用域的不同,将变量分为局部变量和全局变量. 1.局部变量 1> 定义:在函数内部定义的变量,称为局部变量.形式参数也属于局部变量. 2> 作用域:局部变量 ...

  10. Acquiring Heap Dumps

      Acquiring Heap Dumps HPROF Binary Heap Dumps Get Heap Dump on an OutOfMemoryError One can get a HP ...