Change the MIME type of Your Visualforce Page

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

The contentType attribute takes a Multipurpose Internet Mail Extension (MIME) 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 information about valid MIME types, see http://www.iana.org/assignments/media-types/.

The contentType attribute accepts any MIME type as a valid value. However, Visualforce supports content conversion of only PDFs, which you can do by specifying a renderAs attribute.

Visualforce doesn’t generate other file formats. It only sets the Content-Type field of the HTTP response header to the specified MIME type. Some file formats, such as .xlsx, can fail to render.

Note

  • For example, to display Visualforce page data in a Microsoft Excel spreadsheet, use the contentType attribute on the <apex:page> tag. Specify a value of application/vnd.ms-excel.

    This Visualforce page builds a list of cases by using static HTML and the <apex:repeat> component.

    1<!-- This page must be accessed with an Account Id in the URL. For example: 
    2https://MyDomainName--c.vf.force.com/apex/myPage?id=001D000000JRBet -->
    3
    4<apex:page standardController="Account" contentType="application/vnd.ms-excel">
    5    <table border="0" >
    6        <caption>Cases</caption>
    7        <tr>
    8            <th>Case Number</th>
    9            <th>Origin</th>
    10            <th>Creator Email</th>
    11            <th>Status</th>
    12        </tr>
    13        <apex:repeat var="cases" value="{!Account.Cases}">
    14            <tr>
    15                <td>{!cases.CaseNumber}</td>
    16                <td>{!cases.Origin}</td>
    17                <td>{!cases.Contact.email}</td>
    18                <td>{!cases.Status}</td>
    19            </tr>
    20        </apex:repeat>
    21    </table>
    22</apex:page>

    If the page doesn’t display properly in Excel, try a different MIME type, such as text/csv.

    Tip