I have a apex page with 3 tabs. In one of these tabs, I have a data table which I would like to export to csv using a command button. How can I achieve this?

3 Answers 3


I think thats what you are looking for http://salesforceapexcodecorner.blogspot.in/2012/03/export-in-csv-and-excel-in-apex.html


What Sagar posted was a good start, however you'll hit soql governer limits if you have a csv greater than 50,000 results . You want to do basically what was posted, but do it as a scheduled apex job.. This way you can get all the results.

One more thing, after the user clicks the button, redirecect them to a page which says thier request is being processed, and it will take time depending on the query, the number or rows it returns, and the limitations of Salesforce. Most export requests should be done within 10 minutes, but i'd communicate 30, because it's really out of anyone's control.

The csv can be emailed to the user, easily following this

  • I went through the link posted by Sagar. My question here is that I wont the action to trigger when the user clicks a command button and not when the page loads. Because the table is created after selection of multiple dropdown values. How can i enable this?
    – Robin
    Jan 25, 2014 at 15:46

I am posting this as answer as i dont have rights( that needs 50 reps) to comment.

Please consider this as comment

You can remove the action="{!exportToExcel}" from the page.

<apex:page controller="exportExcel" action="{!exportToExcel}" readOnly="true"     contentType="application/vnd.ms-excel#ConsignmentSearchData.xls">

Replace with

<apex:page controller="exportExcel"  readOnly="true" contentType="application/vnd.ms-excel#ConsignmentSearchData.xls">

This will prevent the method to trigger on page load.

Then you can use a button to trigger this method exportToExcel()

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .