Export to Excel using VisualForce Page

Export to Excel using VisualForce Page

By simply modifying the ContentType attribute on the <apex:page> tag, your Visualforce page code will automatically generate an Excel document. For example, the following code will create a table of Contact data for a given Account. After # in the contentType attribute value, it is the name of the excel file that will be generated and you also need to mention the file extension like .xls . For successfully running the below code you need to provide the valid account id in URL. Below is the link for Demo:

Click for Demo

Visualforce Code:

<apex:page standardController="Account" contenttype="application/vnd.ms-excel#Demo Example.xls">
<apex:pageBlock title="Hello {!$User.FirstName}!">
You are viewing the {!account.name} account.
<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}"/>

Permanent link to this article: https://www.sfdcpoint.com/salesforce/export-excel-using-visualforce-page/

1 comment

    • priya on October 29, 2014 at 7:11 pm
    • Reply

    Hi… when we click on the click it takes to next page… but i want to stay on same page with the data entered can you share the controller for this?

Leave a Reply

Your email address will not be published.