You probably know that a Visualforce Page can be easily converted to a PDF (if not, check out Quote2PDF), but did know Visualforce can also generate a Microsoft Excel Worksheet?

By simply modifying the ContentType attribute on the <apex:page> tag, your Visualforce code will automatically generate an Excel document. For example, the following code will create a table of Contact data for a given Account:

<apex:page standardController="Account">
   <apex:pageBlock title="Hello {!$User.FirstName}!">
      You are viewing the {!account.name} account.
   </apex:pageBlock>
   <apex:pageBlock title="Contacts">
      <apex:pageBlockTable value="{!account.Contacts}" var="contact">
         <apex:column value="{!contact.Name}"/>
         <apex:column value="{!contact.Email}"/>
         <apex:column value="{!contact.Phone}"/>
      </apex:pageBlockTable>
   </apex:pageBlock>
</apex:page>

If an ID of a valid Account record is specified as a query parameter in the URL for the page, you will see the following table of data:

TableInVF

We can tell Visualforce to convert this page to an Excel doc by adding:

<apex:page standardController="Account" contenttype="application/vnd.ms-excel">

The next time the page is loaded, you will receive the following prompt:

ExcelGeneration

The spreadsheet generated will convert your pageBlockTable to their respective columns, shown here:

VF2Excel
This is a powerful and easy-to-use feature. You can learn more about ContentType in the documentation.

tagged Bookmark the permalink. Trackbacks are closed, but you can post a comment.
  • http://www.modelmetrics.com/author/rcarlberg/ Reid Carlberg

    Love it.

  • Val

    And it can me XML too
    contenttype=”text/xml”…or indeed csv !

  • Marc

    I KNOW I can find a use for this.
    Thanks!

  • michaelforce

    That’s pretty slick! Thanks Sati

  • David

    Is this only for Standard Object?

  • http://profile.typekey.com/1221579805s4939/ Sati Hillyer

    Hi David,
    This will work for both Standard and Custom objects. Thanks for asking!
    Sati

  • David

    I tried this in CS2 and kept getting error. Even for a blank pageblocktable for a custom object. Is this only in NS3?

  • David

    Sati: Have you tried running this in Internet Explorer? I ran into a problem using IE. but it works fine in FireFox

  • http://developer.force.com Andrew Waite

    This should work across all instances including sandbox (CS2). David, perhaps you can open a discussion with more detail about your error on the Visualforce discussion board.

  • Sati Hillyer

    I just tried in IE6 and wasn’t able to generate the excel doc. You may want to try against IE7. If it’s still not working, please log a case here: http://www.appexchange.com/support

  • Ben Hizak

    any way to control Cell’s formatting? (font, size, color)
    if yes,
    any way to create conditional formatting?
    Thanks in advance!

  • http://www.acfsolutions.com Doug Sharpe

    I suspect your problem is the known bug when rendering VF as Excel under https. That has been an issue since Summer 08 – not sure why it hasn’t been fixed. In the meantime, use http instead.
    That means you need to assemble the full url which is tricky with sandbox vs production. You can do something like this in your controller:
    PageReference excelPage = new PageReference(‘http://’+ApexPages.currentPage().getHeaders().get(‘Host’)+’/apex/HousefileExcel’);

  • Varun

    Feature is great. but everytime the file is downloaded the Present page where export button is displayed Becomes BLANK. How can I resolve that issue?

  • Sumiran

    I tried the same thing and is working fine.But when I do the exporting in MAC OS I get the list plus some gibberish code at the top of my excel document->
    if(!window.sfdcPage){window.sfdcPage = new ApexPage();} UserContext.initialize({‘isAccessibleMode’:false,’ampm’:['AM','PM'],’locale’:'en_US’,'dateTimeFormat’:'M/d/yyyy h:mm a’,'today’:’1/16/2009 1:31 AM’,'dateFormat’:'M/d/yyyy’,'language’:'en_US’,'siteUrlPrefix’:”,’userPreferences’:[{'value':false,'index':119,'name':'HideUserLayoutStdFieldInfo'} ,{'value':false,'index':87,'name':'HideInlineSchedulingSplash'} ,{'value':false,'index':116,'name':'HideRPPWarning'} ,{'value':false,'index':115,'name':'DefaultTaskSendNotification'} ,{'value':false,'index':114,'name':'OverrideTaskSendNotification'} ,{'value':false,'index':112,'name':'HideInlineEditSplash'} ],’startOfWeek’:’1′} );

  • Igor Androsov

    How can we pass query string parameters to control what data is being exported to Excel file?
    It is easy to export data on generic query but I need to control export query parameters and finding that its not possible to do with this method of Excel export. Is there any way I can have a dynamic query data be exported and downloaded by users as Excel file?

  • kelly

    but if datatable is active ,it is according to the selectlist , how should i can do to export to excel

  • Chang Shin

    Exporting in excel is ok in Standard.
    but with extension, the excel is blank data.
    It is like this:
    …. standardController=… extensions=… contenttype=..application/vnd.ms-excel..
    Do you know how can get excel file with extension?
    Chang Shin

  • Rajesh Yeddula

    how to generate worksheets(multiple) dynamically in same excel sheet using visualforc

    • Hemant Patel

      hi did u get any solution for this ?

  • Sandeep Akula

    Hi Sati,
    I’m using the same proceedure to export Opportunities to excel. Everything work fine on every browser except for chrome. When I hit export button, it open a new page downloads the excel file and stays on new page. But for all other browsers, the new page closes itself. I need this to happen in chrome. How to deal with this. Any Ideas?

  • Anonymous

    wonderful. wonderful wonderful!
    THANKYOU!

    any chance someone could add a codeshare of extract to CSV and email the cSV as an attachment?
    like the 2009 winter of QuoteUI to pdf, then send the pdf as a file attachment.

    and …
    be able to do this from a related list OR a list view

  • Anonymous

    love it – thankyou.

    What do I need to change to extract to csv from
    - a related list
    - list view
    thankyou

  • Keith Clarke

    This JavaScript text:

    if(!window.sfdcPage){window.sfdcPage = new ApexPage();} UserContext.initialize…

    is still appearing in the Excel as Sumiran reported 3 years ago.

    • http://www.facebook.com/pratyush22 Pratyush Kumar

      Can we remove the Javascript text from the generated Excel Spreadsheet ?

    • http://www.facebook.com/pratyush22 Pratyush Kumar

      This is basically because we’re using pageblockTable instead of dataTable

      Replacing with the resolved this issue(not sure why), and removed the javascript code from the excel file.

  • http://www.facebook.com/pratyush22 Pratyush Kumar

    Could anybody help me know, why am I receiving the Javascript Literal – if(!window.sfdcPage){window.sfdcPage = new ApexPage();} UserContext.initialize… on the first cell of the generated excel file only when viewed on Mac and not when viewed on Windows

  • http://www.facebook.com/pratyush22 Pratyush Kumar

    Issue Resolved – This is basically because we’re using pageblockTable instead of dataTable

    Replacing with the resolved this issue(not sure why), and removed the javascript code from the excel file.

  • brarelabben

    Is it possible to do this across all accounts?

  • katja

    By using
    Excel’s ISBLANK function, you can prevent such unsatisfactory results from being displayed! I like!