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
Mohan t 7Mohan t 7 

Export Vf to excel but avoid button from that page ?

I am facing an issue when exporting visual force page data as excel. As it exporting whole page ......The problem was like i have vf page with title and button...so when i click the button the data in pageblock table should download. but it download  including button as well . how to avoid button on page or how to download only pageblock table data.

VF code --


<apex:page Standardcontroller="Account" extensions="accNotes" standardStylesheets="false" contentType="{!IF(isExport = true, 'application/vnd.ms-excel#contacts.xls', '')}" cache="true">
<apex:form >
 <apex:pageBlock id="pgb">
 <apex:pageBlockSection columns="8" >
 <apex:outputPanel layout="block">
 <b><apex:outputLabel style="margin" value="Notes & Attachments"></apex:outputLabel></b>

 <apex:commandButton id="button" style="margin-left:80px;" value="Export as Excel" action="{!exportToExcel}" reRender="">     /////**** This is also printing on excel page when downloaded******************///  
 </apex:commandButton>

 </apex:outputPanel>
 </apex:pageBlockSection>
 
 <apex:pageBlockSection >
 <apex:pageBlockTable value="{!Recordsall}"  var="n"> 
 <apex:column style="overflow:hidden;white-space:nowrap" value="{!acc.AccountNumber}">
 <apex:facet name="header">Account Number</apex:facet>
 </apex:column>
 <apex:column style="overflow:hidden;white-space:nowrap" value="{!acc.name}">
 <apex:facet name="header">Account Name</apex:facet>
 </apex:pageBlockSection>
 </apex:pageBlock>
 </apex:form>
 </apex:page>
Sai harsha 7Sai harsha 7
Hi Mohan

What is the need of the button in the vfpage if you have already mention the content type attribute in <apex:page > component. 

just remove the button and check it once. when ever the page relaods the content will gets download in the excel sheet.


thanks & regards
Srikanth

 
Raj VakatiRaj Vakati
Change the code as shonw below
 
pex:commandButton id="button" style="margin-left:80px;" value="Export as Excel" action="{!exportToExcel}" reRender="" render="{!IF(isExport = false"}>

<apex:page Standardcontroller="Account" extensions="accNotes" standardStylesheets="false" contentType="{!IF(isExport = true, 'application/vnd.ms-excel#contacts.xls', '')}" cache="true">
<apex:form >
 <apex:pageBlock id="pgb">
 <apex:pageBlockSection columns="8" >
 <apex:outputPanel layout="block">
 <b><apex:outputLabel style="margin" value="Notes & Attachments"></apex:outputLabel></b>

 <apex:commandButton id="button" style="margin-left:80px;" value="Export as Excel" action="{!exportToExcel}" reRender="" render="{!IF(isExport = false"}>     /////**** This is also printing on excel page when downloaded******************///  
 </apex:commandButton>

 </apex:outputPanel>
 </apex:pageBlockSection>
 
 <apex:pageBlockSection >
 <apex:pageBlockTable value="{!Recordsall}"  var="n"> 
 <apex:column style="overflow:hidden;white-space:nowrap" value="{!acc.AccountNumber}">
 <apex:facet name="header">Account Number</apex:facet>
 </apex:column>
 <apex:column style="overflow:hidden;white-space:nowrap" value="{!acc.name}">
 <apex:facet name="header">Account Name</apex:facet>
 </apex:pageBlockSection>
 </apex:pageBlock>
 </apex:form>
 </apex:page>