function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
venk1255venk1255 

How to download the data from visual force page

 I have a visual force page,

 

I created a serach button in visual force page,the serach button return some records into pageblock table,i want to display CSV link to download the serch results any one can please suggest me on this 

 

it would be great helpfull.

 

Thanks In Adavace

Best Answer chosen by Admin (Salesforce Developers) 
SFDC_VikashSFDC_Vikash

Hi

 

I am posting a sample code for you, you can modify it as per your requirment.

 

<apex:page standardController="Contact" contentType="{!IF(isExport = true, 'application/vnd.ms-excel#report.xls', '')}" Extensions="DependentPicklistExt" sidebar="{!!isExport}" showHeader="{!!isExport}" standardStylesheets="false">  
  <apex:form id="frm">
<apex:pageBlock >
  <apex:outputPanel rendered="{!(isExport = false)}">
    <apex:inputText value="{!selectedAcc}"/>
    <apex:commandButton value="Search" action="{!search}" reRender="resTable"/>
  </apex:outputPanel>
    <apex:outputPanel id="resTable">
      <apex:commandLink action="{!export}" value="Export" rendered="{!AND(isExport = false, lstAccounts.size != 0)}" reRender=""/>
      <apex:pageBlockTable value="{!lstAccounts}" var="acc" rendered="{!(lstAccounts.size != 0)}">
        <apex:column value="{!acc.Id}"/>
        <apex:column value="{!acc.Name}"/>
      </apex:pageBlockTable>
    </apex:outputPanel>
  </apex:pageBlock>
  </apex:form>
</apex:page>

 

public class DependentPicklistExt {
   public String selectedAcc {get;set;}
   public List<Account> lstAccounts {get;set;}
   public boolean isExport {get;set;}

public DependentPicklistExt(ApexPages.StandardController controller) {
      con = (Contact)controller.getRecord();
      if(Apexpages.currentPage().getParameters().get('Export') != null){
        isExport = true;
      }
      else{
        isExport = false;
      }
      lstAccounts = new List<Account>();
}

public PageReference search(){
      lstAccounts = new List<Account>();
      lstAccounts = [SELECT Id, Name, Phone FROM Account WHERE Name = :selectedAcc];
      return null;
    }
    
   public PageReference export(){
    isExport = true;
      return null;
    }

}

     

 

All Answers

Sridhar BonagiriSridhar Bonagiri

Hi,

 

You can export the search results to Excel.

 

Regards,

Sridhar Bonagiri

If this post is your solution, kindly mark this as the solution and give Kudos.

 

venk1255venk1255

Thanks for your Reply Sreedhar,

 

i want to display a Excel format link in my page when i click on that link the results should get download,

 

Thanks In Advance

 

 

 

SFDC_VikashSFDC_Vikash

Hi

 

I am posting a sample code for you, you can modify it as per your requirment.

 

<apex:page standardController="Contact" contentType="{!IF(isExport = true, 'application/vnd.ms-excel#report.xls', '')}" Extensions="DependentPicklistExt" sidebar="{!!isExport}" showHeader="{!!isExport}" standardStylesheets="false">  
  <apex:form id="frm">
<apex:pageBlock >
  <apex:outputPanel rendered="{!(isExport = false)}">
    <apex:inputText value="{!selectedAcc}"/>
    <apex:commandButton value="Search" action="{!search}" reRender="resTable"/>
  </apex:outputPanel>
    <apex:outputPanel id="resTable">
      <apex:commandLink action="{!export}" value="Export" rendered="{!AND(isExport = false, lstAccounts.size != 0)}" reRender=""/>
      <apex:pageBlockTable value="{!lstAccounts}" var="acc" rendered="{!(lstAccounts.size != 0)}">
        <apex:column value="{!acc.Id}"/>
        <apex:column value="{!acc.Name}"/>
      </apex:pageBlockTable>
    </apex:outputPanel>
  </apex:pageBlock>
  </apex:form>
</apex:page>

 

public class DependentPicklistExt {
   public String selectedAcc {get;set;}
   public List<Account> lstAccounts {get;set;}
   public boolean isExport {get;set;}

public DependentPicklistExt(ApexPages.StandardController controller) {
      con = (Contact)controller.getRecord();
      if(Apexpages.currentPage().getParameters().get('Export') != null){
        isExport = true;
      }
      else{
        isExport = false;
      }
      lstAccounts = new List<Account>();
}

public PageReference search(){
      lstAccounts = new List<Account>();
      lstAccounts = [SELECT Id, Name, Phone FROM Account WHERE Name = :selectedAcc];
      return null;
    }
    
   public PageReference export(){
    isExport = true;
      return null;
    }

}

     

 

This was selected as the best answer
venk1255venk1255
Thank you so much Vikash,

Its working Fine.
SFDC_VikashSFDC_Vikash

Hi,

 

Your welcome.