Hope everyone is staying safe.
In this Blog, we are going to learn- How to render Visual Force Page in Excel Sheet.
Similar to render as PDF in Visual force page, we can render Visual force Page in Excel Sheet Format.
This converts your Page Data in Excel sheet when you Click on preview. It downloads excel sheet of your Visual Force Page.
Hope you guys undersood the problem statement, Now lets go to developer console and implement this.
Follow Below steps to accomplish this-
We have taken a fields of Standard object account on visual force page and rendered it as excel to collect the details of all account records in excel sheet format.
Check below Code-
Visual Force Page Name- accExcel.vfp
<apex:page Controller="accExcelController" contentType="application/vnd.ms-excel#fileNameForExcel.xls" language="en-US" cache="True" >
<apex:pageBlock >
<apex:pageBlockTable value="{!showList}" id="accountId" var="a">
<apex:column headerValue="Name" value="{!a.Name}"/>
<apex:column headerValue="Phone" value="{!a.Phone}"/>
<apex:column headerValue="Type" value="{!a.Type}"/>
<apex:column headerValue="Rating" value="{!a.Rating}"/>
<apex:column headerValue="Status" value="{!a.Status__c}"/>
<apex:column headerValue="Description" value="{!a.Description__c}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>
Visual Force Page Controller Name- accExcelController.apxc
public class accExcelController {
public list<account> showList = new list<account>([select Id, Name,Phone,Type,Rating,Status__c,Description__c from Account]);
Public list<account> getshowList(){
return showList;
}
}
The above code will render your Visual Force Page in Excel format.
You can also refer Git Repo for the reference of Code
Stay tuned for upcoming Blogs.
Thanks
Happy Coding😊
Contact:
Twitter Handle : Lakhan Meghani
Apextips Twitter handle : Apextips
linkedin : Lakhan Meghani
Thanks for appreciation :)
ReplyDelete