Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Following is my 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:column value="{!contact.phone}"/>   
    </apex:pageBlockTable>    
    <apex:pageBlockTable value="{!acc}" var="account">                    
        <apex:column value="{!account.id}"/>              
        <apex:column value="{!account.name}"/>            
    </apex:pageBlockTable>
</apex:pageBlock>
</apex:page>

And following is my controller

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

I get an excel sheet for accounts only.

PROBLEM: Unable to display contacts related to every account in a separate column. How can I achieve this? Thanks in advance.

UPDATE: Following is the required sample excel(without borders).Sample screenshot

share|improve this question
    
So what's the problem you are facing? Populating cs list? –  highfive Jul 22 at 6:46
    
The excel sheet displays only account information. My problem is that I'm unable to display contacts in a separate column in the same excel sheet. –  MnZ Jul 22 at 6:47
    
Can you detail exactly what you want to see? An Account can have many Contacts - do you e.g. want the Account information duplicating for each of them or something else? –  Keith C Jul 22 at 8:00
    
Sorry for not being clear enough. Let me update my question with a similar screenshot workbench... –  MnZ Jul 22 at 8:10

1 Answer 1

up vote 4 down vote accepted

Try this,

<apex:dataTable value="{!acc}" var="a">
        <apex:column value="{!a.ID}"/>
        <apex:column value="{!a.Name}"/>
        <apex:column>
         <apex:dataTable value="{!a.contacts}" var="c">
            <apex:column value="{!c.id}"/>                          
            <apex:column value="{!c.name}"/>              
            <apex:column value="{!c.phone}"/> 
         </apex:dataTable>
        </apex:column>
</apex:dataTable>
share|improve this answer
    
Exactly what I need. Thanks. –  MnZ Jul 22 at 8:34
    
Glad it helped.your welcome! –  snehakem Jul 22 at 8:35
2  
The one thing I'd suggest is using apex:dataTable instead of pageBlockTable. You're downloading a lot of extra stuff with pageBlockTable that is unused in Excel (class attributes, event handlers, etc.). With my simple example of some 20 accounts, my file size with dataTable is 19k, versus 37k for pageBlockTable...almost double the size. You're also forcing the VF page renderer to do more work. –  Peter Jul 22 at 8:46
    
Hi @Peter! I realized that as soon as you posted :). Thanks –  MnZ Jul 22 at 9:21
    
Thanks @Peter. Noted your point :) –  snehakem Jul 22 at 9:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.