jsf 1.1保存为框问题

时间:2011-09-18 08:43:51

标签: jsf

我在Tomcat 6.0.26和Java 1.4版本上使用JSF 1.1。我必须在我的项目中使用它们。我的jsf页面有一个数据表,我希望它们导出但是保存为框。单击export excel commandlink后,它会显示如下浏览器:

  

ÐÏࡱá; THYþÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿRootEntryÿÿÿÿÿÿÿÿ@Workbookÿÿÿÿÿÿÿÿÿÿÿÿ0ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ ÿÿÓAA°Áâ\ pmusty B°a =œ¯¼= h \:¾#8X @“·Ú1ÈÿArial1ÈÿArial1ÈÿArial1ÈÿArial”$“#,## 0 _);(”$“#,## 0)!”$“#, ## 0 _); [红色]( “$” #,## 0) “” $ “#,## 0.00 _);(” $ “#,## 0.00)' ”“ $” #,## 0.00 _); [红色](“$”#,## 0.00)7 * 2 _(“$”*#,## 0 _); _(“$”*(#,## 0); _(“$” *“ - ”); (@ )。))(*#,## 0 _); _(*(#,## 0); _(*“ - “); (@ )?,:(”$“*#,## 0.00 _); _(”$“*(#,## 0.00) ; _(“$”*“ - ”?? ); (@ )6 + 1 (*#,## 0.00 _); _(*(#, ## 0.00); _(*“ - ”?? ); (@_)àõÿÀàõÿôÀàõÿôààõÿôÀàõÿôÀàõÿôÀàõÿôÀàõÿôÀàõÿôÀàõÿôÀàõÿôÀàõÿôô àõÿôààõÿôààõÿôÀàÀà+õÿàà)õÿøàà,õÿøàà*õÿøàà€œ€€€€€€€€€€€€€€€€€€€€€€€€€ ADSOYAD DEPARTMANEVCEPDAHILIerdemaktasYonetici02mesutozenIT03mustafaaydemirNetweaver01yunustastutanDeneme0506004ÿT'IΆ»ÌÁdü©ñÒMbP?_ * +,€%ÿÁƒ“¡”d ,,à?à?Uýýýýýýýýýýýýýýýýý Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y YYYYY>!¶@ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ“#$þÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ

我该如何解决这个问题?

thnx balusC。但问题没有解决。这是我的代码..

`public String export(List list){

    Iterator i1 = list.iterator();
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet s = wb.createSheet();
    HSSFRow r = null;
    HSSFCell c = null;
    int i = 0;

    try {

        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext externalContext = facesContext.getExternalContext();
        HttpServletResponse resp = (HttpServletResponse) externalContext
                .getResponse();
        ServletOutputStream out = resp.getOutputStream();
        resp.setContentType("application/vnd.ms-excel");


        HSSFRow r2 = s.createRow(0);

        for (short cellnum = (short) 0; cellnum < 6; cellnum++) {

            c = r2.createCell(cellnum);

            if (cellnum == 0)
                c.setCellValue("AD");
            else if (cellnum == 1)
                c.setCellValue("SOYAD");
            else if (cellnum == 2)
                c.setCellValue("DEPARTMAN");
            else if (cellnum == 3)
                c.setCellValue("EV");
            else if (cellnum == 4)
                c.setCellValue("CEP");

            else if (cellnum == 5)
                c.setCellValue("DAHILI");
        }


        while (i1.hasNext()) {
            md1 = new Model();
            md1 = (Model) i1.next();

            int rownum = i + 1;

            r2 = s.createRow(rownum);

            for (short cellnum = (short) 0; cellnum < 6; cellnum++) {

                c = r2.createCell(cellnum);

                if (cellnum == 0)
                    c.setCellValue(md1.getAd());
                else if (cellnum == 1)
                    c.setCellValue(md1.getSoyad());
                else if (cellnum == 2)
                    c.setCellValue(md1.getDepartman());
                else if (cellnum == 3)
                    c.setCellValue(md1.getEv());
                else if (cellnum == 4)
                    c.setCellValue(md1.getCep());

                else if (cellnum == 5)
                    c.setCellValue(md1.getDahili());

            }

            i++;
        }
        resp.setHeader("content-disposition:",
                "attachment; filename= \"TelefonRehberi.xls\"");

        wb.write(out);
        out.flush();
        out.close();

        facesContext.responseComplete();
    } catch (Exception e) {
        System.out.println("Error: " + e);
    }

    return "";
}`

1 个答案:

答案 0 :(得分:1)

您需要设置内容类型标题,以便浏览器了解它是什么类型的文件。

response.setHeader("Content-Type", "application/vnd.ms-excel");

您需要设置内容处置标题,以便浏览器弹出另存为对话框:

response.setHeader("Content-Disposition", "attachment;filename=\"" + yourFileName + "\"");

您需要确保将文件写为二进制数据,而不是字符数据。

InputStream input = getExcelFileContentSomehow();
OutputStream output = response.getOutputStream();
// Write input to output.

更新:根据您的代码段,您的Content-Disposition标头不正确。标题名称末尾有一个冒号。删除它。