java导出Excel弹出下载框

问题描述

action中;publicvoidgetDownloadFile(){if(this.obj!=null){Integerdepid=Integer.parseInt(this.depId);Filefile=this.employeeService.getInputStream(depid);//request=(HttpServletRequest)context.get(ServletActionContext.HTTP_REQUEST);//response=(HttpServletResponse)context.get(ServletActionContext.HTTP_RESPONSE);Stringstr="/Excel/"+file.getName();Stringpath=request.getSession().getServletContext().getRealPath(str);DownloadFiles.download(path,response);}}serviceimple中:publicFilegetInputStream(Integerdepid){HSSFWorkbookwb=newHSSFWorkbook();HSSFSheetsheet=wb.createSheet("sheet1");HSSFCellStylestyle=wb.createCellStyle();//设置表头的类型style.setAlignment(HSSFCellStyle.ALIGN_CENTER);HSSFCellStylestyle1=wb.createCellStyle();//设置表头的类型style1.setAlignment(HSSFCellStyle.ALIGN_LEFT);HSSFFontfont=wb.createFont();//设置字体//表头标题HSSFRowrow=sheet.createRow(0);HSSFCellcell=row.createCell(0);cell.setCellValue("序号");sheet.setColumnWidth(0,3000);//设置列的宽度font.setColor(HSSFFont.COLOR_NORMAL);//设置单元格字体的颜色.//font.setFontHeight((short)350);//设置单元字体高度style.setFont(font);//设置字体风格cell.setCellStyle(style);////数据List<Employee>list=this.employeeDAO.getEmployeeInfo(depid);for(inti=0;i<list.size();i++){Employeeemployee=list.get(i);row=sheet.createRow(i+1);cell=row.createCell(0);cell.setCellValue(i+1);cell.setCellStyle(style1);}//第一种方法:为每个用户产生不同的文件,随机文件名.xls,但下载完成后要删除(不一定及时删除,如过1小时删除)//将数据从内存中放到真正的test.xls中//StringfileName=CharacterUtil.makeRandomString(10)+".xls";StringfileName="D:/apache-tomcat-6.0.29/webapps/assetManage/Excel/empInfo.xls";finalFilefile=newFile(fileName);//临时文件test.xls默认会放在tomcat/bin下面,//最好生成随机文件名.xls,并在使用完成后,动态删除这些文件try{OutputStreamout=newFileOutputStream(file);wb.write(out);out.close();}catch(Exceptione){e.printStackTrace();}//InputStreamis=null;//try{////is=newFileInputStream(file);//}catch(FileNotFoundExceptione){//e.printStackTrace();//}//用后台线程删除生成的临时文件,这样有问题:万一服务器意外停止,此文件就永远不能删除了//解决办法:让服务器启动时,用servlet检查是否有xls文件,有了就删除//finalfile=newFile(fileName);//newThread(newRunnable(){//内部类//publicvoidrun(){////线程睡10秒,让用户有时间来下载//try{//Thread.sleep(100);//线程睡10秒//}catch(InterruptedExceptione){//e.printStackTrace();//}//file.delete();//删除临时文件//}//}).start();returnfile;}实体中publicclassDownloadFiles{publicDownloadFiles(){}publicstaticHttpServletResponsedownload(Stringpath,HttpServletResponseresponse){try{//path是指欲下载的文件的路径Filefile=newFile(path);//取得文件名。Stringfilename=file.getName();filename=newString(filename.getBytes("GBK"),"iso8859-1");//以流的形式下载文件。InputStreamfis=newBufferedInputStream(newFileInputStream(path));byte[]buffer=newbyte[fis.available()];fis.read(buffer);fis.close();//清空responseresponse.reset();//设置response的Header//HTTP响应弹出下载框的response.setContentType("application/vnd.ms-excel;charset=gb2312");//把文件流写入http响应里面response.addHeader("Content-Disposition","inline;filename="+newString(filename.getBytes()));//attachment---作为附件下载,inline---在线打开response.addHeader("Content-Length",""+file.length());response.sendRedirect(filename);//response.addHeader("Content-Type","application/vnd.ms-excel");//response.setHeader("Content-Disposition","attachment;filename=test.xls");OutputStreamtoClient=newBufferedOutputStream(response.getOutputStream());toClient.write(buffer);toClient.flush();toClient.close();}catch(IOExceptionex){ex.printStackTrace();}returnresponse;}}struts.xml中<actionname="employee_*"method="{1}"class="employeeAction"><resultname="showSuccess"type="json"></result><resultname="Error">/index.jsp</result><resultname="success"type="stream"><paramname="contentType">application/vnd.ms-excel</param><paramname="contentDisposition">attachment;filename="empInfo.xls"</param><paramname="inputName">downloadFile</param></result></action>各位大侠帮忙看看哪里出错了?Stringpath=request.getSession().getServletContext().getRealPath(str);运行到这里就跳过该方法..........

解决方案

解决方案二:
SpringMVC中才难实现啊...
解决方案三:
该回复于2012-08-29 14:12:59被版主删除
解决方案四:
怎样用js导出啊

时间: 2024-08-28 11:49:35

java导出Excel弹出下载框的相关文章

jxl导出excel,如果不通过写死路径来弹出保存框,如何实现导出excel弹出保存框这个功能?

问题描述 web开发中,我实现了通过模板来导出excel数据,是通过写一个模板,和一个目标文件来实现的,这两个文件是写在项目里面的,但是如果是多个人查询内容不同,然后同时导出这个文件的时候可能就出现导出的结果不是自己想要的!请问各位大虾门,怎么才能避免这种情况?也可能是我的方法写的不对,希望指点一下!听说导出excel是根据模板虚拟出一个需要导出的excel表格,当保存后,这个表格就没了!我是不懂!下面是我写的一个jxl导出excel:java代码:public class ExportExce

struts2.0-为什么java导出excel表格没有弹出下载框

问题描述 为什么java导出excel表格没有弹出下载框 /** 导出存支列表 */ public void exportDepositList(){ HttpServletResponse response = Struts2Utils.getResponse(); ServletOutputStream sos = null; WritableWorkbook wb = null; List depositList = null; String str = getSql(); deposit

ie11的excel导出-java poi excel导出时,在ie11上面弹出下载框时文件名后缀为xxx.xls[1]

问题描述 java poi excel导出时,在ie11上面弹出下载框时文件名后缀为xxx.xls[1] 测试在ie8或ie9上面是正确的,但是在ie11上面就会出现这个[1].

JavaWeb导出Excel文件并弹出下载框_java

一.引言 在Java Web开发中经常涉及到报表,最近做的项目中需要实现将数据库中的数据显示为表格,并且实现导出为Excel文件的功能. 二.相关jar包 使用POI可以很好的解决Excel的导入和导出的问题,POI下载地址: poi-3.6-20091214.jar 三.关键代码 首先导入上述jar包. 在生成excel时一般数据源形式为一个List,下面把生成Excel格式的代码贴出来: /** * 以下为生成Excel操作 */ // 1.创建一个workbook,对应一个Excel文件

easyui导出excel无法弹出下载框的快速解决方法_jquery

之前用ajax做的,代码如下(ActionUrl为一般处理程序ashx的路径): $.ajax({ url: ActionUrl + '?action=export&ID=' + $('#fm_ID').val(), dataType: 'json', success: function (jsonstr) { //top.art.dialog.tips('导出成功!'); } }); 没办法弹出下载框. 直接浏览器地址栏输入相关页面地址并打开可以弹出下载框下载,考虑可能是iframe的缘故.

JSP下载。中文文件名不弹出下载框。控制台中文显示????

问题描述 JSP下载.中文文件名不弹出下载框.控制台中文显示????麻烦各位了.谢谢<%@pagecontentType="text/html;charset=GB2312"%><%@pageimport="java.io.*"%><%response.reset();try{Stringstr=request.getParameter("name1");str=newString(str.getBytes(&quo

c# geckofx浏览器控件点击下载链接无法弹出下载框

问题描述 本人最近在用c#做一款winform软件,但在内嵌geckofx浏览器时其他页面显示都是正常的,但是就是点击网页中一些下载的链接时无法弹出如其他浏览器那样的下载窗口,请问是什么原因啊,有什么办法么?谢谢各位专家了.

js导出Excel弹出对话框

WEB页面导出为EXCEL文档的方法 列标题1 列标题2 列标题3 列标题4 列标题5 aaa bbb ccc ddd eee AAA BBB CCC DDD EEE FFF GGG HHH III JJJ

struts2.1.8 ,使用自带json插件struts2-json-plugin-2.1.8.1.客户端得不到json对象,弹出下载action提示框

问题描述 struts2.1.8 ,使用自带struts2-json-plugin-2.1.8.1.jar把action对象返回客户端的json对象.注释配置,客户端总是弹出下载页面,打开后是正常的json数据,各位可以帮忙看看?代码很简单,困老好几天了,非常感谢,代码如下: Java代码 import org.apache.struts2.convention.annotation.ParentPackage;import org.apache.struts2.convention.annot