java使用poi实现excel导出之后如何弹出保存提示框

无论使用poi还是使用jxl导出excel都需要用到流

一种是outputstrean,另一种fileoutputstream

第一种:如果想要弹出保存的提示框必须加入下列三句
response.setContentType("application/vnd.ms-excel; charset=utf-8");
response.setHeader("Content-Disposition","attachment;filename="+filename);
response.setCharacterEncoding("utf-8");
OutputStream os=response.getOutputStream();
在使用第一种的时候,我用的ajax请求。导致excel无法导出,最后我直接请求可以导出
原因是:ajax也用到了response.getWriter()方法 要将 数据结果回传,这里 我虽然 放弃了 回传的写入流writer 参数, 但是ajax还是会默认的去掉用,把流已经占用了,当然返回不了任何东西了。
第二种:
action中使用
FileOutputStream fos=new FileOutputStream(file);
此时可以使用ajax请求,在导出成功后返回文件路径,在页面中使用window.open(path);即可打开导出的excel文件

猜你喜欢

转载自m635674608.iteye.com/blog/2387587