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
VietViet 

Export records to Excel with Visualforce

I have a question regarding a "export data to excel" on a Visualforce page.

For example, I retrieve a bunch of records of a SObject on the extension class in a List ( like "List<Account>").
I want to create a button or a link on the visualforce page to export all the records inside that list to Excel.

Is that possible, does anybody knows how to do it ?

Best Answer chosen by Admin (Salesforce Developers) 
Pradeep_NavatarPradeep_Navatar

You can genereate excel by simply modifying the ContentType attribute on the <apex:page> tag, your Visualforce code will automatically generate an Excel document. For example, the following code will create a table of Contact data for a given Account:

 

<apex:page standardController="Account">
   <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>

 

Another  way is to Bring the data in a datatable and call the sample javascript given below at the <apex:commandbutton> :

 

<html>
<head>
<script type="text/javascript">
function CreateExcelSheet()
{
var x=myTable.rows
var xls = new ActiveXObject("Excel.Application")
xls.visible = true
xls.Workbooks.Add
for (i = 0; i < x.length; i++)
{
var y = x[i].cells
for (j = 0; j < y.length; j++)
{
xls.Cells( i+1, j+1).Value = y[j].innerText
}
}
}
</script>
</head>

<body marginheight="0" marginwidth="0">
<form>
<input type="button" onclick="CreateExcelSheet()" value="Create Excel Sheet">
</form>
<table id="myTable" border="1">
<tr> <b><td>Name </td> <td>Age</td></b></tr>
<tr> <td>Shivani </td> <td>25</td> </tr>
<tr> <td>Naren </td> <td>28</td> </tr>
<tr> <td>Logs</td> <td>57</td> </tr>
<tr> <td>Kas</td> <td>54</td> </tr>
<tr> <td>Sent </td> <td>26</td> </tr>
<tr> <td>Bruce </td> <td>7</td> </tr>
</table>
</body>
</html>

 

Did this answer your question? if so, please mark it solved.

 

 

 

 

<html>

<head>
<script type="text/javascript">

function CreateExcelSheet()
{


var x=myTable.rows

var xls = new ActiveXObject("Excel.Application")
xls.visible = true
xls.Workbooks.Add
for (i = 0; i < x.length; i++)
{
var y = x[i].cells

for (j = 0; j < y.length; j++)
{
xls.Cells( i+1, j+1).Value = y[j].innerText
}
}




}
</script>


</head>

<body marginheight="0" marginwidth="0">
<form>
<input type="button" onclick="CreateExcelSheet()" value="Create Excel Sheet">
</form>
<table id="myTable" border="1">
<tr> <b><td>Name </td> <td>Age</td></b></tr>
<tr> <td>Shivani </td> <td>25</td> </tr>
<tr> <td>Naren </td> <td>28</td> </tr>
<tr> <td>Logs</td> <td>57</td> </tr>
<tr> <td>Kas</td> <td>54</td> </tr>
<tr> <td>Sent </td> <td>26</td> </tr>
<tr> <td>Bruce </td> <td>7</td> </tr>
</table>



</body>

</html>

All Answers

hisrinuhisrinu

If you set the contenttype="application/vnd.ms-excel" at the page level, then it automatically renders the page as an excel sheet

Pradeep_NavatarPradeep_Navatar

You can genereate excel by simply modifying the ContentType attribute on the <apex:page> tag, your Visualforce code will automatically generate an Excel document. For example, the following code will create a table of Contact data for a given Account:

 

<apex:page standardController="Account">
   <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>

 

Another  way is to Bring the data in a datatable and call the sample javascript given below at the <apex:commandbutton> :

 

<html>
<head>
<script type="text/javascript">
function CreateExcelSheet()
{
var x=myTable.rows
var xls = new ActiveXObject("Excel.Application")
xls.visible = true
xls.Workbooks.Add
for (i = 0; i < x.length; i++)
{
var y = x[i].cells
for (j = 0; j < y.length; j++)
{
xls.Cells( i+1, j+1).Value = y[j].innerText
}
}
}
</script>
</head>

<body marginheight="0" marginwidth="0">
<form>
<input type="button" onclick="CreateExcelSheet()" value="Create Excel Sheet">
</form>
<table id="myTable" border="1">
<tr> <b><td>Name </td> <td>Age</td></b></tr>
<tr> <td>Shivani </td> <td>25</td> </tr>
<tr> <td>Naren </td> <td>28</td> </tr>
<tr> <td>Logs</td> <td>57</td> </tr>
<tr> <td>Kas</td> <td>54</td> </tr>
<tr> <td>Sent </td> <td>26</td> </tr>
<tr> <td>Bruce </td> <td>7</td> </tr>
</table>
</body>
</html>

 

Did this answer your question? if so, please mark it solved.

 

 

 

 

<html>

<head>
<script type="text/javascript">

function CreateExcelSheet()
{


var x=myTable.rows

var xls = new ActiveXObject("Excel.Application")
xls.visible = true
xls.Workbooks.Add
for (i = 0; i < x.length; i++)
{
var y = x[i].cells

for (j = 0; j < y.length; j++)
{
xls.Cells( i+1, j+1).Value = y[j].innerText
}
}




}
</script>


</head>

<body marginheight="0" marginwidth="0">
<form>
<input type="button" onclick="CreateExcelSheet()" value="Create Excel Sheet">
</form>
<table id="myTable" border="1">
<tr> <b><td>Name </td> <td>Age</td></b></tr>
<tr> <td>Shivani </td> <td>25</td> </tr>
<tr> <td>Naren </td> <td>28</td> </tr>
<tr> <td>Logs</td> <td>57</td> </tr>
<tr> <td>Kas</td> <td>54</td> </tr>
<tr> <td>Sent </td> <td>26</td> </tr>
<tr> <td>Bruce </td> <td>7</td> </tr>
</table>



</body>

</html>
This was selected as the best answer
bcgbcg

Hi Pradeep,

 

Am facing some problem in the same. i.e. am exporting the data of visual force page on excel with the help of content type. and its also successfully exported as .xls file. But problem is in the .xls file Calender is coming at the end of report i.e. Week days name and Years and Months in the form of picklist.

 

am able to remove the weekdays and image from the file but Picklist are not deleting or not going.Will you please help me for this that is why this problem is coming.?

 

Thanks

Neeru

Mahesh.NallaMahesh.Nalla

Hi Neeru,

I'm also facing the problem same as yours. Could you get it resolved? If resolved could you please help me

Thanks,

mahesh

DuffAlthausDuffAlthaus

The issue is with using the apex:form tag.  When the form tag is included in a page that will be exported to Excel content type or as PDF, these unexpected items appear at the end.  Remove the apex:form tag (and unfortunately, anything that requires to be included in the FORM tag) and the items will disappear.  Good luck.

Juan SpagnoliJuan Spagnoli

Hi guys, i'm using contentType="application/vnd.ms-excel#filename.xls" in my VF but i'm loosing styles definitions like "border:solid 1px #333" or "font-family: Arial Unicode MS, Arial, Verdana"

 

Do you know how to set styling in the xls?

 

Thanks.

Amit4SFDCAmit4SFDC
Thanks ,It is very useful post.
Carlos AndiaCarlos Andia

That worked. Thanks.

Harshit Mishra 12Harshit Mishra 12
Hi guys, i'm using contentType="application/vnd.ms-excel#filename.xls" in my VF but i'm loosing styles definitions like "border:solid 1px #333" or "font-family: Arial Unicode MS, Arial, Verdana"

 

Do you know how to set styling in the xls?