+ Start a Discussion
Amit Visapurkar 5Amit Visapurkar 5 

How to populate data from visualforce page in a particular column in excel

I want to populate data from visualforce page in a particular column in excel for example column D

Its urgent
NagendraNagendra (Salesforce Developers) 
Hi Amit,

Please find the sample code below and modify it as per your requirement.

VisualForce Page:
<apex:page controller="MyControlleremployee" tabStyle="employee__c">
<apex:form >
  <apex:pageBlock id="blockId" title="Employee Details">
    <apex:pageblocktable value="{!myemployees}" var="emp">
         <apex:column >
         <apex:facet name="header">{!$ObjectType.employee__c.fields.name.label}</apex:facet>
             <apex:inputField value="{!emp.name}" rendered="{!tobeEdited == emp.id}"/>
             <apex:outputField value="{!emp.name}"  rendered="{!!(tobeEdited == emp.id)}"/>
         </apex:column>
         <apex:column >
         <apex:facet name="header">{!$ObjectType.employee__c.fields.email__c.label}</apex:facet>
             <apex:inputField value="{!emp.email__c}"  rendered="{!tobeEdited == emp.id}"/>
             <apex:outputField value="{!emp.email__c}"   rendered="{!!(tobeEdited == emp.id)}"/>             
         </apex:column>
         <apex:column >
         <apex:facet name="header">{!$ObjectType.employee__c.fields.max_pay__c.label}</apex:facet>
             <apex:inputField value="{!emp.max_pay__c}"  rendered="{!tobeEdited == emp.id}"/>
             <apex:outputField value="{!emp.max_pay__c}"   rendered="{!!(tobeEdited == emp.id)}"/>             
         </apex:column>
         <apex:column >
         <apex:facet name="header">{!$ObjectType.employee__c.fields.min_pay__c.label}</apex:facet>
             <apex:inputField value="{!emp.min_pay__c}"  rendered="{!tobeEdited == emp.id}"/>
             <apex:outputField value="{!emp.min_pay__c}"   rendered="{!!(tobeEdited == emp.id)}"/>             
         </apex:column>
         <apex:column >
              <apex:commandbutton value="Edit" rerender="blockId" rendered="{!!(tobeEdited == emp.id)}" >
                  <apex:param assignTo="{!tobeEdited}" value="{!emp.id}" name="ittobeedited"/>
              </apex:commandbutton>
                <apex:commandbutton value="save" rerender="blockId" rendered="{!!(tobeEdited == emp.id)}" />
              <apex:commandButton value="save" action="{!saveRecord}" reRender="blockId" rendered="{!tobeEdited == emp.id}"/>
               <apex:commandButton action="{!Deleted}" value="Deleted" />
                   <apex:commandButton action="{!Download}" value="Download"/>
  </apex:column>
   </apex:pageblocktable>
</apex:pageBlock>   
</apex:form>
</apex:page>

Apex Controller:
public class MyControlleremployee {

 public PageReference Download() {
       PageReference nextpage = new PageReference('/apex/ExcelReportPage?recordID');
            return nextpage;
}




    public List<employee__c> myemployees;
    public id tobeEdited{get; set;}     

    public MyControlleremployee(){

    }

    public List<employee__c> getMyemployees(){
        myemployees = [select Id,name,email__c,max_pay__c,min_pay__c from employee__c WHERE (owner.id =: userinfo.getuserid()) limit 10];            
        return myemployees;           
    }  
 public void saveRecord(){
        employee__c tobeupdated;
        for(employee__c temp:myemployees){
            if(temp.id==tobeEdited){
                tobeupdated = temp;   
                break;
            }
        }
        update tobeupdated;
        tobeEdited = null;
    }
    public void Deleted(){
    employee__c tobedeleted;
    for(employee__c tmp:myemployees){
    tobedeleted = tmp;
    delete tobedeleted;
    tobedeleted = null;
    }

        }
        }

PageReference Page:
<apex:page controller="MyControlleremployee" contentType="application/vnd.ms-excel#SalesForceExport.xls" cache="true">
    <apex:pageBlock title="employeeDetails" id="emp"  >
        <apex:pageBlockTable value="{!myemployees}" var="emp" >

            <apex:column value="{!emp.Name}"/>
             <apex:column value="{!emp.email__c}"/>
              <apex:column value="{!emp.max_pay__c}"/>
                 <apex:column value="{!emp.min_pay__c}"/>
</apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

Hope this helps.

Best Regards,
Nagendra.P