在IE中显示CSV文件

时间:2012-03-12 13:52:01

标签: java internet-explorer jsf csv

这是我的代码:

public String download(){
    if(log.isDebugEnabled()){
       log.info("Entered for downloading: ");
    }
    FacesContext facesContext = FacesContext.getCurrentInstance(); 
    ExternalContext externalContext = facesContext.getExternalContext(); 
    HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
    try{
        List<TableController> list = fetchTechniciansList();
        Iterator<TableController> iter = list.iterator();
        ServletOutputStream out = null;
        response.reset();`
        response.setContentType("application/csv"); 
        response.setHeader("Cache-Control", "private"); 
        response.setHeader("Pragma", "public");
        response.addHeader("Accept-Ranges", "none");
        response.setHeader("Content-disposition","inline; filename=\""
            + "technician_list.csv" + "\"");
        out=response.getOutputStream();
        out.write("First Name".getBytes());
        out.write(',');
        out.write("Last Name".getBytes());
        out.write(',');
        out.write("User ID".getBytes());
        out.write('\n');
        Integer i=0; 
        while(iter.hasNext()) {             
            out.write(list.get(i).getFirst_name().getBytes());
            out.write(',');
            out.write(list.get(i).getBean_last_name().getBytes());
            out.write(',');
            out.write(list.get(i).getUser_id().toString().getBytes());
            out.write('\n');
            i++;            
            iter.next();
        }       
        out.flush();
        out.close();
        facesContext.responseComplete();

    }
    catch (Exception e) {
        // TODO: handle exception
        log.error("downloading error");
        log.info("Exited");
    }
    return null;
}

我在HTML中有命令链接:

<h:commandLink value="Download Technician List" action="#{tableControllerBean.download}" style="float:right" target="_blank"></h:commandLink>

调用此download()方法。问题是该列表直接在Excel中打开,但不在IE中打开。我在Windows 7中使用IE 8.如何在IE中打开它?

2 个答案:

答案 0 :(得分:0)

找到以下页面,其中包含如何在IE中显示CVS和XLS的说明。说明适用于XP,但可能会给你一些线索......

http://www.winxptutor.com/iedocs.htm

您还可以尝试其他ContentType,例如文本/无格式。

答案 1 :(得分:0)

用户代理对向服务器发出请求时收到的内容的作用是100%由用户代理决定。简而言之,除了建议用户如何配置系统以将* .csv文件视为文本并将其显示在页面中之外,您无能为力。

那就是说,要检查的一件事是你在标题中发回的媒体类型。许多浏览器将根据媒体类型处理不同的响应。

最后,如果在浏览器中显示内容至关重要,您可以考虑使用HTML + CSS注释CSV内容,使其以您希望的方式显示。浏览器旨在处理HTML,并且通常倾向于将其他内容传递给系统中安装的其他更合适的应用程序。在许多情况下,根据用户对CSV内容的要求,实际的电子表格程序可能 对他们更有用,因为它具有比浏览器更容易提供的功能。< / p>

相关问题