This simple program demostrate how to create well formatted MS Word documents using C#, 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 C# project as the Project Type and Windows Application as the Template. For the project name, specify 'CShartXSLT'; 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.cs, add the following references
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.IO;
6. In Form1.cs, add a button and write the following code
private void button1_Click(object sender, EventArgs e)
{
DataSet ds;
XmlDataDocument xmlDoc;
XslCompiledTransform xslTran;
XmlElement root;
XPathNavigator nav;
XmlTextWriter writer;
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"))
{
File.Delete("Employee.rtf");
}
//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 = null;
nav = null;
root = null;
xmlDoc = null;
ds = null;
MessageBox.Show("Document created successfully.....");
}
catch (Exception ex)
{
writer = null;
nav = null;
root = null;
xmlDoc = null;
ds = null;
MessageBox.Show(ex.StackTrace);
}
}
7. Compile and run the program.
Vallikannu APosted Dec 11, 2015, 5:17 AM
Hi how can i add header and footer in the word document with XML and XSLT
DANNY ROUGHeditedPosted May 20, 2013, 12:26 AMEdited May 20, 2013, 12:27 AM
thanks for sharing. that's great
P JPosted Nov 22, 2011, 2:02 PM
There are much better solutions like FlexDocs and Docentric.
just tshaPosted Jul 15, 2011, 6:01 AM
Hi!, how can I automatically Save the created Word file to a Folder maybe in a server
emrah caparPosted Jan 31, 2010, 6:55 PM
Thank You I Love C-sharpCorner
jem murilloPosted Oct 11, 2009, 8:00 AM
hi,, i'm having a problem on making a program that will make an output into a word document, and the input data will be in a textfile or xml file.
Bilal WaniPosted Sep 16, 2009, 6:00 AM
Hello, Good example and very very Useful.. Thanks a lot for Posting
hoang nhuteditedPosted Feb 18, 2009, 5:48 AMEdited Feb 18, 2009, 5:55 AM
Thanks for your article.Sudipto But I have a problem now. I design the Word template and attach xml schema to it and save to Xml file. After that I generate the xsl file from this file. Unfortunately, the richtext field after Transform can not display correctly My application have a richtext field and it have RTF format when save to database. So I consider that how do you create the xslt file? Please help me. Thanks.
Brian QuinnPosted Feb 6, 2009, 4:18 PM
Sudipta, You posting here is greatly appreciated. I am generating resumes from my IT contracting job tracker web application. I've got the HMTL transform working but needed a way to transform to MS Word. You have also inormed me that I should look at RTF on MSDN and learn a little. This will get me where I need to go faster. Thanks from an old legacy dinosaur learning .NET web apps.
Vitalii SymonPosted Dec 23, 2008, 6:54 PM
Use pure word libraries like aspose.net. As for me - i'd used invoke docx lib to work out the task described in the article. it is much easier because you have the template already and no need to work with messy xml. You can apply styles, add custom text right in word (but no need to have word on server). see http://invoke.co.nz/products/docx.aspx and sample at http://invoke.co.nz/products/help/docx_tables.aspx
Sundar Rajan 0Posted Nov 21, 2007, 7:18 AM
Hi, I just went through your sample program which create a word doc using XSLT by c# code. The same thing i want to do in my application but i am restricted not to use any .net langs, i can use Java Script instead. If you have any suggestion please let me know, it will be helpful for me.
Avijit RoyPosted Mar 30, 2007, 6:10 AM
Hi Sudipto, I'm creating a word document and geting the printout from c#.net. Could u plz tell me what should I do if word is not installed in server (from where my project is running). Thanks Avijit [email protected]
Prasad ReddyPosted Oct 23, 2006, 3:46 AM
Hi i am facing one problem. I am trying to convert xml file into rtf using xmlprecompiledtransfer class. But it is not avail in the 1.1. Can u please help me in this issue. Kind Regards, Prasad Reddy