0

I have a visualforce page in which there is a button called download.
If I click that button then it will download all the account records as text file. For this use case how can I implement.
Do not redirect to another visualforce page
Can someone suggest some ideas to implement?

My vfp code:

<apex:page standardController="Account" extensions="AccountDownload" recordSetVar="accounts" sidebar="false">    
    <apex:form >    
        <apex:pageBlock title="Archiver For Account">   
        <apex:pageBlockSection >                
            <apex:commandButton action="{!Download}" value="download" id="theButton" /><br/>    
             <apex:outputPanel id="panel1" rendered="{!ren}">   
                 <apex:pageBlockTable value="{!accounts}" var="ac">    
                     <apex:column value="{!ac.Name}"/>   
                     <apex:column value="{!ac.AccountNumber}"/>  
                     <apex:column value="{!ac.AnnualRevenue}"/>   
                     <apex:column value="{!ac.Phone}"/>   
                     <apex:column value="{!ac.Type}"/>       
                 </apex:pageBlockTable>                  
             </apex:outputPanel>   
        </apex:pageBlockSection>   
        </apex:pageBlock>           
    </apex:form>     
</apex:page>    

Controller:

   public class AccountDownload {    
    public boolean ren{get;set;}
    public AccountDownload(ApexPages.StandardSetController controller) {
        ren=false;
    }
    public PageReference Download(){
        ren=true;
        return null;

    }
}
1

You may use the renderAs property of page to set it as "PDF" sothat your page will be rendered as a PDF. another way is to define the contenttype of your page as MS word. so after clicking on "Download" button, the page content will be downloaded as word file.

  • I want to download particular section only – user3332076 Apr 2 '15 at 8:39
  • I want to download that outpanel content only – user3332076 Apr 2 '15 at 9:30
  • 1
    Visualforce does not allow you to download a particular part of page... you probably you will need to create a new page with particular section, set contenttype of new page as word, and navigate to new page on click of download button. – Anup Apr 3 '15 at 17:51
1

You would need to create another VF page with contentType="text/csv#filename.csv".

On clicking the download button, from the method you can return a page reference to this new page.

<apex:page controller="AccountDownload" contentType="text/csv#filename.csv" >

<apex:repeat value="{!accounts}" var="acc">

{!acc.Name},{!acc.AccountNumber},{!acc.AnnualRevenue},{!acc.Phone}, {!acc.Type},

</apex:repeat>
</apex:page>

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.