Using Content Type

You can specify a different format for a Visualforce page by using the ContentType attribute on the <apex:page> tag.

The ContentType attribute takes a Multipurpose Internet Mail Extension (MIME) media type as a value, such as application/vnd.ms-works, audio/mpeg or image/gif. For more information about valid MIME media types, see http://www.iana.org/assignments/media-types/.

Microsoft Excel ContentType Example

To display Visualforce page data in a Microsoft Excel spreadsheet, use the contentType attribute on the <apex:page> tag, and specify a value of application/vnd.ms-excel.

For example, the following page builds a simple list of contacts. It is a simplified version of the example shown in Building a Table of Data in a Page:

<apex:page standardController="Account">
   <apex:pageBlock title="Contacts">
      <apex:pageBlockTable value="{!account.Contacts}" var="contact">
         <apex:column value="{!contact.Name}"/>
         <apex:column value="{!contact.MailingCity}"/>
         <apex:column value="{!contact.Phone}"/>
      </apex:pageBlockTable>
   </apex:pageBlock>
</apex:page>
Note
Remember, for this page to display account data, the ID of a valid account record must be specified as a query parameter in the URL for the page. For example,
https://Salesforce.com_instance/apex/myPage?id=001x000xxx3Jsxb
Displaying Field Values with Visualforce has more information about retrieving the ID of a record.

To display this page in Excel, add the contentType attribute to the <apex:page> tag, as follows:

<apex:page standardController="Account" contenttype="application/vnd.ms-excel">
   <apex:pageBlock title="Contacts">
      <apex:pageBlockTable value="{!account.Contacts}" var="contact">
         <apex:column value="{!contact.Name}"/>
         <apex:column value="{!contact.MailingCity}"/>
         <apex:column value="{!contact.Phone}"/>
      </apex:pageBlockTable>
   </apex:pageBlock>
   </apex:page>

If the page does not display properly in Excel, try a different content-type, such as text/rtf.

© Copyright 2000-2010 salesforce.com, inc. All rights reserved.
Various trademarks held by their respective owners.