1

I am trying like this but its not getting exact output.

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 class:

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()) ];            
        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 contentType="application/x-excel#reRenderAsTestFile.xls" Controller="MyControlleremployee">
    <apex:pageBlock >
        <apex:pageblockTable value="{!myemployees}" var="v" border="1" 
                      columnsWidth="100px,200px,150px" cellspacing="0" cellpadding="8">
            <apex:column headerValue="name">{!v.name}</apex:column>
            <apex:column headerValue="email__c">{!v.email__c}</apex:column>
            <apex:column headerValue="max_pay__c">{!v.max_pay__c}</apex:column>
            <apex:column headerValue="min_pay__c">{!v.min_pay__c}</apex:column>
        </apex:pageblockTable>
    </apex:pageBlock>    
</apex:page>

can anybody help me.

5
  • When you say "not getting exact output", what do you mean? Can you explain what you are getting and what you would like to get?
    – lindon fox
    Commented Jan 7, 2015 at 7:41
  • make sure you use contentType. Check my example below. Commented Jan 7, 2015 at 7:50
  • Its getting all records in contact.i want only single record when download button clicks.i put a single button for single record.for that particular record download button clicks that particular record should have to download.if u know the answer help me
    – user
    Commented Jan 7, 2015 at 8:00
  • Try printing off the value of userinfo.getuserid(), then copy the SQL statement and run it in the Developer Console. Does it look correct to you? Nowhere in your question do you mention that you want only a single record but you are getting many. To be kind to others who might be trying to answer your question, please edit it so that it is more clear.
    – lindon fox
    Commented Jan 7, 2015 at 8:46
  • 1) where clause should be simplified to where ownerId = :UserInfo.getuserId() 2) what is recordId in the URL of the download() action method? syntax doesnt look correct 3) you could make everyone's life easier here if you stripped out the unnecessary bits such as the save and delete methods
    – cropredy
    Commented Jan 8, 2015 at 2:59

1 Answer 1

1

Using a VF page exporting data is very easy.

Here is sample for you. Compere this with you code and you should be all set.

VF Page:

<apex:page controller="contactquery" contentType="application/vnd.ms-excel#SalesForceExport.xls" cache="true">
<apex:pageBlock title="Export Results" >
    <apex:pageBlockTable value="{!cs}" var="contact">
        <apex:column value="{!contact.ID}"/>
        <apex:column value="{!contact.Name}"/>
    </apex:pageBlockTable>
</apex:pageBlock>

Here is Controller:

public class contactquery{
public List<Contact> cs{get; set;}
public contactquery()
{
cs = new List<Contact>();
   for (Contact c : [Select id, Name from Contact])
   {       
       cs.add(c);
   }
}
} 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .