You probably know that a Visualforce Page can be easily converted to a PDF (if not, check out Quote2PDF), but did know Visualforce can also generate a Microsoft Excel Worksheet?
By simply modifying the ContentType attribute on the <apex:page> tag, your Visualforce code will automatically generate an Excel document. For example, the following code will create a table of Contact data for a given Account:
<apex:page standardController="Account">
<apex:pageBlock title="Hello {!$User.FirstName}!">
You are viewing the {!account.name} account.
</apex:pageBlock>
<apex:pageBlock title="Contacts">
<apex:pageBlockTable value="{!account.Contacts}" var="contact">
<apex:column value="{!contact.Name}"/>
<apex:column value="{!contact.Email}"/>
<apex:column value="{!contact.Phone}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>
<apex:pageBlock title="Hello {!$User.FirstName}!">
You are viewing the {!account.name} account.
</apex:pageBlock>
<apex:pageBlock title="Contacts">
<apex:pageBlockTable value="{!account.Contacts}" var="contact">
<apex:column value="{!contact.Name}"/>
<apex:column value="{!contact.Email}"/>
<apex:column value="{!contact.Phone}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>
If an ID of a valid Account record is specified as a query parameter in the URL for the page, you will see the following table of data:
We can tell Visualforce to convert this page to an Excel doc by adding:
<apex:page standardController="Account" contenttype="application/vnd.ms-excel">
The next time the page is loaded, you will receive the following prompt:
This is a powerful and easy-to-use feature. You can learn more about ContentType in the documentation.
