ARTICLE

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

Posted by Sudipta Sankar Das Articles | Office and VB.NET November 10, 2006
This simple program demostrate how to create well formatted MS Word documents using VB.Net, XML and XSLT. Using XSLT to create Word documents requires the knowledge of RTF key words.
Download Files:
 
Reader Level:

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>

 

4. 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> 

5. 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

6. 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

 

7. Compile and run the program.

 

NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON C# CORNER (http://www.c-sharpcorner.com/).

Login to add your contents and source code to this article
share this article :
post comment
 

thank you for sharing :)

Posted by Popi Popio Sep 14, 2010

hi sir i want to generet invoice in word file pls help me out
than you !

Posted by deepak bhogre Mar 05, 2010

hi, how to print lines and images at required coordinates in word document. and fill some values in word document for required font and design..... plzzzzzzzzzz help urgently required

Posted by Sudheendra Desai Apr 16, 2007
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Diagram
Become a Sponsor