1

The snippet below is used to download a file, with extension is xls. After exporting, the downloaded file is not opening in Excel 2013 or previous versions, nor in Office 365, until I unblock the file from its properties. It is opening only in MS Excel 2016. Can some please help me how to resolve this and how to make it to open in "Protected View" by default?

Actually yesterday I got the issue. Without HTML tags it exports properly. If I add HTML tags, it causes the issue.

<cfheader name="Content-Disposition" value="attachment; filename=xyz.xls">
<cfcontent type="application/vnd.ms-excel">
<cfoutput> <table border='1'> <tr> <td>123</td> <td>456</td> <td>789</td></tr> </table> </cfoutput>
Leigh
  • 28,765
  • 10
  • 55
  • 103
Kiran
  • 71
  • 1
  • 14
  • 1
    see if this helps: https://support.microsoft.com/en-us/kb/3181507 – cyboashu Aug 09 '16 at 04:03
  • thank you, changing settings we can get the work done. But need to do through code.(coldfusion) – Kiran Aug 09 '16 at 04:16
  • 1
    you can't do it in code. that's the whole idea behind the security/trust center settings. – cyboashu Aug 09 '16 at 14:30
  • actually the issue is with html tags. without html tags its exporting properly if i add html tags its causing issue. – Kiran Aug 10 '16 at 03:50
  • What do you mean by "add html tags"? Are you actually downloading a *true* Excel file or something different - like html that Excel may be able to open? – Leigh Aug 10 '16 at 13:11
  • 123 456 789
    for example now is html tags right. if i use them and export its not opening. if i just enter random text inside cfoutput its opening properly.
    – Kiran Aug 11 '16 at 04:04
  • I am using that code in a cfm file and opening that file in browser. So it'll download an excel file right. – Kiran Aug 11 '16 at 04:29
  • That is *not* an Excel file. It is HTML. Older versions of Excel opened such files without complaint. However, newer versions will not due to a [security feature called Extension Hardening](https://blogs.msdn.microsoft.com/vsofficedeveloper/2008/03/11/excel-2007-extension-warning-on-opening-excel-workbook-from-a-web-site-2/). As long content (ie HTML) differs from what the headers claim (ie Excel File) you run the risk of Excel failing to open the file. The only reliable solution is to ensure they both match, such as generating a *real* Excel file, instead of HTML. – Leigh Aug 11 '16 at 14:45
  • See [How to suppress the file corrupt warning at Excel download?](http://stackoverflow.com/questions/940045/how-to-suppress-the-file-corrupt-warning-at-excel-download). Answer - It cannot be done from the server side. The extension must match the content. – Leigh Aug 11 '16 at 14:47

1 Answers1

2

As cyboashu mentioned, there have been some changes in M$ office security protocols, where you can't open HTML files with XLS(X) extensions in Excel. We've had the same problem with our application and had to use the ColdFusion Spreadsheet functions. (Which are actually pretty cool. Documentation on them is on the Adobe-site.)

I'd suggest a code like:

<cfset xlsSheet = spreadsheetNew("xyz.xls","no")>
<cfset spreadsheetAddRow(xlsSheet,"123,456,789")>
<cfset spreadsheetWrite(xlsSheet,"xyz.xls",true)>
<cfheader name="Content-Disposition" value="attachment; filename=xyz.xls">
<cfcontent type="application/vnd.ms-excel">

By the way, you can also try the custom tags Ben Nadel made. Here is a link to the GIT. It's a bit more complicated to set up, but you can make all Excel files with it that you might wish.

Sander
  • 390
  • 1
  • 4
  • 13
  • Yes, due to the new security features like [Extension Hardening](https://blogs.msdn.microsoft.com/vsofficedeveloper/2008/03/11/excel-2007-extension-warning-on-opening-excel-workbook-from-a-web-site-2/), the old tricks of using HTML are iffy at best. The only reliable method is to ensure the actual content and file extensions match. – Leigh Aug 11 '16 at 14:54
  • No i am not trying to open html files with xls(x) extension. i mean if i add any html tags (table , anchor , paragraph, span , div) inside . its causing this issue and which is opening without any issues for random text inside . – Kiran Aug 16 '16 at 06:31
  • If you use html tags like etc inside a file with the xls(x) extension, newer versions of Excel won't open that file. Maybe we are misunderstanding you though. Do you have any problems displaying the cfcontent without the cfheader?
    – Sander Aug 16 '16 at 11:05
  • Without cfheader ? Then where do i specify the file name , content type etc. is their any other way – Kiran Aug 16 '16 at 12:43
  • @Kiran: I meant: do you have problems displaying the content in the browser instead of a file? Or are you only having problems displaying html content in an xls file? – Sander Aug 16 '16 at 13:15
  • Oh sorry for that, Yes we are suppose to display in browser and the same should be exported in excel as well. Problem is in xls file. in browser its perfect. – Kiran Aug 17 '16 at 03:58
  • Yep, that's your problem then. We do something simular where we have something that needs to be exported to PDF or Excel. We used to be able to generate the content for this in the same function, but now we need to use one function to build up the HTML for the PDF and one function to generate real Excel-content. You can do that with the code I provided. – Sander Aug 17 '16 at 08:55