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 value="{!acc}" var="account">                    
        <apex:column value="{!account.id}"/>              
        <apex:column value="{!account.name}"/>            

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])   

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

  • So what's the problem you are facing? Populating cs list?
    – highfive
    Commented Jul 22, 2014 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
    Commented Jul 22, 2014 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
    Commented Jul 22, 2014 at 8:00
  • Sorry for not being clear enough. Let me update my question with a similar screenshot workbench...
    – MnZ
    Commented Jul 22, 2014 at 8:10

1 Answer 1


Try this,

<apex:dataTable value="{!acc}" var="a">
        <apex:column value="{!a.ID}"/>
        <apex:column value="{!a.Name}"/>
         <apex:dataTable value="{!a.contacts}" var="c">
            <apex:column value="{!c.id}"/>                          
            <apex:column value="{!c.name}"/>              
            <apex:column value="{!c.phone}"/> 
  • 1
    Glad it helped.your welcome!
    – snehakem
    Commented Jul 22, 2014 at 8:35
  • 3
    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.
    – pchittum
    Commented Jul 22, 2014 at 8:46
  • Hi @Peter! I realized that as soon as you posted :). Thanks
    – MnZ
    Commented Jul 22, 2014 at 9:21
  • 1
    snehakem, not too late to edit your answer... :-)
    – pchittum
    Commented Jul 22, 2014 at 9:28
  • 1
    Thanks again @Peter.you read my mind :).i was bit hesitated,thought you would have that pleasure :P
    – snehakem
    Commented Jul 22, 2014 at 9:33

You must log in to answer this question.

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