I have my below VF Page where I have to show dynamically the field names and corresponding data.

<apex:page standardController="X" recordsetVar="CPAT" extensions="getContactFromAccount"  contentType="application/vnd.ms-excel#BusinessPlanExport.xls" cache="true">
   <apex:form >
        <apex:pageBlock title="Account List" id="CPATAccounts">
           <apex:pageBlockTable var="item" value="{!Contacts}">
            <apex:repeat var="f" value="{!planfields}">
                <apex:column value="{!item[f]}"/>

I have below the controller.

public class getContactFromAccount {
    public X acc;
    public List <X> Contacts {get; set;}
        public String planfields {get; set;}
    public getContactFromAccount(ApexPages.StandardSetController controller) {
        this.acc = (X)controller.getRecord();

    public List <X> getContacts() {
        planfields = 'Id, Name, CreatedbyID, OwnerId';
        String query = 'SELECT ';
        query += planfields + ' from X WHERE PID__c = \'XXXX\'';
          System.debug('query@@' + query);
        Contacts = (List<CPAT_Plan_Account_abv__c >)Database.query(query);
            System.debug('Contactlist' + Contacts);
            return Contacts;



Here, I am getting the required excel successfully downloaded but it is blank and not having any data. Please note my query results are fine and having data.

Can someone suggest me where I am doing wrong?

Please find how the excel looks like just after being downloaded. enter image description here

  • So calling getContacts returns a list, but viewing the page in the browser shows no data? And this page is also being exported/downloaded as an excel document? Can you make an edit to your question to include the page? Apr 20, 2018 at 15:19
  • You say "below VF", but there is no code. Also, "required excel"? We seem to be missing a lot of context here. Apr 20, 2018 at 15:26
  • 1
    Turns out the code was there, just not indented. @SFDC_Learner, I'd still suggest making an edit to include some extra context & explain exactly what isn't working - the export to excel or displaying your data. Apr 20, 2018 at 15:29
  • Please find the attached screenshot of how excel looks like. I find though the query has data, in page it is not showing up. Apr 20, 2018 at 15:57
  • I am confused why you thought your applied tags were appropriate. None of the tags you included were remotely relevant aside from [apex], and you missed the most obvious one.
    – Adrian Larson
    Apr 20, 2018 at 15:58

1 Answer 1


I just tossed something together to see if I could get it to work and this is what I came up with and it works.


<apex:page controller="testPageController" contentType="application/vnd.ms-excel#BusinessPlanExport.xls">
        <apex:pageBlockTable value="{!contacts}" var="c">
            <apex:repeat value="{!contactFields}" var="f">
                <apex:column value="{!c[f]}" />


public class testPageController {
    public List<Contact> contacts {get; set;}

    public List<String> contactFields {
        get {
            if (contactFields == null) {
                contactFields = new List<String>();

                for (Schema.FieldSetMember fsm : SObjectType.Contact.FieldSets.testFieldSet.getFields()) {

            return contactFields;

        private set;

    public String queryFieldString {
        get {
            if (queryFieldString == null) {
                queryFieldString = '';

                for (String s : contactFields) {
                    queryFieldString += ',' + s;

            return queryFieldString;

        private set;

    public testPageController() {
        String query = 'SELECT Id' + queryFieldString + ' FROM Contact';
        this.contacts = database.query(query);

The only difference I see between your code and the code I used as a sample for mine is that yours has the <apex:form> tag which is not required; however, adding that in did not cause the file to display incorrectly.

Also, you're using the standard set controller (since you have record set var) but you never use the records from the controller (at least, in the sample provided). Consider just going with a custom controller like I did, gives you a little more control and it reduces the complexity of the page.

Lastly, the code provided can't be copied/pasted into an org which makes it hard for us to test and troubleshoot. You should re-write your sample to use standard objects and fields and verify that it is still broken, and if so, provide a full sample containing the error that we can reproduce as it will allow us to actually troubleshoot the issue. https://tex.meta.stackexchange.com/questions/228/ive-just-been-asked-to-write-a-minimal-example-what-is-that

I should also note, that I got a warning when opening my excel file saying that the format didn't match, but it still opened and displayed correctly. Consider a CSV instead of an excel file. You wont be able to use pageblock/pageblock tables though, youll need nested repeats. Last time I worked on a CSV vf the file was kinda finicy with formatting and seemed to care about the line breaks that were in the VF file so watch out for that.


You must log in to answer this question.

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