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
justin_sfdcjustin_sfdc 

download the records into excel using Internet Explorer

Hey guys,

I am trying to download the records that I have in Visualforce page to excel. I am able to download the file in all other browsers except IE8. I do not want to download the whole VF page. I want to download the records that are in the VF page. Basically, the VF page has excel icon, which when clicked should download the records in excel format. I have tried using cache="true" and contenttype="application/vnd.ms-excel#filename.xls" but this downloads the whole VF page at the time of loading. I want to be able to download the records at the click of the icon instead. Any kind of help is appreciated.

Thanks!

hemant037hemant037

hello.

first develope new page with pageblock table in this table data insert data that you want to download as excel and than add Apex:page tag Like this

 

 

<apex:page controller="Contorller_Name"  contentType="application/vnd.ms-excel#FileName.xls">

and than on Download icon button you have a action method right ?

in this action method just redirect page to this page.

And be carefull both page(Download button containing page & this page block table containing page) should contain SAME controller.

hope this will help you.
if isnt than plz post situation at you stucked..

Thanks.

justin_sfdcjustin_sfdc

HI Hemant037, 

    Thanks for the quick reply. I did understand the logic that you are putting on, but I am a bit confused. I now created a new VF page with all the records on that page and also I have the controller name SAME. In this new page, I have the tag as you told me. 
In my originial VF page, the line of code in the download icon button is: a href="#" onclick="renderRecords(); now how do i direct it to this new page as there is no renderRecords() method in my controller. And also if this is achieved, at the time the icon is clicked, this visual force page will direct it to this new page to download the file and then it will stay in this new visual force page, and the user will not be able to do anything else after the download then.

 

Thanks a lot, I really appreciate your help.

justin_sfdcjustin_sfdc

renderRecords() is being called in actionFunction tag, with action="{!renderRecords}". So, I redirected from this page to the new VF page I created. But what this does is downloads the Visualforce page into excel. It does not download all the records, is there any other alternative

hemant037hemant037

ok.. 

i am doing this same its working for me i am adding code here.

this is Page code for download page in which we will make a table of our data which we want to download as Excel.

<apex:page controller="DemoExcel" contentType="application/vnd.ms-excel#FileName.xls">

<apex:pageBlock>
<apex:pageBlockTable value="{!GetUser}" var="a" columns="3" width="1000" >
<apex:column value="{!a.Id}"/>
<apex:column value="{!a.Name}"/>
<apex:column value="{!a.Email}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>


now this is code for controller "DemoExcel" in which we will add code for get data into above table...

 

public with sharing class DemoExcel

{

                public List<user> GetUser()

               {

                 List<User> u=[select Id,Name,Email from User];

                 return u;
               }             

}

 

-- Now main page from which we will click button for download.

 

<apex:page controller="CsvExample" tabStyle="Wrapper__tab" >
<apex:form >

<apex:pageBlock >
<apex:pageBlockButtons >
<apex:commandButton value="Download as .Xls" action="{!DownloadXls}"/>
</apex:pageBlockButtons>

</apex:pageBlock>
</apex:form>
</apex:page>

 

-----------------------------------------
Now we will add code for DownloadXls action in our controller

 

public with sharing class DemoExcel

{

                public List<user> GetUser()

               {

                 List<User> u=[select Id,Name,Email from User];

                 return u;
               }

 

            

             public PageReference DownloadXls()
             {

                  return new PageReference('/apex/NAMEOFYOURFIRSTDOWNLOAD_PAGE');
            }

}

now i think this will be helpful to you.
if it is than please mark it as solved.
Thanks.

justin_sfdcjustin_sfdc

Hi Hemant037,

 

I finally got it solved. In order to open it in internet explorer, I had to add the line cache="true" in the 2nd visualforce page. Thank you very much for all the help you have guided me through. I have one more issue on this topic if you have any idea about it. The excel download works completely fine in any browser.

After the file is downloaded, when I open it in Windows, all the records are fine, but when I download the file in mac and open it it add some html code in the first column of the excel. below is the code it shows:

 

if(!window.sfdcPage){window.sfdcPage = new ApexPage();} UserContext.initialize({"networkId":"","locale":"en_US","labelLastModified":"1356556072000","isDefaultNetwork":true,"today":"1/4/2013 2:24 PM","timeFormat":"h:mm a","userPreferences":[{"index":112,"name":"HideInlineEditSplash","value":false},{"index":114,"name":"OverrideTaskSendNotification","value":false},{"index":115,"name":"DefaultTaskSendNotification","value":false},{"index":119,"name":"HideUserLayoutStdFieldInfo","value":false},{"index":116,"name":"HideRPPWarning","value":false},{"index":87,"name":"HideInlineSchedulingSplash","value":false},{"index":88,"name":"HideCRUCNotification","value":false},{"index":89,"name":"HideNewPLESplash","value":false},{"index":90,"name":"HideNewPLEWarnIE6","value":false},{"index":122,"name":"HideOverrideSharingMessage","value":false},{"index":91,"name":"HideProfileILEWarn","value":false},{"index":93,"name":"HideProfileElvVideo","value":false},{"index":97,"name":"ShowPicklistEditSplash","value":false},{"index":92,"name":"HideDataCategorySplash","value":false},{"index":128,"name":"ShowDealView","value":false},{"index":129,"name". and more

 

This problem is only in mac. I tried creating a new vf page with some data, and did the excel download for it and still shows that code. 

Please let me know if you have any idea about it and if it is something that can be solved.

 

Thanks!