Friday, April 18, 2014

How to Generate MS-Word document using VF pages?

How to Generate MS-Word document using VF pages?



I have a VF which renders as MS-Word. for this you need to mention content type in <apex:page> tag. 

EX:-

<apex:page standardController="Account"  contentType="application/msword#sfdcsrini.doc" cache="true">

<html xmlns:w="urn:schemas-microsoft-com:office:word">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<body>
<apex:outputText title="Welcome to word" value="{!$User.FirstName}"/>
<br/>
<br/>
<apex:pageBlock >
<div style="text-align:left" >
<b>Account Name :</b><apex:outputText value="{!account.name}"/><br/>
<b>Account Number: </b><apex:outputText value="{!account.AccountNumber}"/><br/>
</div>            
</apex:pageBlock>
</body>
</html>


</apex:page>


once you save this code in to your vf page you need to pass the account id in to url like

https://c.ap1.visual.force.com/apex/worddocvf?id=0019000000RMAaF


 here contentType="application/msword#sfdcsrini.doc" tells that page should render as ms-word document.(sfdcsrini) is the name of the document in content type, you can give your own name. 




1 comments:

Post a Comment

 
| ,