Creating MS Word Document using VB.Net, XML and XSLT

2008-04-29


This simple program demostrate how to create well formatted MS Word documents using VB.Net, XML and XSLT. Using XSLT to create Word document requires the knowledge of RTF key words. RTF specification is available in MSDN site.

  1. Open Visual Studio .NET and select File --> New --> Project.

  2. Select Visual Basic project as the Project Type and Windows Application as the Template. For the project name, specify 'VBXSLT'; for the path specify the location where you want the project to be created. Click OK to create the new project.

  3. Add an XML file named "Employee.xml" in \Debug\Bin directory (other wise you have to specify the path in the program). The structure of the file will be like:

<?xml version="1.0" encoding="utf-8" ?>

<Employee>

<Record>

<EmpID>E1</EmpID>

<EmpName>Sudipta</EmpName>

<EmpAge>29</EmpAge>

<EmpSex>M</EmpSex>

<EmpAddress>Kolkata</EmpAddress>

<Department>

<DeptID>D1</DeptID>

<EmpID>E1</EmpID>

<DeptName>Sales</DeptName>

</Department>

</Record>

<Record>

<EmpID>E2</EmpID>

<EmpName>Chiranjib</EmpName>

<EmpAge>26</EmpAge>

<EmpSex>M</EmpSex>

<EmpAddress>Kolkata</EmpAddress>

<Department>

<DeptID>D1</DeptID>

<EmpID>E2</EmpID>

<DeptName>Sales</DeptName>

</Department>

</Record>

<Record>

<EmpID>E3</EmpID>

<EmpName>Nilanjan</EmpName>

<EmpAge>29</EmpAge>

<EmpSex>M</EmpSex>

<EmpAddress>Kolkata</EmpAddress>

<Department>

<DeptID>D2</DeptID>

<EmpID>E3</EmpID>

<DeptName>Finance</DeptName>

</Department>

</Record>

<Record>

<EmpID>E4</EmpID>

<EmpName>Chayan</EmpName>

<EmpAge>30</EmpAge>

<EmpSex>M</EmpSex>

<EmpAddress>Kolkata</EmpAddress>

<Department>

<DeptID>D3</DeptID>

<EmpID>E4</EmpID>

<DeptName>Human Resource</DeptName>

</Department>

</Record>

<Record>

<EmpID>E5</EmpID>

<EmpName>Biplab</EmpName>

<EmpAge>31</EmpAge>

<EmpSex>M</EmpSex>

<EmpAddress>Kolkata</EmpAddress>

<Department>

<DeptID>D4</DeptID>

<EmpID>E5</EmpID>

<DeptName>Administration</DeptName>

</Department>

</Record>

</Employee>

  1. Add an XSLT file named "Employee.xslt" in \Debug\Bin directory (other wise you have to specify the path in the program). The structure of the file will be like,

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:dt="urn:schemas-microsoft-com:datatypes" xmlns:user="urn:my-scripts">

<xsl:output method="text" />

<xsl:template match="Employee">

<xsl:text>{\rtf1\fs22</xsl:text>

<!--Print the Header Row-->

<xsl:text>\trowd\cellx2500\cellx3000\cellx3800\cellx6800\cellx9144\intbl\keepn\ql\b Name\cell Age\cell Sex\cell Address\cell Department\cell</xsl:text>

<xsl:text>\b0\ql0\intbl\row</xsl:text>

<!--Print Employee Records-->

<xsl:apply-templates select="Record" />

<xsl:text>}</xsl:text>

</xsl:template>

<xsl:template match="Record">

<xsl:text>\trowd\cellx2500\cellx3000\cellx3800\cellx6800\cellx9144\intbl\keepn\ql </xsl:text>

<xsl:value-of select="EmpName" />

<xsl:text>\cell </xsl:text>

<xsl:value-of select="EmpAge" />

<xsl:text>\cell </xsl:text>

<xsl:if test="EmpSex='M'">

<xsl:text>Male</xsl:text>

<xsl:text>\cell </xsl:text>

</xsl:if>

<xsl:if test="EmpSex='F'">

<xsl:text>Female</xsl:text>

<xsl:text>\cell </xsl:text>

</xsl:if>

<xsl:value-of select="EmpAddress" />

<xsl:text>\cell </xsl:text>

<!--Print Employee Department-->

<xsl:apply-templates select="Department" />

<xsl:text>\cell</xsl:text>

<xsl:text>\intbl\row</xsl:text>

</xsl:template>

<xsl:template match="Department">

<xsl:value-of select="DeptName" />

</xsl:template>

</xsl:stylesheet>

  1. In Form1.vb, add the following references

Imports System Imports System.Collections.Generic Imports System.ComponentModel Imports System.Data Imports System.Drawing Imports System.Text Imports System.Windows.Forms Imports System.Xml Imports System.Xml.Xsl Imports System.Xml.XPath Imports System.IO

  1. In Form1.cs, add a button and write the following code

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim ds As DataSet

Dim xmlDoc As XmlDataDocument

Dim xslTran As XslCompiledTransform

Dim root As XmlElement

Dim nav As XPathNavigator

Dim writer As XmlTextWriter

Try

'Create the DataSet from the XML file

ds = New DataSet()

ds.ReadXml("Employee.xml")

'Create the XML from the DataSet

xmlDoc = New XmlDataDocument(ds)

'Load the XSLT for Transformation

xslTran = New XslCompiledTransform()

xslTran.Load("Employee.xslt")

'Determine the Root object in the XML

root = xmlDoc.DocumentElement

'Create the XPath Navigator to navigate throuth the XML

nav = root.CreateNavigator()

'First delete the RTF, if already exist

If File.Exists("Employee.rtf") Then

File.Delete("Employee.rtf")

End If

'Create the RTF by Transforming the XML and XSLT

writer = New XmlTextWriter("Employee.rtf", System.Text.Encoding.Default)

xslTran.Transform(nav, writer)

'Close the Writer after Transformation

writer.Close()

'Release all objects

writer = Nothing

nav = Nothing

root = Nothing

xmlDoc = Nothing

ds = Nothing

MessageBox.Show("Document created successfully.....")

Catch ex As Exception

writer = Nothing

nav = Nothing

root = Nothing

xmlDoc = Nothing

ds = Nothing

MessageBox.Show(ex.StackTrace)

End Try

End Sub

  1. Compile and run the program.

http://www.vbdotnetheaven.com/UploadFile/sudiptasdas/WordVBXMLXSLT11092006060002AM/WordVBXMLXSLT.aspx