+ Start a Discussion
Subodh Shukla 16Subodh Shukla 16 

Send Visualforce page data as Excelsheet

Hi all,
My reuirement is that i want to Export all my visualForce page data into Excelsheet when i click to Download as Excel Link on the page. it should create a new vf page and show all related data. 
My VF Page is:

<apex:page sidebar="false" controller="RolesAndUsers" >
  <apex:form >
   Roles:&nbsp;&nbsp;&nbsp;&nbsp;<apex:selectList value="{!SelectedRole}" multiselect="false" size="1">
   <apex:actionSupport event="onchange" reRender="User,Subroles"/>
   <apex:selectOptions value="{!RolesName}">
   </apex:selectOptions>
   </apex:selectList><br></br><br></br><br></br>
   Users in Selected Role:&nbsp;&nbsp;&nbsp; 
   <apex:pageBlock id="User">
    <apex:pageblockTable value="{!UserName}" var="Ur"> 
     <apex:column >
      <apex:OutputField value="{!Ur.Name}"/>
      </apex:column>
    </apex:pageblockTable>
   </apex:pageBlock>
       
   <br></br>
   Sub Roles In Hierarchy:
   <apex:pageBlock id="Subroles" >
   <apex:pageBlockTable value="{!subrole}" var="sr">
     <apex:Column >
      <apex:OutputField Value="{!sr.name}"/>
     </apex:Column>
     <apex:repeat value="{!sr.Users}" var="c">
                <apex:facet name="header">Users</apex:facet>
                <apex:column value="{!c.Name}"/>
                 <apex:facet name="header">Users Email</apex:facet>
                <apex:column value="{!c.Email}"/>
            </apex:repeat>
     
   </apex:pageBlockTable>
  </apex:pageblock>
 
my controller is :

public with sharing class RolesAndUsers{

Public List<User>UserList= new List<User>();
Public String SelectedRole {get;set;}
Public List<UserRole>SelectedUserRole= new List<UserRole>();

Public List<SelectOption>Roleoptions= new List<SelectOption>();
///---------------Method to get all Roles-------------------////
Public List<SelectOption>getRolesName(){

   Roleoptions.add(new selectOption('-None-','-None-'));
    for (UserRole ur1 :[select id,Name from UserRole ])
     {
       
       Roleoptions.add(new selectoption(ur1.name,ur1.name));
       
     }
     return Roleoptions;
  }
 
///--------------Method to get all User Under selected Role-------------------/// 

public List<User>getUserName(){
   UserList.clear();    
           
       
      for(User u1:[Select id, name ,UserRole.name From USer Where UserRole.Name=:SelectedRole and UserRole.name!=null])
      {
        UserList.add(u1);
      
      }
      
return UserList;
}
/////--------------Method to get all Sub Roles------------------////
Public List<UserRole>getSubrole(){
//subroles.clear();
List<UserRole>SelectedUserRole = [select id from UserRole Where UserRole.Name=:SelectedRole];
List<UserRole>Subroles=[SELECT id, Name,(Select name,email From USERS) From UserRole WHERE ParentRoleid IN: SelectedUserRole];
Return Subroles;


}


what i want is whenever i select a role all the Subrole and there User Should be Send as excel in new vf page
Aslam ChaudharyAslam Chaudhary
Subodh Shukla 16Subodh Shukla 16
Hi Aslam,
thanks For reply But i tried that and its not showing the data because its a Dynamic VF page.
Aslam ChaudharyAslam Chaudhary
I did this in past but don’t have code right now. I can provide you a hint. Create two page with same controller name. One page will have the your select option and second page will show the data using contentType="application/vnd.ms-excel#Demo.xls.