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’s a simplified version of the example shown in Building a Table of Data in a Page.
<apex:page standardController="Account"> <!-- This page must be accessed with an Account Id in the URL. For example: https://<salesforceInstance>/apex/myPage?id=001D000000JRBet --> <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>
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 doesn’t display properly in Excel, try a different MIME type, such as text/csv.