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

Visual force and excel



I have a custom solution which displays records based on the criteria in the form of a data table.

Now, how can i export the results in Visualforce page to excel?


I would have a link/button that reads "Export to Excel" and should export the results.


Any ideas on how to accomplish this?





I created a second visualforce page with


contentType="application/"  cache="true"

in the opening <apex:page> tag.


I also removed all content from this page that is not related to the excel version (ie javascript) as I called the page using paramateres in the url to pull the data needed.


When creating the second page how would i just extract the results without the input for second page?

can you walk me through the steps?










everything related to the product thats been selected.


Now in my second page how would i just get the results? I hope i am clear in my explanation.





You wuld have to add a section to your controller to


1. test for the presence of a paramater in the URL of the VF page

2. If it exists, use that parameter in the query for the records returned



Say you are returning results for a specific account


1. The VF page would display the results is say a pabgeblocktable with a {!getAccounts} as the value

2. In the controller you would have to


Set variable to hold value: String accIDstring;

Set var to set the accID as ID: ID accID;

In the init get the parameter: 


if(ApexPages.CurrentPage().getparameters().get('accID')!= Null) {

     accID = ApexPages.CurrentPage().getparameters().get('accID');



Where you get the records:


ID = (id)accIDstring;


then use the accID in the query. This will also mean you have to resolve how to set the accID initially if you are not running the NON excel page using the parameter. (I set it based on user input (I am using a year)


You would also have a method in your controller:


public PageReference createExcel(){

         return new Pagereference('/apex/YOURVFPAGENAME?accID=' + accIDstring);


So basically when the VF (non excel) runs it sets the value of accIDstring or you can set it in the controller where you test for accIDstring == NULL.


Then it pulls the data for the VF page (NonExcel).


The button on the VF page say "Create Excel" would call the createExcel(0 method and use the value to open the VF Excel page and set the parameter in the url. Since it is not null, it will use that value to populate the data.


You can shorten this to use the existing ID of any record that you get from running the standard VF page. Then all you would have to do is use that value for the parameter and test for it in the controller.....


Hope I did not make it too confusing. The first way is more confusing because I was doing this based on user input and not from a particular ?id= from a standard page. If you can use that ID then it is simpler.

HI Starz26

Thanks for the solution its working is it possible to display excel image instead of download link?