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
uat_live1.3903047664538198E12uat_live1.3903047664538198E12 

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,

Regards,
Christwin

 
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>
<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}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>
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
for(i){
body+='valueiForColumn1,valueifroColumn2,valueiforcolumn3'+'\n';
}

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

Thanks
Choose it as best answer if it has helped you