导出到客户端站点

时间:2014-07-14 07:30:16

标签: java excel jsf export

我有一个web应用程序,可以在办公室的服务器上的websphere上运行。主页面显示了包含数据的表格。当我从菜单中选择导出时,它必须将数据导出到Excel工作表。我使用apache poi。当我制作我的工作簿时,我通过FileOutputStream编写它。在这里,我不知道自己该做什么。当我选择新文件(" C:\ Temp \ test.xls")时,它将在服务器站点上写入文件。但我希望应用程序将文件导出到客户端文件夹上的excel。我怎么能这样做?

THX

更新: 我在这个问题上搜索了很多,他们都回答了同样的问题,所以我尝试了他们的解决方案:

    HttpServletResponse res = (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
    res.setContentType("application/vnd.ms-excel");  
    res.setHeader("Content-disposition",  "attachment; filename=test.xls"); 

    try {
      ServletOutputStream fileOut = res.getOutputStream();
      workbook.write(fileOut);
      fileOut.flush();
      fileOut.close();
    } 
    catch (FileNotFoundException e) {
        System.out.println("... FileNotFoundException ...");
        e.printStackTrace();
    } 
    catch (IOException e) {
        System.out.println("... IOException ...");
        e.printStackTrace();
    }  
    System.out.println("... excel file created ...");
    FacesContext faces = FacesContext.getCurrentInstance();  
    faces.responseComplete();

但我仍然没有弹出屏幕,我可以在客户端选择方向。所以这个解决方案不起作用。

1 个答案:

答案 0 :(得分:0)

您需要将文件直接写入响应输出流

OutputStream out = response.getOutputStream();
FileInputStream in = new FileInputStream(my_excel);
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) > 0){
    out.write(buffer, 0, length);
}
in.close();
out.flush();