0

I have below VF Page that downloads the excel as I need. But I have further requirement to show A,B,C etc as header values in excel instead of the generic labels of the custom fields shown in excel.

Can someone please help me know how to achieve that?

Apex Page Code

<apex:page controller="CPAT_Plan_Accounts_Controller" contentType="application/vnd.ms-excel#BusinessPlanExport.xls">
  <head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
    <style> 
        br {mso-data-placement:same-cell;}  <!-- MSFT style causes Excel to treat <br/> as soft carriage return 'ALT-ENTER' when constructing cells from SFDC HTML output-->
    </style>
</head>
    <apex:pageBlock >
        <apex:pageBlockTable style="font-size: xx-small;" value="{!CPAT_accounts}" var="c">

           <apex:repeat value="{!allqueryfields}" var="f">
                <apex:column value="{!c[f]}"  headervalue="{!excelfields[f]}"/>
            </apex:repeat>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

As suggested in below answer, I have modified my controller to have excelfields as map.

public Map<String, String> excelFields {
        get {
            if (excelFields.size()==0) {
                excelFields = new Map<String,String>();
                    queryfields = [select Fields__c from Franchise__c where Name='ABC' and Time_Period__c='Q2 2018'].Fields__c;

                String JSONString = JSON.serialize(hcpviewcolumns);
                fieldshow='Id,';
                hcpviewcolumns = hcpviewcolumns.replaceall('\\{',';').replaceall('\\}',';').replace('\\','');

                JSONString = JSONString.replaceall('\\}, \\{','\\},\\{');
                List<String> Splitcomma = JSONString.split('\\},\\{');

                   for(String a: Splitcomma)
                   { 
                    if(!a.contains('className: \"NoExport '))
                      fieldshow += a.substring(a.indexof('name:\"')+1,a.indexof('\",data:')+2)+',';  
                   }



                fieldshow = fieldshow.removeEnd(',');
                List<String> fieldlist= queryfields.split(',');
                for (String fsm : fieldlist) {
                    fsm = fsm.replace('&#124;','');
                     String api = fsm.substring(0,fsm.indexof('|'));
                    fsm = fsm.substring(fsm.indexof('|')+1,fsm.length());

                    excelFields.put(api,fsm);
                }
            }

            return excelFields;
        }

        private set;
    }
1
  • Have you tried giving a value for the headerValue attribute of your apex:column? Also, mind making an edit to your post to be more specific about the generated output & what your looking to replace it with (A picture might help here)? – battery.cord Apr 23 '18 at 16:55
1

You can use the headerValue attribute of your apex:column tag:

<apex:column value="{!c[f]}" headerValue="Some Value" /> 

Documentation

We can also see here That the apex:pageblocktable tag supports the "header" facet. This can be used to do control the appearance of the entire first row of the table if you need to get crazy (maybe merging headers for instance).

<apex:pageBlockTable>
    <apex:facet name="header">
        <th colspan="x"> Merged header </th><th colspan="y"> Anotherone </th>
    </apex:facet>
</apex:pageBlockTable>

And the apex:column also has a header facet if you want to get crazy as well:

<apex:column>
    <apex:facet name="header">
        <img src="lol.com/smileyface.png"/>
    </apex:facet>
</apex:column>

Update based on comment:

If you have a list of values in the controller, you'll need to convert that into a Map where the key is the api name of the field and the value is what you want in your header. Then it should be as easy as:

<apex:column value="{!c[f]}" headerValue="{!columnMap[f]}" /> 

Controller:

Map<String, String> columnMap = new Map<String, String>();
columnMap.put('FieldApiName__c', 'Some Value');
4
  • "Some value" here is basically a value to be printed from list<string> in controller. Can you please suggest me a way out? – Testing_SFDC Apr 23 '18 at 19:51
  • Updated my answer – gNerb Apr 23 '18 at 20:00
  • It is showing java.lang.nullpointer exception to me. Can you please help me why I am getting this? – Testing_SFDC Apr 23 '18 at 20:30
  • You'll have to update your question with your new code if you want me to get more specific. That being said, it's worth noting that this is not a "do it for me" website. There is some measure of work that you have to do on your own. – gNerb Apr 23 '18 at 21:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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