简体   繁体   English

使用 Internet Explorer 打开 Excel 文档时出现问题

[英]Issue opening Excel documents with Internet Explorer

I have run into an issue in which IE does not open up the Save As/Open dialog box for an Excel document like Firefox does.我遇到了一个问题,IE 没有像 Firefox 那样打开 Excel 文档的另存为/打开对话框。

So I have created a servlet filter that is using '*.xls' as the url pattern.因此,我创建了一个使用“*.xls”作为 url 模式的 servlet 过滤器。 The issue that I now face (since this is the first filter I have created) is how to get the name of the file that the user wants so that the dialog box gets populated correctly.我现在面临的问题(因为这是我创建的第一个过滤器)是如何获取用户想要的文件名,以便正确填充对话框。 Currently the filter is invoked when the user selects a link on a given page.当前,当用户选择给定页面上的链接时,将调用过滤器。

Here is what I came up with:这是我想出的:

The above is what I have doFilter().以上就是我所拥有的doFilter()。

String fileName = "fileName.xls";

HttpServletRequest httpRequest = (HttpServletRequest) pRequest;
String requestURI = httpRequest.getRequestURI();

if(StringUtils.isNotBlank(requestURI))
{
  String uri[] = StringUtils.split(requestURI, '/');
  fileName = uri[uri.length - 1];
}

HttpServletResponse httpResponse = (HttpServletResponse) pResponse;
httpResponse.setContentType("application/vnd.ms-excel");
httpResponse.setHeader("Content-disposition", "attachment; filename=\"" + fileName +"\"");

web.xml: web.xml:

<filter>
    <filter-name>ExcelFilter</filter-name>
    <filter-class>vsg.rp.common.ExcelFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>ExcelFilter</filter-name>
    <url-pattern>*.xls</url-pattern>
</filter-mapping>

This all is working on my development box: Windows XP, JBoss, Eclipse, Oracle.这一切都在我的开发箱上工作:Windows XP,JBoss,Eclipse,Z30162418B6C40F243 But when it runs on the test server—Linux, Apache/JBoss, Oracle—it does not work.但是当它在测试服务器(Linux、Apache/JBoss、Oracle)上运行时,它就不起作用了。 It appears that the filter is not even being called, no errors thrown, etc. Any idea as to why this would happen?似乎甚至没有调用过滤器,也没有抛出错误等。知道为什么会发生这种情况吗?

You want the content type set appropriately as well as the content disposition header, thus:您希望正确设置内容类型以及内容配置 header,因此:

response.setContentType("application/vnd.ms-excel"); response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\""); response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");

Use the Content-Disposition HTTP header and set it to something like:使用Content-Disposition HTTP header 并将其设置为:

attachment; filename=myworkbook.xls

The is a Microsoft Knowledge Base Article all about this problem.这是一篇关于此问题的Microsoft 知识库文章 And here is an example of setting Content-Disposition in Java code .这是在 Java 代码中设置 Content-Disposition 的示例

In addition to setting the headers for the content type, you'll also want to ensure that the server DOES NOT tell the browser to NOT CACHE the file.除了为内容类型设置标题之外,您还需要确保服务器不会告诉浏览器不缓存文件。

In IE land, if you tell IE not to cache the file, it happily downloads the file... then tries to open the file from the directory it saved the file to... However since the headers said "don't cache" it takes this literally, and doesn't save the file, thus Excel throws an error saying, "There is no file.".在 IE 领域,如果您告诉 IE 不要缓存文件,它会愉快地下载文件...然后尝试从将文件保存到的目录中打开文件...但是由于标题说“不要缓存”它从字面上理解,并且不保存文件,因此 Excel 会抛出一个错误,说“没有文件。”。

Long story short, tell IE to CACHE the file .长话短说,告诉 IE 缓存文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM