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
bujjibujji 

Export All StandarSetController Data to Excel

Hi Guys,

I am displaying data using StandardSetController and pagination, but when i want to export the data, it displaying only current page data but i want all data to be excel.How to achieve it.

Below is the code i am using to display to data.

public ApexPages.StandardSetController contoller{get; set;}

controller = new ApexPages.StandardSetController(Database.getQueryLocator(finalQuery));
               
 controller.setPageSize(200);

           public Boolean hasNext
            {
               get
               {
                     return controller.getHasNext();
               }
              set;
            }  
        
            public Boolean hasPrevious
            {
               get
               {
                   return controller.getHasPrevious();
               }
               set;
            }
Suggest me any idea how to achieve it.

Thanks,
Bujji

AshwaniAshwani
There is attribute in apex:page tag "contentType"

<apex:page standardController="Account" contentType="application/vnd.ms-excel">
Rajendra ORajendra O
As per your configured controller it's returning limited data, so only that will be included in your ecport file. So export all data you can use readonly attribute of apex:page, which will relax data row limitation (in case data is huge) then in related controller you can query data manually and bind the list to data table.

Please refer :- http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_annotation_ReadOnly.htm
http://www.salesforce.com/us/developer/docs/pages/index_Left.htm#StartTopic=Content/pages_controller_readonly_context.htm
bujjibujji
Hi Ranjendra,

Thanks for the reply, if the data is 5000 records and i displayed than it will be tough for the user to scroll down, that's why i am giving pagination. I have custom link on clicking it will generate the report. Is there any way that when i click on the custom link to export the all data in to excel.

Thanks,
Bujji
Deepak Kumar ShyoranDeepak Kumar Shyoran
Use a different Collection other then controller which will hold the List<SObject> and contains all record for a Specific SObject.
As currently you set the size to 200 that's why you will be able to download only 200 records.

public ApexPages.StandardSetController contoller{get; set;}
public List<SObject> allRecord  { get;set;}

public constructer() {
allRecord = new List<SObject>() ;
controller = new ApexPages.StandardSetController(Database.getQueryLocator(finalQuery));
allRecord = controller ;

}

bujjibujji
Deepak,

Thanks for reply, we cannot assign directly StandardSetController to the List<Sobject>, even though  how can you export on clicking of custom link. Can you explain detail.

Thanks,
Bujji.
Rajendra ORajendra O
Bujji, the suggest approch is only to export the data, not to display.
I mean keep using the current solution to display records but to export data, create a custom button/link on UI which will be linked to new VF page designed as I suggested above.
 

Export.page
<apex:page contentType="application/vnd.ms-excel#nameofdownloadedfile.xls" language="en-US" cache="false" readonly="true" sidebar="false" showHeader="false"   controller="ExportController" >
<!--@todo display list of contact/sobject here -->
</apex:page>
Related sample controller :-
ExportController.cls
public with sharing ExportController(){
	public transient list<contact> contacts {get; set;}
	public ExportController(){
	    contacts = [select name, email from contact];
	}
}

So when you click on button/link you will call Export.page, which will dump all data to excel. Let us know if it's not what you are looking for.