2

I want to develop MS Excel download feature for some of my data, thus I created a VF page with following attributes on the apex:page tag.

contentType="application/vnd.ms-excel#somefile.xls" cache="true" applyHtmlTag="false" standardStylesheets="false" sidebar="false" showHeader="false"

With this I get the file downloaded but when I try to open it, the Excel application (I use 2013 version) shows a warning message before showing the data.

The file format and extension of 'somefile.xls' don't match. The file could be corrupted or unsafe. Unless you trust its source, don't open it. Do you want to open it anyway?

I've used XML Spreadsheet markup for this and have avoided the use of Visualforce tags wherever I could, just to make sure the output is not HTML but native Spreadsheet markup. The file is not corrupted for sure since I can see the expected data as I open it after clicking "Yes" on the warning message.

Could anybody help me make this download without that warning message ?

2
  • Have you tried changing .xls to .xlsx in the contentType attribute?
    – Folkert
    Mar 17, 2016 at 6:45
  • Yes I did, but it doesn't solve the problem, rather it refuses even open the file with following error - "Excel cannot open the file 'somefile.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file." Mar 17, 2016 at 6:55

1 Answer 1

0

I dont have a solution for this problem but can explain why this happens as i had gone through with the same issue.

This error is because VF page while generating the xls file actually saves the html page as a xls file.

This alert is due to a feature called extension hardening introduced by excel 2007 onwards

The alert is a new security feature in Excel 2007 called Extension Hardening, which ensures that the file content being opened matches the extension type specified in the shell command that is attempting to open the file. Because the MIME types listed above are associated with the .XLS extension, the file must be in XLS (BIFF8) file format to open without this warning prompt. If the file type is a different format (such as HTML, XML, CSV, etc.) the prompt is expected since the file content is different that the extension or MIME type. The alert is first seen when opening the file from a URL site directly. If you cancel the alert, the open will fail, but then IE will attempt to download the file and open again using a different shell command. Depending on what the file contents is and what extension IE gives the file it downloads, you may see the second open attempt succeed, or you may see the prompt again but with a different filename in the alert dialog.

https://blogs.msdn.microsoft.com/vsofficedeveloper/2008/03/11/excel-2007-extension-warning-on-opening-excel-workbook-from-a-web-site-2/

As of now i dont think there is a way to remove it, i would be interested to know if there is a way.

The workaround we did was to ask the sales guy to convert the xls file by opening the xls file first and clicking the convert option by File-> convert and then send the file to the client.(This would actually convert into a proper excel format). This would be a extra step that our client did as it was better than manually create the excel file.

1

Not the answer you're looking for? Browse other questions tagged .