+ Start a Discussion

Export a custom contacts view to Excel

Is it possible to export a custom contact view to Excel?  I have a button that goes to the ExportContacts.page that is defined like:



<apex:page standardController="Contact" contenttype="application/vnd.ms-excel#ExportContacts.xls" 
recordSetVar="contacts" extensions="ExportContactsExtension" >
<apex:pageBlock title="Contacts">
<apex:pageBlockTable value="{!contacts}" var="contact">
<apex:column value="{!contact.LastName}"/>
<apex:column value="{!contact.FirstName}"/>
<apex:column value="{!contact.Name}"/>
<apex:column value="{!contact.MailingCity}"/>
<apex:column value="{!contact.Phone}"/>
<apex:column value="{!contact.Fax}"/>
<apex:column value="{!contact.MobilePhone}"/>



The ExportContactsExtension.cls is defined like:


public class ExportContactsExtension {

    public ExportContactsExtension(ApexPages.StandardSetController controller) {
    	//set the page size so all records will be exported


The question is can I export the specific fields specified in the contacts view?  On the ExportContacts.page, I have to define the fields to export, like last name, first name, etc.  Now if I create a new contacts view and add say the email address, I'll see it on the page, but if I click the export button, it doesn't include the email address.  Can I make that export dynamic to include all of the values from the current view?



You can use contentType="application/vnd.ms-excel#Contacts.xls" as a attribute in <apex:page> to get the contactview in excel.


Hope this helps.


I've updated the page - I did have the contenttype set on the page, but I didn't copy it when posting the question.  The issue is not how to export it to Excel - I can export the specified fields with no issues.  The question is how can I export all of the fields specified in the view without changing the ExportContacts.page.  Currently, the exported fields are hardcoded on that page.  Is there a way to make that dynamic so it will always export the fields in the current view?