MIME类型/内容类型,用于在IE和IE中使用Excel打开CSV文件。 Firefox [英] MIME type/Content-type for opening CSV files using Excel in IE & Firefox

查看:189
本文介绍了MIME类型/内容类型,用于在IE和IE中使用Excel打开CSV文件。 Firefox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,让CSV输出可以识别&在Excel中打开。在我的web应用程序中,我有一个java servlet,向用户返回搜索结果。搜索结果由Apache Solr服务器提供。在GUI前端有一个选项,允许用户请求CSV格式的搜索结果。如果选择此选项,则通过添加wt = csv参数来对Solr重新运行查询。然后,一个java过滤器添加头以返回结果作为CSV格式化文本。但它不能正常工作。




  • 在IE(ver 8.0)中,系统提示您打开或保存文件。如果我选择打开;它只是转储CSV格式的文本在屏幕上,而不是在Excel中打开它。如果我选择保存;我收到一条错误消息 - 无法打开此网站。请求的网站不可用或找不到。

  • 在Firefox中(版本10.0.9)我得到提示打开或保存文件。但是打开CSV文件;默认选项是记事本& Excel甚至不列出。如果我点击保存,那么它会下载CSV文件&我可以在Excel中打开它,并且它可以被Excel正确识别。



我尝试了各种标题&

  // ((HttpServletResponse)response).setHeader(\"Content-Type,application / octet; charset = utf-8); 
((HttpServletResponse)response).setHeader(Content-Type,text / csv);
//((HttpServletResponse )response ).setHeader (\"Content-Type,application / vnd.ms-excel);
//((HttpServletResponse )response ).setHeader(Cache-Control,private,private,max-age = 0,must-revalidate);
//((HttpServletResponse)response).setHeader(\"Content-Encoding,gzip);
// ((HttpServletResponse )response ).setHeader(\"Content-Encoding,binary);
((HttpServletResponse)response).setHeader(Content-Disposition,attachment; filename = Download.C​​SV);
//((HttpServletResponse )response ).setHeader (\"Content-Disposition,inline; filename = Download.C​​SV;);

但没有一个似乎工作。我使用Firefox检查正在发送回来的头。我看到以下内容 -



Content-Disposition:attachment; filename = Download.C​​SV



内容类型:text / plain; charset = UTF-8



Jetty(7.xy-SNAPSHOT)



传输编码:chunked



Vary:Accept-Encoding

即使内容类型指定为csv或excel;当它到达浏览器它被解释为文本/平原&我认为这是问题。不知道我应该设置什么头或编码?有什么建议么?感谢。

解决方案

我做类似的事情,这是我使用的代码的一部分:

 日期dt = new Date(); 
SimpleDateFormat fmt = new SimpleDateFormat(yyyy-MM-dd_HHmmss);
String filename = fmt.format(dt)+.csv;
//设置输出
String contentType =application / vnd.ms-excel;
FacesContext fc = FacesContext.getCurrentInstance();
HttpServletResponse response =(HttpServletResponse)fc.getExternalContext()。getResponse();
response.setHeader(Content-disposition,attachment; filename =+ filename);
response.setContentType(contentType);请注意,我使用变量contentType告诉servlet将生成什么类型​​的文件。



<



希望它适合你。


I have been having an issue with getting CSV output to be recognized & opened in Excel. In my web application I have a java servlet that returns search results to the user. The search results are delivered by an Apache Solr server. There is an option on the GUI front-end that allows the user to request the search results in "CSV" format. If this option is selected then the query is re-run against Solr by adding a "wt=csv" param. Then a java filter adds headers to return the results as CSV formatted-text. But it’s not working correctly.

  • In IE (ver 8.0) I am prompted to "Open" or "Save" the file. If I select "Open"; it just dumps CSV-formatted text on the screen instead of opening it in Excel. And if I select "Save"; I get an error message – "Unable to open this internet site. The requested site is either unavailable or cannot be found."
  • In Firefox (ver 10.0.9) also; I get prompted to open or save the file. But for opening the CSV file; the default option is Notepad & Excel is not even listed. If I click "Save" then it does download the CSV file & I can open it in Excel and it’s correctly recognized by Excel.

I have tried a variety of headers & combinations –

  //((HttpServletResponse)response).setHeader("Content-Type", "application/octet; charset=utf-8");
  ((HttpServletResponse)response).setHeader("Content-Type", "text/csv");
  //((HttpServletResponse)response).setHeader("Content-Type", "application/vnd.ms-excel");
  //((HttpServletResponse)response).setHeader("Cache-Control", "private, private,max-age=0,must-revalidate");
  //((HttpServletResponse)response).setHeader("Content-Encoding", "gzip");
  //((HttpServletResponse)response).setHeader("Content-Encoding", "binary");
  ((HttpServletResponse)response).setHeader("Content-Disposition", "attachment; filename=Download.CSV");
  //((HttpServletResponse)response).setHeader("Content-Disposition", "inline; filename=Download.CSV;");

But none of them seem to be working. I used Firefox to inspect the headers that are being sent back & I see the following –

Content-Disposition:attachment; filename=Download.CSV

Content-Type:text/plain;charset=UTF-8

Server:Jetty(7.x.y-SNAPSHOT)

Transfer-Encoding:chunked

Vary:Accept-Encoding

Even though the content-type is specified as csv Or excel; when it reaches the browser it’s being interpreted as text/plain & I think that’s the issue. Not sure what header Or encoding I should be setting? Any suggestions? Thanks.

解决方案

I'm doing something similar and this is part of the code I'm using:

    Date dt = new Date();
    SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd_HHmmss");
    String filename = fmt.format(dt) + ".csv";
    //Setup the output
    String contentType = "application/vnd.ms-excel";
    FacesContext fc = FacesContext.getCurrentInstance();
    HttpServletResponse response = (HttpServletResponse)fc.getExternalContext().getResponse();
    response.setHeader("Content-disposition","attachment; filename=" + filename);
    response.setContentType(contentType);

Note that I use the variable "contentType" to tell the servlet what kind of file will be generated.

Hope it works for you.

这篇关于MIME类型/内容类型,用于在IE和IE中使用Excel打开CSV文件。 Firefox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆