POI - 导出的文件保存到客户端 [英] POI - save exported file to a client

查看:284
本文介绍了POI - 导出的文件保存到客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我dataTable的出口(绑定DataList控件),以excel文件调用由 xlsWorkBook prepare下面的方法(C:\\\\ export.xls);

方法的一部分:

 公共无效xlsWorkBook prepare(字符串文件)抛出IOException异常
{
  / * $ P $工作簿的ppare * /
  工作簿WB =新HSSFWorkbook();
  地图<弦乐,CellStyle>风格= Style.createStyles(WB);
  ...  对于(FoodList项目:DataList控件)
  {
    ...
  }  / *创建文件* /
  FileOutputStream中FILEOUT;
  尝试
  {
    FILEOUT =新的FileOutputStream(文件);
    wb.write(FILEOUT);
    fileOut.flush();
    fileOut.close();
  }
  赶上(FileNotFoundException异常E)
  {
    e.printStackTrace();
  }
}

但该路径相关的服务器。如何将其保存在客户侧??

SOLUTION (基于Rangi林答案):

  HttpServletResponse的解析度=(HttpServletResponse的)FacesContext.getCurrentInstance()getExternalContext()的GetResponse()。
res.setContentType(应用程序/ vnd.ms - Excel中);
res.setHeader(内容处置,附件;文件名= TEST.XLS);尝试
{
  ServletOutputStream的FILEOUT = res.getOutputStream();
  wb.write(FILEOUT);
  fileOut.flush();
  fileOut.close();
}
赶上(FileNotFoundException异常E)
{
  e.printStackTrace();
}
FacesContext中面临= FacesContext.getCurrentInstance();
faces.responseComplete();


解决方案

如果我得到你的权利,你需要通过HTTP文件传输回客户端。
相反的FileOutputStream ,你可以使用的getOutputStream()方法 HttpServletResponse的

code应该是这样的:

 字符串文件名=excel.xls;
HttpServletResponse的响应= GETRESPONSE(); //获取ServletResponse的
response.setContentType(应用程序/ vnd.ms - Excel中); //设置MIME类型
response.addHeader(内容处置,附件;文件名=+文件名);
OutputStream的OUT = response.getOutputStream()
wb.write(出);
了out.flush();

注:我没有测试它,但你应该能够得到的想法

I'm exporting dataTable (binding dataList) to excel file calling the following method by xlsWorkBookPrepare("c:\\export.xls");

Part of method:

public void xlsWorkBookPrepare(String file) throws IOException
{
  /* prepare of workbook */
  Workbook wb = new HSSFWorkbook();
  Map<String, CellStyle> styles = Style.createStyles(wb);
  ... 

  for (FoodList item : dataList)
  { 
    ...
  }  

  /* create file */
  FileOutputStream fileOut;
  try 
  {
    fileOut = new FileOutputStream(file);
    wb.write(fileOut);
    fileOut.flush();
    fileOut.close();
  } 
  catch (FileNotFoundException e) 
  {
    e.printStackTrace();
  }  
}

But the path is related to the server. How to save it on side of client??

SOLUTION (based on Rangi Lin answer):

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();
  wb.write(fileOut);
  fileOut.flush();
  fileOut.close();
} 
catch (FileNotFoundException e) 
{
  e.printStackTrace();
}  
FacesContext faces = FacesContext.getCurrentInstance();  
faces.responseComplete(); 

解决方案

If I get you right, you need to transfer back the file to client through http. Instead of FileOutputStream, you can use getOutputStream() method in HttpServletResponse.

Code should look like this :

String fileName = "excel.xls";
HttpServletResponse response = getResponse(); // get ServletResponse
response.setContentType("application/vnd.ms-excel"); // Set up mime type
response.addHeader("Content-Disposition", "attachment; filename=" + fileName);
OutputStream out = response.getOutputStream()
wb.write(out);
out.flush();

Note : I didn't test it, but you should able to get the idea.

这篇关于POI - 导出的文件保存到客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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