function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Visualforce Messaging Attachment as Excel XLS

I am building an automated email which I am adding a Visualforce attachment that I would like to open as an XLS file  in excel. I am doing so using the following messaging:attachment syntax.


    <messaging:attachment renderAs="application/" filename="testexcel.xls">
        <c:TestExcel />

 The component I am using I kept as simple as possible just to get it working, which is a 3 column table with 10 rows of contacts. Here is my component code.


<apex:component controller="TestClass" access="global">
      <apex:dataTable value="{!Contacts}" var="c">
         <apex:column value="{!c.Name}"/>
         <apex:column value="{!c.Email}"/>
         <apex:column value="{!c.Phone}"/>


The message sends and the attachment shows up fine on the email. The issue I am having is while opening the file I get the "The file you are trying to open, 'testexcel.xls', is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?" message because it is not encoded correctly. Clicking open, opens the file just fine, but I would really like to find a way to prevent this warning message from popping up when my users open the file. I originally considered doing it in CSV, but I need to format certain cells with certain styling.


Does anyone know of any tags or headers I can try adding so that this warning message does not pop up, or does anyone have any idea how to force a formatted Visualforce table to open in Excel without warning? Thanks!


You will have to output the results in an XML format that Excel will recognize.


Something like this:


<head><META http-equiv="Content-Type" content="text/html; charset=UTF-8"></head><table><tr><th filter=all>Account Name</th><th filter=all>Created Date</th></tr>
<tr><td style="">GenePoint</td><td align=right>5/3/2012</td></tr>
<tr><td style="">United Oil &amp; Gas, UK</td><td align=right>5/3/2012</td></tr>
<tr><td style="">United Oil &amp; Gas, Singapore</td><td align=right>5/3/2012</td></tr>
<tr><td style="">Edge Communications</td><td align=right>5/3/2012</td></tr>
<tr><td style="">Burlington Textiles Corp of America</td><td align=right>5/3/2012</td></tr>
<tr><td style="">Pyramid Construction Inc.</td><td align=right>5/3/2012</td></tr>
<tr><td style="">Dickenson plc</td><td align=right>5/3/2012</td></tr>
<tr><td style="">Grand Hotels &amp; Resorts Ltd</td><td align=right>5/3/2012</td></tr>
<tr><td style="">Express Logistics and Transport</td><td align=right>5/3/2012</td></tr>
<tr><td style="">University of Arizona</td><td align=right>5/3/2012</td></tr>
<tr><td style="">United Oil &amp; Gas Corp.</td><td align=right>5/3/2012</td></tr>
<tr><td style="">sForce</td><td align=right>5/3/2012</td></tr>

This is how itself gets around the message without outputting real Office binary code. Go ahead and give it a try.


Strangely enough, copying that exact code, pasting it into notepad and saving it as .xls - you still get the corrupt file message. There must be something added to excel files that is not visible when opening through notepad.




I am also getting the same error message while opening the excel.

Did u get the solution?




No solution, Microsoft must have some hidden attribute appended to the file type that recognizes it as native excel.

Sohit BhardwajSohit Bhardwaj

Any Resolution on this?


I tried below but no success

1. showheader=false
2. no form tag on VF page
3. use style=””
4. Use Meta tag :

Page is simple : just data table on account. Also if I open excel file in notepad i see junk script.




Try below.


<apex:page controller="Controller" cache="true" contenttype="application/x-excel#FILENAME.xls" showheader="false">
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
		<apex:repeat value="{!Data}" var="d">