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
Meeta Khullar 5Meeta Khullar 5 

Export to CSV button on VisualForce Page not working

I'm new to export csv functionality on Visualforce page. We have a command button on the visualforce page that is suppose to export the output of the page. There is no coding for this button. I need help in building this functionality. Here are the code for the VF page as well as the controller
VF Page
<apex:page controller="RMSControllerTest" title="Physico" docType="html-5.0" showHeader="true" >

 <apex:form >
 
    <apex:pageBlock title="Revenue Recognition test">
           <apex:pageBlockSection >
            <apex:pageBlockSectionItem >
           
            <apex:facet name="header">
                <span style="color:black">Enter Search Details</span>
            </apex:facet>
       
            <apex:outputlabel value="From:"/>
               <apex:input type="date" value="{!startDate}" size="10" id="StartDate" onfocus="DatePicker.pickDate(false, this , false);" />
            </apex:pageBlockSectionItem> 
           
            <apex:pageBlockSectionItem >
                <apex:outputlabel value="To:"/>
               <apex:input type="date" value="{!endDate}" size="10" id="Enddate" onfocus="DatePicker.pickDate(false, this , false);" />         
               <!--<apex:input value="{!endDate}" size="10" id="EndDate" onfocus="DatePicker.pickDate(false, this , false);" /> -->
            </apex:pageBlockSectionItem>
           
             <apex:inputText id="ordernumber" title="Order No." value="{!orderNumber}" label="Order No." />
          
             <apex:inputText id="invoiceNumber" title="Invoice no" value="{!InvoiceNo}" label="Invoice No."/>
          
             <apex:pageBlockSectionItem >
                 <apex:outputLabel value="QAD Domain"/>
                 <apex:inputField id="QADDomain" value="{!ord.account.QAD_Domain__c}">
                       <apex:actionSupport event="onchange" rendered="false">                            
                           <apex:param name="domainName" value="{!ord.account.QAD_Domain__c}" assignTo="{!domain}"/> 
                         </apex:actionSupport>
                 </apex:inputField>
           </apex:pageBlockSectionItem>
                     
          
            <apex:pageBlockSectionItem >
                 <apex:outputLabel value="Account" for="theLookup"/>
                 <apex:inputField id="Account" value="{!ord.accountid}" label="Account">
                     <apex:actionSupport event="onchange" rendered="false">                            
                           <apex:param name="accountId" value="" assignTo="{!accountId}"/> 
                     </apex:actionSupport>
                 </apex:inputField>
             </apex:pageBlockSectionItem>
             
          </apex:pageBlockSection>
         
         <apex:pageBlockButtons location="bottom" >
            <apex:commandButton id="searchBtn" action="{!searchResults}" value="Search" style="text-align:center;align:center;" />
            <apex:commandButton action="{!cancel}" value="Cancel" style="text-align:left"/>
            <apex:commandButton action="{!Import}" value="Export to csv" style="text-align:left"/>
         </apex:pageBlockButtons>
        </apex:pageBlock>
       
    <apex:pageblock id="RMSData">    
   
        <apex:pageblocktable value="{!invoiceLines}"  var="item" rendered="{!renderSearch}">
       
           <apex:column headervalue="Invoice Number">
               <apex:outputLink value="/{!item.invoiceLine.invoice__r.id}" target="_parent">{!item.invoiceLine.invoice__r.name} </apex:outputLink>
            </apex:column>
           
            <apex:column headervalue="Invoice Line Name">
                <apex:outputLink value="/{!item.invoiceLine.id}" target="_parent">{!item.invoiceLine.Name}</apex:outputLink>
            </apex:column>
           
            <apex:column headervalue="Product Name">
                <apex:outputLink value="/{!item.invoiceLine.Order_Product__r.PricebookEntry.product2.id}" target="_parent">{!item.invoiceLine.Order_Product__r.PricebookEntry.product2.name}</apex:outputLink>
            </apex:column>
           
             <apex:column headervalue="Product Line">
                <apex:outputtext value="{!item.invoiceLine.Order_Product__r.PricebookEntry.product2.Product_Line__c}"/>
            </apex:column>           
          
           
            <apex:column headervalue="Start Date">
              <apex:outputtext value="{0,date,MM'/'dd'/'yyyy}">
                    <apex:param value="{!item.invoiceLine.Start_Date__c}"/>
                    </apex:outputtext>
             </apex:column>
           
             <apex:column headervalue="End Date">
                    <apex:outputtext value="{0,date,MM'/'dd'/'yyyy}">
                    <apex:param value="{!item.invoiceLine.End_Date__c}"/>
                    </apex:outputtext>
            </apex:column>
           
             <apex:column headervalue="Recognized Revenue">
                <apex:outputtext value="{!item.recognizedRevenue}"/>
            </apex:column>
           
            <apex:column headervalue="Deferred Revenue">
                 <apex:outputtext value="{!item.deferredRevenue}"/>
            </apex:column>
           
            <apex:column headervalue="Total Amount">
                 <apex:outputtext value="{!item.totalAmount}"/>
            </apex:column>
           
             <apex:column headervalue="QAD Domian">
                 <apex:outputtext value="{!item.invoiceLine.Order_Product__r.order.account.QAD_Domain__c}"/>
            </apex:column>
           
                       
        </apex:pageblocktable>
    </apex:pageblock>
     <script type="text/javascript">
      function validateSearchCriteria(field){
         var ordernumber = document.getElementById(field).value;
          var isIntegerRegex = /^\s*(\+|-)?\d+\s*$/;
         if(String(ordernumber).search(isIntegerRegex) == -1){
            alert('please enter valid order number');
         }
         return true;
      }
    </script>
  </apex:form>  
</apex:page>


Controller

public with sharing class RMSControllerTest {
    public Date startDate {get; set;}
    public Date endDate {get; set;}
    public boolean renderSearch = false;
    public string  orderNumber{get;set;}
    public string  InvoiceNo {get;set;}
    public String  domain {get;set;}
    public String accountId { get;  set; }
    public Order ord { get; set; }
       
    public RMSControllerTest(ApexPages.StandardController controller) {
        this.startDate = startDate;
        this.endDate   = endDate;   
        this.accountId = accountId;
        this.OrderNumber= OrderNumber;
        this.domain = domain;
        this.InvoiceNo = InvoiceNo;
    }
   
    public void setRenderSearch(boolean renderSearch){
      this.renderSearch = renderSearch;
    }
   
    public boolean getRenderSearch(){
       return renderSearch;
    }
  
    public RMSControllerTest() {
          
    }
   
    public void searchResults(){
        renderSearch = true;
    }

With more coding to get the data. Here is the code on how csv is being exported  

public PageReference Import() {
      PageReference logPage = New PageReference('/apex/RevenueRecognizedTest?sfdc.tabName=01r550000000MXh');
      logPage.setRedirect(true);
      return logpage;

 
sandeep madhavsandeep madhav
Hi,

Hope this helps,

Add contentType="text/csv#Export.csv;charset=utf8"  for vf page.

Mark this as the best answer if helps