[SalesForce] Export to excel file problem with displaying information

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

Best Answer

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>
Related Topic