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.
   <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}"/>

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:


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

<apex:page standardController="Account" contenttype="application/">

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


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

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.
  • 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.

  • michaelforce

    That’s pretty slick! Thanks Sati

  • David

    Is this only for Standard Object?

  • Sati Hillyer

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

  • 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

  • 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:

  • Ben Hizak

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

  • 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/
    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!

    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

  • 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.

    • Pratyush Kumar

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

    • 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.

  • 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

  • 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!