Problem Statement
I want to export list of object into CSV file using Apex

Solution

<apex:page controller="csvController" cache="true" 
contentType="text/csv#filename.csv" language="en-US">
"Col A","Col B","Col C","Col D"
<apex:repeat value="{!myList}" var="a">
"{!a.ColA}","{!a.ColB}","{!a.ColC}","{!a.ColD}"
</apex:repeat>
</apex:page>

Please note that

  • With the #filename.csv, it prompts the user to save/open the file. Otherwise, it loads into the browser as html
  • You’ll also have to format the VF page as a clean csv text file, that means no pageblocktables or anything that might insert html formatting.
  • if customer want to export data object to text file use contentType=”text/plain”

Source : Salesforce Community