Here is the code for exporting leads to excel in salesforce apex
Class Code:
public class exportExcel {
public string header{get;set;}
public List<wrapper> lstwrapper {get; set;}
public class wrapper{
public string Id{get; set;}
public string Name{get; set;}
public string Email{get; set;} }
public string Filetype{get;set;}
public boolean isExcel {get;set;}
public boolean isCsv {get;set;}
public exportExcel(){
Filetype = ”;
lstwrapper = new List<wrapper>();
header = ‘Id,Name,Email’;
}
public void exportToExcel(){
string queryString = ‘Select Id, Name, Email from Lead’;
List<Lead> lstLead = DataBase.Query(queryString);
if(lstLead.size() > 0) {
for(Lead ld : lstLead)
{
wrapper w = new wrapper();
w.Id = ld.Id;
w.Name= ld.Name;
w.Email = ld.Email;
lstwrapper.add(w);
}
}
}
}
Page Code:
<apex:page controller=”exportExcel” action=”{!exportToExcel}” readOnly=”true” contentType=”application/vnd.ms-excel#ConsignmentSearchData.xls”> <apex:pageBlock >
<apex:pageBlockTable value=”{!lstwrapper}” var=”x”>
<apex:column headerValue=”Id”>
{!x.Id}
</apex:column>
<apex:column headerValue=”Name”>
{!x.Name}
</apex:column>
<apex:column headerValue=”Email”>
{!x.Email}
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>