现在要实现下载excel功能,但excel不是存放在硬盘的,而是服务器Java生成的workbook。试标签的href,但是href只能输入文件路径。
现在我需要的是:前端点击一下,然后到达服务器的 excelExport.do方法里面,方法里生成workbook,在传送excel到前端,前端弹出下载对话框,然后下载。 我已经会生成excel,只是不知道怎么传送给前端。急!
下载excel,excel是服务器动态生成,没有URL,所以不能用《a》
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
8条回答
- 奔跑的小鱼儿 2017-01-07 00:55关注
你这个问题有很多解决办法,第一个:没必要弹出下载提示,点击后生成excel直接输出,实现下载,以下是参考代码
response.setContentType("application/msexcel;"); response.setHeader("Content-Disposition", new String(("attachment;filename="+"error.xls").getBytes("GB2312"), "UTF-8")); File f = new File("d:/20121211165820140_err.xls"); //File f = new File(targetFolder.getPath()+File.separator+fName); FileInputStream in = new FileInputStream(f); byte b[] = new byte[1024]; int i = 0; ServletOutputStream out = response.getOutputStream(); while((i=in.read(b))!= -1){ out.write(b, 0, i); } out.flush(); out.close(); in.close();
第二种:保留下载提示,生成excel后把它保存到服务器然后再把地址传给前台,不推荐这种方法,就像脱裤子放屁一样
File path=new File(Util.DOWNLOAD+"导出Excel");//创建文件夹 if(!path.exists()) { path.mkdirs();//不存在该路径则自动生成 } String path1 = Util.DOWNLOAD+"/导出Excel"+File.separator+filename+".xls"; FileOutputStream fileout = new FileOutputStream(path1); wb.write(fileout); fileout.flush(); fileout.close(); return name+".xls" HTML中: window.location.href = path;//path就是返回的name+".xls"
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报