[SalesForce] Downloading from visualforce page to excel

Hi i have page which contains the contacts information ,these information is displaying in pageblock .This data is exported to excel when i click export button ,the data is exporting but the issue is , in the excel file css also downloading from page.Though i didnt add any css in page but downloading the css of page block.

here is my code snippet:

public class download {
public boolean isExport{get; set;}


    public list<contact> getcontacts(){

isExport= false;
list<contact> cc = new list<contact>();
cc= [select id,firstname,lastname,email from contact];
return cc;
}
public PageReference getexport() {

        isExport= true;
        return null;
    }
}

<apex:page controller="download" contentType="{!IF(isExport = true, 'application/vnd.ms-excel#report.xls', '')}" >
<apex:form >
<apex:commandbutton id="button" value="Export" action="{!getexport}" reRender=""/>
<button onclick="openWin()">Open "myWindow"</button>
 <a href="excel"  target="_blank">download</a>
    <apex:pageBlock title="Contacts">
        <apex:pageBlockTable value="{!contacts}" var="contact">
            <apex:column value="{!contact.firstname}"/>
            <apex:column value="{!contact.lastname}"/>
            <apex:column value="{!contact.email}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
    </apex:form>

</apex:page>

like this coming in excel sheet along with data

[{"displayName": "excel","name": "excel","viewstateSize":
1.91,"percentOfParent": 100.0,"percentOfTotalViewstate": 100.0,"leaf": false,"isGroupable": false,"identityCode":
"2796bc40d38351913b4847fb564c0523","children": [{"displayName":
"Component Tree","name": "Component Tree","viewstateSize":
0.25,"percentOfParent": 13.0,"percentOfTotalViewstate": 13.0,"leaf": true,"iconCls": "task","isGroupable": false,"identityCode":
"1630e98078efadc941226a9d6d013209"},{"displayName": "State","name":
"State","viewstateSize": 1.8,"percentOfParent":
94.0,"percentOfTotalViewstate": 94.0,"leaf": false,"isGroupable": false,"identityCode": "496d794a0b11ea33448f993f5bd5958","children":
[{"displayName": "Expressions","name": "Expressions","viewstateSize":
0.52,"percentOfParent": 29.0,"percentOfTotalViewstate": 27.0,"leaf": true,"iconCls": "task","isGroupable": false,"identityCode":
"10da1aa4a08187126b7978a9c670f6b6"},{"displayName":
"Controllers","name": "Controllers","viewstateSize":
0.41,"percentOfParent": 23.0,"percentOfTotalViewstate": 22.0,"leaf": false,"isGroupable": false,"identityCode":
"5a02804e8b095ae7a166094fb8f68dae","children": [{"displayName":
"Getcontacts","name": "Getcontacts","type": "Page excel
Controller","viewstateSize": 0.41,"percentOfParent":
100.0,"percentOfTotalViewstate": 22.0,"leaf": false,"iconCls": "task","isGroupable": false,"identityCode":
"8a671e22b51cc5846840dbb9812e8caf","children": [{"displayName":
"error","name": "error","type": "Boolean","viewstateSize":
0.08,"percentOfParent": 20.0,"percentOfTotalViewstate": 4.0,"value": "false","leaf": true,"iconCls": "task","isGroupable":
false,"identityCode":
"e96efa568e49f8406f4e93c94c5785bd"},{"displayName": "getcc
[0]","name": "getcc","type": "List>","viewstateSize":
0.22,"percentOfParent": 54.0,"percentOfTotalViewstate": 12.0,"leaf": true,"isGroupable": true,"identityCode":
"9f2246134c18340dfa716fe3a695f70f","count": 0},{"displayName":
"isExport","name": "isExport","type": "Boolean","viewstateSize":
0.08,"percentOfParent": 20.0,"percentOfTotalViewstate": 4.0,"value": "true","leaf": true,"iconCls": "task","isGroupable":
false,"identityCode":
"27dd83478c46c16bbf6651ff651d0f1c"}]}]},{"displayName":
"Internal","name": "Internal","viewstateSize": 0.87,"percentOfParent":
48.0,"percentOfTotalViewstate": 46.0,"leaf": true,"iconCls": "task","isGroupable": false,"identityCode":
"15bb2636e995792041b3a319a0d6be6c"}]}],"viewstateLimit":
135.0,"percentOfViewstateLimit": 1.41}]

Best Answer

If you want to make the exported result tidy, it's best to completely avoid VF constructs and use basic HTML. Here's an outline of an export that I've used in the past:

    <apex:page standardController="Object__c" extensions="anExtension"  showHeader="false" standardStylesheets="false" sidebar="false">
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
    </head>

    <table>
  <tr>
    <apex:repeat value="{!$ObjectType.Object__c.FieldSets.FieldSetName}" var="f">
      <th>{!f.label}</th>
    </apex:repeat>
  </tr>

    <apex:repeat value="{!myList}" var="item">
      <tr>
        <apex:repeat value="{!$ObjectType.Object__c.FieldSets.FieldSetName}" var="f">
          <td>{!item[f]}</td>
        </apex:repeat>
      </tr>
    </apex:repeat>
</table>
</apex:page>

The standardStylesheets="false" solves the bit that you were asking for. You'll probably want the Content-Type tag in case you have any accented characters in your data. And by using raw HTML you don't get odd bits of VF tags hanging around.

Related Topic