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 l开发者_运维百科ast 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?
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.
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.
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
精彩评论