Christian Nagel's OneNotes

.NET Training, Consulting, Coaching - C#, Web Services, Enterprise Services, ASP.NET, Whidbey, Longhorn and More!

Sponsors

Affiliations

Books I've written

INETA UG Leaders

My Blogroll

Creating Word Documents with XSLT (Part 2 - Creating Tables)

With my last blog about creating Word documents with XSLT I've shown an example to do a very simple document.

Here I'm showing how to create a table using Word. The same XML file as before is used:

<?xml version="1.0" encoding="utf-8" ?>
<Courses>
 <Course Number="MS-2524">
  <Title>XML Web Services Programming</Title>
 </Course>
 <Course Number="MS-2124">
  <Title>C# Programming</Title>
 </Course>
 <Course Number="NET2">
  <Title>.NET 2.0 Early Adapter</Title>
 </Course>
</Courses>

The XSLT file to create a Word document with a table:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">
 <xsl:output method="xml" indent="yes" />
 <xsl:template match="/">
  <xsl:processing-instruction name="mso-application">
   <xsl:text>progid="Word.Document"</xsl:text>
  </xsl:processing-instruction>
  <w:wordDocument>
   <w:body>
    <w:tbl>
     <w:tblPr>
      <w:tblStyle w:val="TableGrid"/>
      <w:tblW w:w="0" w:type="auto"/>
      <w:tblLook w:val="01E0"/>
     </w:tblPr>
     <w:tblGrid>
      <w:gridCol w:w="4428"/>
      <w:gridCol w:w="4428"/>
     </w:tblGrid>

     <w:tr>
      <w:tc>

       <w:p>
        <w:r>
         <w:t>Number</w:t>
        </w:r>
       </w:p>
      </w:tc>
      <w:tc>

       <w:p>
        <w:r>
         <w:t>Course Title</w:t>
        </w:r>
       </w:p>
      </w:tc>
     </w:tr>

     <xsl:apply-templates select="Courses/Course" />
    </w:tbl>
   </w:body>
  </w:wordDocument>
 </xsl:template>
 <xsl:template match="Course">
  <w:tr>
   <w:tc>
    <w:p>
     <w:r>
      <w:t>
       <xsl:value-of select="@Number" />
      </w:t>
     </w:r>
    </w:p>
   </w:tc>
   <w:tc>
    <w:p>
     <w:r>
      <w:t>
       <xsl:value-of select="Title"/>
      </w:t>
     </w:r>
    </w:p>
   </w:tc>
  </w:tr>
 </xsl:template>
</xsl:stylesheet>

With WordML these are some elements that make up a table:

<tbl> <tbl> represents a table. This is similar to HTML <TABLE>. A WordML <table> element exists, too. However, the <table> element is used for a Office Data Source Object. 
<tblPr> With the element <tblPr> table-wide properties such as the style and the width are defined.
<tblGrid> The element <tblGrid> defines the grid layout of the table. In the example two columns with similar width are defined.
<tr> <tr> is the row of a table
<tc> <tc> is a column inside the row

This helps creating Word documents with WordML:

  • Create a document with Microsoft Word and save it as XML file. Check the XML source.
  • Using Visual Studio 2005, add the Word schemas to the XML file for Intellisense-support.
  • Check the documentation of the Office schemas.

The Office schemas (including documentation) can be found on the MSDN Website.

Christian

Comments

TrackBack said:

Randektív informatikai szakmai blogajánlónak lenni... hátha összejön
# October 2, 2004 5:54 PM

TrackBack said:

# October 17, 2004 11:52 AM

TrackBack said:

# October 18, 2004 12:08 PM

Wazz said:

How to use this files in asp.net application ..I want to use this XSLT files in aspnet application ..

# July 24, 2007 2:39 AM

Mielz said:

damn, that is a lot of xML to get a simple table

# January 30, 2008 12:57 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)