views:

7846

answers:

12

If you call javascript window.open and pass a url to a .xls file it open on some machines in the browser window. How can you force it into Excel?

+7  A: 

You must se Content-Disposition as "attachment" in response header:

  Response.Clear()
  Response.ContentEncoding = System.Text.Encoding.UTF8
  Response.ContentType = "text/xml"
  Response.Expires = -1
  //main statement 
  Response.AddHeader("Content-Disposition", "attachment; filename=yourFile.xls");
  Response.AddHeader("Content-Length", fileLength)

i hope i help you!

stefano m
This is some sort of a Header-change which (AFAIK) cannot be done from JavaScript.
Huppie
Shouldn't that be application/vnd.ms-excel rather than text/xml?
David Dorward
This should explain that it is not JavaScript
Liam
+2  A: 

Only the users machine can "force" it into Excel. That said, 99% of the time if you send the correct mime-type and a user has Excel, then it will open in Excel assuming they approve.

And only the server can send the correct mime-type. The document type you pass to a JavaScript window.open call will have no effect on this. In fact, calling window.open will at best just open a superfluous window.

It's best to just link to the document with <a href="foo.xls">. And provided your server is sending a mime-type of application/x-excel or application/x-msexcel this will almost always nudge the browser into opening a new window with the Excel document.

pcorcoran
A: 

AFAIK you can't do this with JavaScript alone. If you have some sort of scripting language on the server's side you can alter the header to force a download.

Here's a simple tutorial in PHP, but you can easily find one in your favorite language.

Huppie
A: 

You cannot force it into Excel. You can allow the browser to handle it whichever way it is configured to do so, or you can try to force it to download the file and let the user open if from their desktop. To force a download, search for "force download" and your server-side language (PHP, ASP.NET, JSP, etc.)

Liam
A: 

I don't think you can: you cannot call external programs using Javascript for security reasons. Assuming that the user has Excel installed, you may want to open the new window without the address bar to give the user "the illusion" that the file has been opened with Excel in Internet Explorer.

h3rald
A: 

I wouldn't think this is possible from javascript due to security issues, there would be nothing stopping a rogue webpage from opening dozens of excel/word instances.

Could you not set a hyperlink to the url of the .xls, that way the user would get the usual download prompt to view the file.

Luke Smith
A: 
  1. Set the http content type to the Excel datatype: application/vnd.ms-excel
  2. You shouldn't need to redirect to a new window, but you will get a popup asking the user to save or open the file.
  3. In relation to (2): I'd worry if a browser could launch an external application and load data into it automatically without user intervention.
JeeBee
A: 

This is a setting in each user's browser and not in something that can be set by code. So unfortunately you do not have control of that.

jschoen
A: 

If it's just a static file, and you're using Apache on Linux, check for a file called /etc/mime.types, and ensure that it has the following line in there to associate the .xls file extension with the correct MIME type:

application/vnd.ms-excel    xls

I'm guessing the location of that file might vary across systems, but it's in /etc/mime.types on my server which is running RHEL4.

MB
A: 

You can not, as it depends on the client machine.

For example on Windows if you want it to always open it with Excel, not in the browser window, you have to open My Computer, Tools, Folder Options, File Types, select the XLS type, and click on Advanced. There are two checkboxes: Browse in same window and Open web documents in place. Uncheck both, close browser window, open it again and try again.

However as I said: it depends on the client, you can not force it.

Biri
A: 

You can do this using LaunchinIE, an ActiveX Control that will enable HTML pages to start whatever application on the client's machine, without security warnings.

Quote from the site: "At last, web pages can start Word, Excel, or any other corporate application without complaints. Securely."

For this you do have to install the control on the user machine and also add the URL that is allowed to execute local applications to the Windows registry.

Another quote from the site: "To ensure security, LaunchinIE needs to be carefully configured client-side; due to this restriction it's only fit for intranet use."

I use LaunchinIE in our training facility so I can use Internet Explorer as a menu which lets the user choose the machine setup. LaunchinIE then calls a batch script that configures the machine to best support the selected training.

Mackaaij
A: 

Here are the steps to get this pop-up back when opening a saved Excel file.

  1. Right-Click on the windows [START] button and select Explore to open Windows Explorer window will open.
  2. From the menu select Tools \ Folder Options…
    • Choose the File Types tab and scroll down the list of files.
    • Left-Click to highlight the XLS Microsoft Excel Worksheet file extension and click on the Advanced button.

  3. In the Edit File Type window, Uncheck the “Browse in same window” option.

  4. Click OK button to accept your changes.

  5. Start a new browser session. The next time you open your Excel spreadsheet in your Inbox, you should be prompted with the following window. Be sure to leave the “Always ask before opening this type of file” as checked. Clicking the Open button should now open your file in Excel.