浏览 5153 次
锁定老帖子 主题:一个比较难的Excel输出的问题请教
该帖已经被评为隐藏帖
|
|
---|---|
作者 | 正文 |
发表时间:2007-11-29
Ext(1.0)下表单提交到servlet中生成excel文件供用户下载,要求是浏览器自动出现下载对话框供用户保存到本地。经测试,生成excel本身没有任何问题,但浏览器不能出现下载对话框,请帮忙看看。 Ext表单主要代码如下: if (form_instance_create.isValid()) { aAddInstanceDlg.hide(); //alert(swjg_tf); form_instance_create.submit({ url:'/ssgly/ExcelOutPut.do', params:{ssgly:ldCb.getValue()}, waitMsg:'正在导出,请稍候........', reset: false, failure: function(form_instance_create, action) { Ext.MessageBox.alert('错误', action.result.errorInfo); }, success: function(form_instance_create, action) { Ext.MessageBox.alert('导出成功', action.result.info); aAddInstanceDlg.hide(); ds.load({params:{start:0, limit:myPageSize}}); } }); servlet代码如下: System.out.println("已有用户正在导出Excel!....."); PrintWriter out = null; out = response.getWriter(); //String fname="纳税人基本信息表"; String fname="nsrxxTable.xls"; OutputStream os = null; os = response.getOutputStream();// 取得输出流 response.reset();// 清空输出流 response.setHeader("Content-Disposition", "attachment; filename=\"" + fname + "\"");// 设定输出文件头 response.setContentType("application/vnd.ms-excel");// 定义输出类型 String ssgly=request.getParameter("ssgly"); Page page=new Page(1,MAX_PAGE_SIZES); Boolean asc=true; //String zg_swgy_dm="13408030006"; String nsrlx=""; String nsrsbh=""; List datas = new ArrayList(); List nsrxxList=businessService.listNsrxxService(page, asc, ssgly, nsrlx, nsrsbh); if (nsrxxList!=null){ for(Iterator data=nsrxxList.iterator();data.hasNext();){ Nsrxx nsrxx=(Nsrxx)data.next(); String[] sz=nsrxx.toStringSz(); datas.add(sz); try { OutputStream outPut = new FileOutputStream(fname); JxlExcelWriter jxlExcelWriter = new JxlExcelWriter(); //我自己定义的产生excel类 jxlExcelWriter.writeExcel(outPut, datas, new String[] { "纳税人识别号", "纳税人名称", "scjydz ", "fddbrmc ", "bsrmc ", "dhhm ", "hy_dm ", "hy_mc ", "hymx_dm ", "hymx_mc ", "nsrzt_dm ", "nsrzt_mc ", "djzclx_dm ", "djzclx_mc ", "zg_swgy_dm", "czry_mc ", "nsrlx ", "qzd_bz ", "zdsy_dm ", "zdsy_mc ", "nsr_swjg_dm" },"纳税人信息"); //os.close(); } catch (FileNotFoundException e) { out.print("{failure:true,errorInfo:'数据导出失败!'}"); e.printStackTrace(); } catch (IOException e) { out.print("{failure:true,errorInfo:'数据导出失败!'}"); e.printStackTrace(); } } } //end if out.print("{success:true,info:'outPut Excel Success!'}"); return null; } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2007-11-29
response.setContentType("application/octet-stream");
|
|
返回顶楼 | |
发表时间:2007-11-29
cbhyk 写道 response.setContentType("application/octet-stream");
这个我试了,还是没有效果。 |
|
返回顶楼 | |
发表时间:2007-11-29
用的是poi-3.0.1-FINAL-20070705.jar
ByteArrayOutputStream byteOutputStream=new ByteArrayOutputStream(); workBook.write(byteOutputStream); byte[] bytes=byteOutputStream.toByteArray(); response.reset(); response.setContentType("application/vnd.ms-excel"); response.setContentLength(bytes.length); response.setHeader("Content-Disposition","attachment;filename=\"exportfile.xls\""); ServletOutputStream ouputStream = response.getOutputStream(); ouputStream.write(bytes,0,bytes.length); ouputStream.flush(); ouputStream.close(); byteOutputStream.close(); |
|
返回顶楼 | |
发表时间:2007-11-29
我是这样实现的:
System.out.println("已有用户正在导出Excel!....."); PrintWriter out = null; String fname="nsrxxTable.xls"; out = response.getWriter(); //String fname="纳税人基本信息表"; String path="/ssgly"; File file=new File(path,fname); response.reset();// 清空输出流 response.setHeader("Content-Disposition", "attachment; filename=" + fname);// 设定输出文件头 response.setContentType("application/msexcel");// 定义输出类型 String ssgly=request.getParameter("ssgly"); System.out.println(ssgly); Page page=new Page(1,MAX_PAGE_SIZES); Boolean asc=true; //String zg_swgy_dm="13408030006"; ssgly="13408030006"; String nsrlx=""; String nsrsbh=""; List datas = new ArrayList(); List nsrxxList=businessService.listNsrxxService(page, asc, ssgly, nsrlx, nsrsbh); FileInputStream outPut = new FileInputStream(path+"/"+fname); if (nsrxxList!=null){ for(Iterator data=nsrxxList.iterator();data.hasNext();){ Nsrxx nsrxx=(Nsrxx)data.next(); String[] sz=nsrxx.toStringSz(); datas.add(sz); JxlExcelWriter jxlExcelWriter = new JxlExcelWriter(); jxlExcelWriter.writeExcel(file, datas, new String[] { "纳税人识别号", "纳税人名称", "scjydz ", "fddbrmc ", "bsrmc ", "dhhm ", "hy_dm ", "hy_mc ", "hymx_dm ", "hymx_mc ", "nsrzt_dm ", "nsrzt_mc ", "djzclx_dm ", "djzclx_mc ", "zg_swgy_dm", "czry_mc ", "nsrlx ", "qzd_bz ", "zdsy_dm ", "zdsy_mc ", "nsr_swjg_dm" },"纳税人信息"); //os.flush(); //os.close(); } } //end if // out.print("{success:true,info:'outPut Excel Success!'}"); System.out.println("数据流长度:"+outPut.toString()); int i; while ((i=outPut.read())!=-1){ out.write(i); } outPut.close(); //out.print("{success:true,info:'outPut Excel Success!'}"); out.close(); return null; } 不同之处在于使用了 变量 PrintWriter out ,但这和ext中表单提交后返回: out.print("{success:true,info:'outPut Excel Success!'}"); 冲突了。还请大家出出主意。 |
|
返回顶楼 | |
发表时间:2007-11-30
要做到浏览器启动出现下载对话框,客户端只能采用传统的form/submit方式,这个在Ext官方论坛上讨论过。
我的代码如下(实现网格数据导出到Excel): (1)添加form html: var html2 = [ '<div id="grid-export-form" class="x-hidden">', ' <form method="POST" name="GridExportForm">', ... ' </form>', '</div> '].join(''); Ext.DomHelper.append(document.body, { html : html2 }); (2)通过提交动作调用后台Servlet导出到Excel: document.GridExportForm.action = this.url+'&export2excel=y'; ... document.GridExportForm.submit(); |
|
返回顶楼 | |
发表时间:2007-11-30
这个问题已解决。楼上说的对。还是在Ext管网上找到答案的。
但也很感谢fangzhouxing。看来你也是ext官网的常客。呵呵。 最后,对robbin说上几句:有关ext上的帖子看仔细了,不要随便转移到别处。这点ext官网上的网管做的比较专业。 |
|
返回顶楼 | |
发表时间:2008-03-29
可以利用 upload 方式实现
|
|
返回顶楼 | |