+ Start a Discussion
Natavar GhodasaraNatavar Ghodasara 

How to generate CVS file on button click ?

I had tried three methods. When i write in the page like contentType="application/vnd.ms-excel#ConsignmentSearchData.csv" or contentType="application/octet-stream#record.csv" or contentType="text/csv#filename.csv" it is giing me the code of the page in the cvs file.
Anoop yadavAnoop yadav
Hi Natavar,

try below links to export as Excel.

http://anupjadhav.com/2012/04/23/export-to-excel-functionality/
http://help.salesforce.com/apex/HTViewSolution?id=000003176&language=en_US (http://help.salesforce.com/apex/HTViewSolution?id=000003176&language=en_US)
vmanumachu1.393650924860069E12vmanumachu1.393650924860069E12
if you are using pageblocktable replace it with apex:repeat, also remove pageblock or placeblocksection references if you have any.

Mark it as best answer if it works for you. It helps others.
Natavar GhodasaraNatavar Ghodasara
Thanks Anoop and vmanumachu1.393650924860069E12. I want it in CSV format. here is my code.

VF page :- Employee


<apex:page controller="Employeedetail" contentType="application/vnd.ms-excel#Employeedatafile.csv"  language="en-US">
            <table>
                <tr>
                    <td>
                        Employee ID
                    </td>
                    <td>
                        Employee Name
                    </td>
                    <td>
                        Employee Phone Number
                    </td>
                </tr>
                <apex:repeat value="{!lstEmployeeDetails}" var="varemp" id="rptr">
                     <tr>
                           <td>
                               <apex:outputField id="optlbl_EmployeeID" value="{!varemp.EmployeeID__c}"/>                              
                           </td>                    
                           <td>
                            <apex:outputField id="optlbl_EmployeeName"  value="{!varemp.EmployeeName__c}"/>
                           </td>                    
                           <td>
                           <apex:outputField id="optlbl_EmployeePhoneno" value="{!varemp.Phone_Number__c}"/>
                           </td>
                     </tr>
                </apex:repeat>            
            </table>
            <br />      
        </apex:page>

Controller :- Employeedetail

Public class Employeedetail
{
     Public EmployeeDetails__c obj_Employeedetail{get;set;}
     Public List<EmployeeDetails__c> lstEmployeeDetails{get;set;}
    // class that shows the data of the employee.
    Public Employeedetail()
    {
        // default constructor
        lstEmployeeDetails =[select EmployeeID__c,EmployeeName__c,Phone_Number__c from EmployeeDetails__c];
    }
public pagereference Download()
     {
        PageReference exdownload = null;
        exdownload = Page.Employeedetaildownload;
        exdownload.setRedirect(false);
        return exdownload ;
    }

}

Natavar GhodasaraNatavar Ghodasara
The Above code is giving me the output as dowloading the CSV with page code and contents. I want only content not code.
vmanumachu1.393650924860069E12vmanumachu1.393650924860069E12
Try the below. It works.

<apex:page controller="Employeedetail"   cache="true" contentType="text/csv#filename.csv" language="en-US">"Employee ID","Employee Name","Employee Phone Number"
     <apex:repeat value="{!lstEmployeeDetails}" var="con" >
     {!con.Name},{!con.Functional_Area__c},{!con.Max_Pay__c}
                </apex:repeat>           
        </apex:page>
 

User-added image