+ Start a Discussion
mjohnson-TICmjohnson-TIC 

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/vnd.ms-excel" filename="testexcel.xls">
        <c:TestExcel />
    </messaging:attachment>

 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}"/>
      </apex:dataTable>
</apex:component>

 

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!

sfdcfoxsfdcfox

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="vnd.ms-excel.numberformat:@">GenePoint</td><td align=right>5/3/2012</td></tr>
<tr><td style="vnd.ms-excel.numberformat:@">United Oil &amp; Gas, UK</td><td align=right>5/3/2012</td></tr>
<tr><td style="vnd.ms-excel.numberformat:@">United Oil &amp; Gas, Singapore</td><td align=right>5/3/2012</td></tr>
<tr><td style="vnd.ms-excel.numberformat:@">Edge Communications</td><td align=right>5/3/2012</td></tr>
<tr><td style="vnd.ms-excel.numberformat:@">Burlington Textiles Corp of America</td><td align=right>5/3/2012</td></tr>
<tr><td style="vnd.ms-excel.numberformat:@">Pyramid Construction Inc.</td><td align=right>5/3/2012</td></tr>
<tr><td style="vnd.ms-excel.numberformat:@">Dickenson plc</td><td align=right>5/3/2012</td></tr>
<tr><td style="vnd.ms-excel.numberformat:@">Grand Hotels &amp; Resorts Ltd</td><td align=right>5/3/2012</td></tr>
<tr><td style="vnd.ms-excel.numberformat:@">Express Logistics and Transport</td><td align=right>5/3/2012</td></tr>
<tr><td style="vnd.ms-excel.numberformat:@">University of Arizona</td><td align=right>5/3/2012</td></tr>
<tr><td style="vnd.ms-excel.numberformat:@">United Oil &amp; Gas Corp.</td><td align=right>5/3/2012</td></tr>
<tr><td style="vnd.ms-excel.numberformat:@">sForce</td><td align=right>5/3/2012</td></tr>
</table>

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

mjohnson-TICmjohnson-TIC

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.

imishraimishra

Hi,

 

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

Did u get the solution?

 

Thanks.

mjohnson-TICmjohnson-TIC

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=”vnd.ms-excel.numberformat:@”
4. Use Meta tag :

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

 

thanks

mjohnson-TICmjohnson-TIC

Try below.

 

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