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
shashi kanaparthishashi kanaparthi 

Exporting data to excel on click of a button

Hi,

I have a command button "Export To Excel" on the edit screen. This button when clicked should export the data to excel. For this purpose i created another visualforce page to which the user is redirected after the button is clicked. But when the button is clicked, the user is not redirected to another page and the data is not being downloaded in the excel format. Both the VF Pages share the same controller.


Below is the method for the button and the vf page.

 public PageReference ExportToExcel(){
   
       PageReference exdownload=new PageReference('apex/ExportAsExcel');
        exdownload = Page.ExportAsExcel;
        OpportunityData();
        exdownload.setRedirect(true);
        return exdownload;
       
   
   }


<apex:page controller="ManageListController" id="page" showHeader="false" Sidebar="false" cache="true" expires="1200" contentType="application/vnd.ms-excel#Candidate.xls" > <!-- Schema added to add autofilter for header row in Excel sheet --> <html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"> <head></head> <body > <apex:pageBlock id="results" > <!-- HTML Table used to display Candidate Data in Excel Sheet with Grid Lines --> <table border="1" cellpadding="1" cellspacing="0" style="width:100%;border:1px solid #000000;font-familyans-serif;font-size:10px"> <tr style="font-family:verdana;color:Black;font-size:12px;height=18px;width:100%" > <!-- Autofilter added for header row in Excel sheet --> <td x:autofilter='all' style="background-color: #DAFFFF" ><b> <apex:outputText value="Id" /></b> </td> <td x:autofilter='all' style="background-color: #DAFFFF" ><b><apex:outputText value="Name"/></b> </td> </tr> <apex:repeat value="{!OppList}" var="opp"> <tr border="1" style="font-family:verdana;color:Black;font-size:12px;width:100%" > <td ><apex:outputText value="{!opp.Id}"/></td> <td><apex:outputText value="{!opp.Name}"/></td> </tr> </apex:repeat> </table> <br/> <br/> <br/> <br/> <br/> <apex:outputtext > Confidential Information - Do Not Distribute</apex:outputtext> <br/> <apex:outputtext >Generated By: {!$User.FirstName} {!$User.LastName} {!Now()} </apex:outputtext> <br/> <apex:outputtext value="Your Company" /> </apex:pageBlock> </body > </html> </apex:page>
SalesFORCE_enFORCErSalesFORCE_enFORCEr
Instead of creating another page, you can just set the content type of the same page on click of Export button