I have a SFDC App which does data (Account, Contact, Lead) segmentation and flags them according to segmentation rules. I want to be able to provide a link on visualforce page to download certain accounts/contacts/leads (according to flag) in csv/excel(whichever is easy to do) format. How to do it?


You can use following for your purpose

<!-- Your Main Page -->
<apex:page controller="DownloadData">
<apex:form >
<apex:commandButton value="DownloadRecords" action="{!records}"/>

<!-- -Your DownloadDemo Page- -->
<apex:page controller="DownloadData"  contentType="application/vnd.ms-excel#FileName.xls" >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:pageBlockTable value="{!data}" var="d">
<apex:column value="{!d.ID}"/>
<apex:column value="{!d.Data__c}"/>

/*****Same Controller for both the classes*****/
 public with sharing class DownloadData {

public PageReference records() 
   return new PageReference('/apex/DownloadDemo');

public List<MyObject__c> getData() {

    List<MyObject__c> mo = [SELECT ID,Data__C FROM MyObject__c];

    return mo;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.