10

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?

4

11 回答 11

3

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.

于 2008-09-23T12:36:40.757 回答
1

如果它只是一个静态文件,并且您在 Linux 上使用 Apache,请检查名为 /etc/mime.types 的文件,并确保其中包含以下行以将 .xls 文件扩展名与正确的 MIME 类型相关联:

application/vnd.ms-excel    xls

我猜该文件的位置可能因系统而异,但它位于运行 RHEL4 的服务器上的 /etc/mime.types 中。

于 2008-09-23T12:45:34.417 回答
0

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.

于 2008-09-23T12:36:41.023 回答
0

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.)

于 2008-09-23T12:38:14.560 回答
0

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.

于 2008-09-23T12:38:42.313 回答
0

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.

于 2008-09-23T12:38:45.620 回答
0
  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.
于 2008-09-23T12:40:19.173 回答
0

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.

于 2008-09-23T12:41:16.380 回答
0

你不能,因为它取决于客户端机器。

例如,在 Windows 上,如果您希望它始终使用 Excel 打开,而不是在浏览器窗口中打开,您必须打开我的电脑、工具、文件夹选项、文件类型,选择 XLS 类型,然后单击高级。有两个复选框:在同一窗口中浏览和就地打开 Web 文档。取消选中两者,关闭浏览器窗口,再次打开并重试。

但是正如我所说:这取决于客户,你不能强迫它。

于 2008-09-23T13:13:01.827 回答
0

您可以使用LaunchinIE来做到这一点,这是一个 ActiveX 控件,它使 HTML 页面能够启动客户端计算机上的任何应用程序,而不会出现安全警告。

引自网站:“最后,网页可以毫无怨言地启动 Word、Excel 或任何其他企业应用程序。安全。”

为此,您必须在用户计算机上安装控件,并将允许执行本地应用程序的 URL 添加到 Windows 注册表。

该网站的另一句话是:“为了确保安全,LaunchinIE 需要在客户端进行仔细配置;由于这个限制,它只适合 Intranet 使用。”

我在我们的培训设施中使用 LaunchinIE,因此我可以使用 Internet Explorer 作为菜单,让用户选择机器设置。LaunchinIE 然后调用一个批处理脚本,将机器配置为最好地支持选定的训练。

于 2008-09-23T17:37:24.233 回答
0

以下是在打开已保存的 Excel 文件时恢复此弹出窗口的步骤。

  1. 右键单击windows [START] 按钮并选择Explore 打开Windows Explorer 窗口将打开。
  2. 从菜单中选择工具\文件夹选项...<br> • 选择文件类型选项卡并向下滚动文件列表。
    • 左键单击以突出显示XLS Microsoft Excel 工作表文件扩展名,然后单击高级按钮。

  3. 在“编辑文件类型”窗口中,取消选中“在同一窗口中浏览”选项。

  4. 单击确定按钮接受您的更改。

  5. 启动一个新的浏览器会话。下次您在收件箱中打开 Excel 电子表格时,系统会提示您以下窗口。请务必将“打开此类文件之前始终询问”保留为选中状态。单击“打开”按钮现在应该在 Excel 中打开您的文件。

于 2009-04-15T22:29:29.160 回答