Download Visualforce Page Content in Excel File

Requirement: Click on output link download the visual force page content


  Visualforce Page Code

<apex:page controller="ClassController" sidebar="false" contentType="{!strContentType}">
       <apex:form >  
        <apex:pageBlock id="pgBlock">
           <apex:outputPanel rendered="{!flag}">
               <apex:outputPanel id="OutPanelID" rendered="{!isDownloadPanel}">
                 <apex:commandLink action="{!ExportData}" hreflang="apex/VFPageName" value="Download Report" style="margin-left: 560px;color:blue;"/>
               </apex:outputPanel>          
           <table  border="1">                        
                <tr style='background-color:silver;font-weight:bold;'>
                    <td align="center" width="260px"><b>Column Name</b></td>
                </tr>             
                <apex:repeat value="{!lstName}" var="x">
                    <tr>  
                        <td align="left">{!x.FieldName}</td>                      
                 </apex:repeat>                          
                    </tr>              
            </table>
            </apex:outputPanel>
           </apex:pageBlock>  
        </apex:form> 
</apex:page>


Class Code:

public class ClassController 
{
    
    public boolean isDownloadPanel{get;set;}
    public String strContentType {get; set;}
    public Boolean flag {get; set;}
    
    public ClassControllerController()
    {
         isDownloadPanel =true;
          flag = false;
       
    }
    
     public void ExportData()
     {
          isDownloadPanel = false;
          strContentType = 'application/vnd.ms-excel#NameofExcelSheet.xls'; 
     }                  
               
}


In case of any issue please feel free call to me @9711055997 
Drop a mail : sumitshukla.mca@gmail.com
Skype ID: shuklasumit1


Comments

Popular posts from this blog

Salesforce Spring 16 Release Exam (Maintenance Exam Q&A) for Developer 201 Admin

Show Hyper Link On Add Error in Salesforce Trigger

Show the Success Message before Redirecting the Detail page on Visualforce Page