18

I have a web page which links to an Excel 2007 worksheet. It is a .xls file and not .xlsx file. When I click on the link I get the usual dialog box to either open/save the Excel file. On clicking 'Open', I get the following warning message-

The file you are trying to open, 'filename.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?

Can I some how suppress this warning message programmatically (i.e. to hide it or prevent it from showing up?) I'm using ColdFusion for web development.

Shawn Holmes
  • 3,752
  • 22
  • 25
Arnkrishn
  • 29,828
  • 40
  • 114
  • 128
  • Why not just leave the extension as .XLSX? – ceejayoz Jun 02 '09 at 15:19
  • 1
    Is this for one specific XLS file, or every XLS file you try? Have you considered the possibility that the one file in question might actually be corrupted? If it's every XLS file, then it sounds like an Excel/Windows option, which you can't control on the user's computer from ColdFusion (or any other server-side language). – Adam Tuttle Jun 02 '09 at 16:58
  • 3
    Adam, this is a new Excel 'feature' - before you could simply produce a CSV file or HTML table and name it XLS, but now this results in a stupid dialog box every time you open the file (except if the individual client disables it). – Peter Boughton Jun 02 '09 at 17:39
  • You can try use SheetJS, refer to my answer [here](https://stackoverflow.com/a/52712803/3967044). – Kevin Ng Oct 09 '18 at 03:13

8 Answers8

20

If you don’t want to look for a solution, but just want to solve the problem, insert this key in your registry to suppress the notification:

[HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\Security] “ExtensionHardening”=dword:00000000

You can accomplish the above by doing the following:

  1. Open your Registry (Start -> Run -> regedit.exe)
  2. Navigate to HKEY_CURRENT_USER\SOFTWARE\MICROSOFT\OFFICE\12.0\EXCEL\SECURITY
  3. Right click in the right window and choose New -> DWORD
  4. Type “ExtensionHardening” as the name (without the quotes)
  5. Verify that the data has the value “0″
jgallant
  • 11,143
  • 1
  • 38
  • 72
12

This problem results from a feature called Extension Hardening, and you can find more information about it here

I've run into this problem a lot in my projects, and like Jon said, turning off Extension Hardening is something that will have to be done by each client side user.

Unfortunately, the link above also states that there are no expected changes to this code until at least Office 14.

Jason Pyeron
  • 2,388
  • 1
  • 22
  • 31
Matthew Jones
  • 25,644
  • 17
  • 102
  • 155
  • 2
    I think that only applies if you're using CSV and save as XLS. However, if you construct a real excel file, then it should be fine. CF9 cfspreadsheet will be your friend. :) – Henry Jun 25 '09 at 23:53
  • I agree with @Henry, I've just encountered this warning message when I tried to open .xls file that was exported from JTable in java using FileWriter class. – Mr. Xymon Apr 26 '12 at 07:22
2

For the old Excel format (.xls)

Assuming you create your spreadsheet object with this syntax:

<cfset sheet = SpreadsheetNew() /> 

You would use this to create the download:

<cfheader name="content-disposition" value="attachment;filename=my_spreadsheet.xls">
<cfcontent type="application/vnd.ms-excel" variable="#spreadsheetReadBinary(sheet)#" reset="true">

For the newer Excel format (.xlsx)

Create your spreadsheet object with this syntax:

<cfset sheet = SpreadsheetNew("", "true") />

And use this to create the download:

<cfheader name="content-disposition" value="attachment;filename=my_xml_spreadsheet.xlsx">
<cfcontent type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" variable="#spreadsheetReadBinary(sheet)#" reset="true">

Thanks to Raymond Camden for the bulk of this (his example was for creating the spreadsheet in the older format -- I updated for the newer format and use slightly different MIME values).

Leigh
  • 28,765
  • 10
  • 55
  • 103
geekmuse
  • 360
  • 1
  • 5
  • While this works on my CF9 Server, it doesn't on CF11. It says, that the file cannot be opened, due to file-format or file-endig is invalid. Is there anything else on CF11? Do I have to install OpenOffice on the server? It works, when sending the file as email-attachment. Might be somehting with the IIS. – Boris Dec 03 '14 at 15:33
0

I had same kind of issue and client complaining about it. I had done much googling but no luck. Finally I have created spreadsheet using cfspreadsheet, I know it require additional work than directly creating through HTML but it remove warning.

Pritesh Patel
  • 1,969
  • 1
  • 18
  • 32
0

I don't believe you can hide it from the user. Those kinds of warnings are external to your browser.

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
fmsf
  • 36,317
  • 49
  • 147
  • 195
0

The message suggests that the file contents and the file extension don't match. So either it's an XLSX file or the current file on the server is broken in some way. I suggest to try to open the file with a ZIP tool. If that works, it's really an XLSX-format file. Then you can fix the warning by renaming the file.

If that's not the case, something happened to the file.

But either way, suppressing the warning is not a solution to your problem. The next time a virus comes along and asks your customers to open nakedgrls.gif.exe, you better didn't mess with the security settings.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
0

If you're using the handy cfcontent trick to output as an excel file, consider using cfx_query2excel, it will write real excel files that should get you past this. It's a Java library and was worth it.

Best of luck.

Jas Panesar
  • 6,597
  • 3
  • 36
  • 47
0

If you are not specifying the file extension, it is probably getting named as a .cfm file with the content type as "application/vnd-excel" which causes this warning.

Try adding this before the file output begins:

<cfheader name="Content-Disposition" value="attachment;filename=myexcelfile.xls">
<cfcontent type="application/ms-excel" reset="true">
eapen
  • 579
  • 6
  • 15