views:

95

answers:

3

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" 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}"/>
    </apex:pageBlockTable>
  </apex:pageBlock>
</apex:page>

The ExportContactsExtension.cls is defined like:

public class ExportContactsExtension {

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

}

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?

A: 

Why exactly you need such view in first place? Is there some complex logic in controller for selecting the records?

If the query for them can be written in standard SOQL (like [SELECT FirstName, LastName, MailingCity, Phone, Fax, MobilePhone FROM Contact]), then it might be much more effective for you to export records with Data Loader or Excel plugin called "Excel Connector".

And if you need to be able to both export and (pre)view the data on the Salesforce page - consider creating a report? That's what they are for and there's built-in functionality to export to Excel or CSV.

eyescream
The view is the standard Contacts view - it's not a custom controller. It doesn't include any custom SOQL to retrieve the information.
Tai Squared
A: 

You can export your results to Excel by using the contentType attribute on the apex:page component. See the following post:

Visualforce Export to Excel / IE Bug

HTH

Jeff Douglas

Appirio, Inc.

jeffdonthemic
I updated the ExportContacts.page to include the contenttype - I had that in my page, but took it out to easily see the results without launching Excel each time and didn't paste it into this question. I can export the specified fields to Excel, but the question still remains as to the best way to export a new field like email address. Can I do that dynamically on the ExportContacts.page, or do I have to hardcode the fields that will be exported and update them if a new field has to be exported?
Tai Squared
A: 

Unfortunately what your asking is not possible with apex today. To do this you'd need a way to query what the page layout was for the record being view and then inspect which fields are visible.

Ralph Callaway