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
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}">
   Users in Selected Role:&nbsp;&nbsp;&nbsp; 
   <apex:pageBlock id="User">
    <apex:pageblockTable value="{!UserName}" var="Ur"> 
     <apex:column >
      <apex:OutputField value="{!Ur.Name}"/>
   Sub Roles In Hierarchy:
   <apex:pageBlock id="Subroles" >
   <apex:pageBlockTable value="{!subrole}" var="sr">
     <apex:Column >
      <apex:OutputField Value="{!}"/>
     <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}"/>
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(,;
     return Roleoptions;
///--------------Method to get all User Under selected Role-------------------/// 

public List<User>getUserName(){
      for(User u1:[Select id, name , From USer Where UserRole.Name=:SelectedRole and!=null])
return UserList;
/////--------------Method to get all Sub Roles------------------////
Public List<UserRole>getSubrole(){
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/