+ Start a Discussion

Export to a CSV or Excel From a Table in VF Page

Hi All,

We need to Download the data Displayed in a VF Table in a VF page to a CSV or an Excel file on a button Click.

Could someone please suggest a method to get this done.

Thanks in Advance,


Subhranshu MohantySubhranshu Mohanty

<apex:page standardController="Account" contenttype="application/vnd.ms-excel#Demo Example.xls">
<apex:pageBlock title="Hello {!$User.FirstName}!">
You are viewing the {!account.name} account.
<apex:pageBlock title="Contacts">
<apex:pageBlockTable value="{!account.Contacts}" var="contact">
<apex:column value="{!contact.Name}"/>
<apex:column value="{!contact.Email}"/>
<apex:column value="{!contact.Phone}"/>
Chidambar ReddyChidambar Reddy
Hi Christwin,

you can export is as a CSV

create a string in the controller that stores the body of the file
and give header column, values seperated by commas
string body = 'Column1,Column2,Column3'+'\n';

then iterate through data

convert this string as blob at the finish 
and set the blob value as body of the attchment 

Choose it as best answer if it has helped you