-----------------------------------------------------------

New Message on BDOTNET

-----------------------------------------------------------
From: KashiMVP
Message 2 in Discussion

Hi Guys,   Recently I did this, generating word doc in asp.net and data fetched from 
database, the best way is generate rtf file and stream it to word doc, for this you 
dont need server side automation and generating Word documents with ASP server-side 
Automation is a bad idea, coz  Microsoft Word is an out-of-process Automation server; 
out-of-process calls take longer to execute. Microsoft Word has substantial overhead 
because the Winword.exe file must be loaded in order for you to reap the benefits of 
Word's rich object model.   Even the microsoft wont recommend server side automation, 
you can find following message in MSDN itself  Microsoft does not currently recommend, 
and does not support, Automation of Microsoft Office applications from any unattended, 
non-interactive client application or component (including ASP, DCOM, and NT 
Services), because Office may exhibit unstable behavior and/or deadlock when run in 
this environment.   okay... too much story i think.. now let me explain how to 
generate rtf file easily and stream it word doc.   here are the steps for the same 1. 
create template file using MS Word and save it to disk as a RTF file 2. Then open the 
same in notepad 3. Then as usual add XSL:stylesheet and XSL:template in the top and in 
the end to make it XSL file. 4. add <xsl:value of select="your tags"> ..wherever u 
want to get data from XML 5. generate XML file with data from database 6. now u got 
XML and XSL files, so transform it using system.xml.xsl 7. now save the transformed 
output data to a file as a word doc.   XML generation sample  
string xmlData = ""; 
xmlData += "<SOW>"; 
xmlData += "<MetaData>"; 
xmlData += "<ProjectName>" + CleanData(projectName.ToString()) + "</ProjectName>"; 
xmlData += "<CompanyName>" + CleanData(companyName.ToString()) + "</CompanyName>"; 
xmlData += "<ProjectNumber>" + SowID.ToString() + "</ProjectNumber>"; ......    Sample 
XSL  
<?xml version="1.0" encoding="utf-8" ?>  
 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";> 
<xsl:output version="1.0" encoding="iso-8859-1" omit-xml-declaration="yes" /> 
<xsl:template match="/SOW"> 
\par Name:\tab }{\b\f1\fs20\cf6\insrsid4218535}{\b\i\f1\fs20\cf6\insrsid4218535  
<xsl:value-of select="MetaData/Supplier/Name"/>}{\b\f1\fs20\cf6\insrsid4218535 \tab 
\tab \tab }{\f1\fs20\insrsid4218535 \tab \tab Name:\tab 
}{\b\f1\fs20\cf6\insrsid4218535 }{\b\i\f1\fs20\cf6\insrsid4218535  
<xsl:value-of select="MetaData/Customer/Name"/>}{\b\f1\fs20\cf6\insrsid4218535 
}{\f1\fs20\insrsid4218535  
\par Title:\tab }{\b\f1\fs20\cf6\insrsid4218535 }{\b\i\f1\fs20\cf6\insrsid4218535  
<xsl:value-of select="MetaData/Supplier/Title"/>}{\b\f1\fs20\cf6\insrsid4218535 
}{\f1\fs20\insrsid4218535 \tab \tab \tab \tab \tab Title:\tab 
}{\b\f1\fs20\cf6\insrsid4218535 }{\b\i\f1\fs20\cf6\insrsid4218535  
<xsl:value-of select="MetaData/Customer/Title"/>}{ 
.............blah blah blah..... 
\par }} 
</xsl:template> 
</xsl:stylesheet> 
HOW TO TRANSFORM IT 
xDoc.LoadXml(xmlData); 
XPathNavigator xdNav = xDoc.CreateNavigator(); 
XslTransform tr = new XslTransform(); 
tr.Load(Server.MapPath("template.xslt")); 
Response.ClearContent(); 
Response.ClearHeaders(); 
Response.ContentType="text/plain"; 
Response.AddHeader("Content-Disposition", "attachment; filename=SOW.rtf") ; 
tr.Transform(xdNav,null,Response.OutputStream); 
  
okay now u got to know how to do this, but there is one small problem when your data 
is comming as xml , it might have special characters/entities like &  < > " '  
so u need to replace these charcters with rtf/ASCII characters before passing it to 
XML from database 
something like this 
return data.Replace("'",@" \'27 ").Replace("&",@" \'26 ").Replace("<",@" \'3C 
").Replace(">",@" \'3E ").Replace("\"",@" \'22 
").Replace(Environment.NewLine.ToString(),@" \par "); this will be much faster and 
simple too...    voila now your word doc is ready ... :)   Hope it helps   - Kashi  - 
Microsoft MVP   - Wipro Bangalore

-----------------------------------------------------------

To stop getting this e-mail, or change how often it arrives, go to your E-mail 
Settings.
http://groups.msn.com/bdotnet/_emailsettings.msnw

Need help? If you've forgotten your password, please go to Passport Member Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help

For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact

If you do not want to receive future e-mail from this MSN group, or if you received 
this message by mistake, please click the "Remove" link below. On the pre-addressed 
e-mail message that opens, simply click "Send". Your e-mail address will be deleted 
from this group's mailing list.
mailto:[EMAIL PROTECTED]

Reply via email to