Newer Version Available

This content describes an older version of this product. View Latest

Using a Custom ContentType

You can specify a different format for a Visualforce page by using the ContentType attribute on the <apex:page> tag. This sets the Content-Type HTTP header for the response to the value of the page’s ContentType attribute.

The ContentType attribute takes a Multipurpose Internet Mail Extension (MIME) media type as a value, such as application/vnd.ms-excel, text/csv, or image/gif. Browsers can behave unpredictably if you set an invalid ContentType. 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.

1swfobject.registerObject("clippy.codeblock-0", "9");<apex:page standardController="Account">
2    <apex:pageBlock title="Contacts">
3        <apex:pageBlockTable value="{!account.Contacts}" var="contact">
4            <apex:column value="{!contact.Name}"/>
5            <apex:column value="{!contact.MailingCity}"/>
6            <apex:column value="{!contact.Phone}"/>
7        </apex:pageBlockTable>
8    </apex:pageBlock>
9</apex:page>

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:

1https://Salesforce_instance/apex/myPage?id=001x000xxx3Jsxb

Note

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

1swfobject.registerObject("clippy.codeblock-2", "9");<apex:page standardController="Account" contentType="application/vnd.ms-excel">
2    <apex:pageBlock title="Contacts">
3        <apex:pageBlockTable value="{!account.Contacts}" var="contact">
4            <apex:column value="{!contact.Name}"/>
5            <apex:column value="{!contact.MailingCity}"/>
6            <apex:column value="{!contact.Phone}"/>
7        </apex:pageBlockTable>
8    </apex:pageBlock>
9</apex:page>

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